我是magento2的初学者,我会尝试检索所有与订单相关的内容 信息和该信息只是想在管理员方面显示 如订单的ID,订单状态,商店名称,订单日期, 付款方式等 在这里,我尝试获取订单信息,如订单ID,订单状态等 页面将重定向到主页/仪表板。
我使用产品Collection对象获取产品相关信息,但我如何获取所有订单信息。
我不知道如何使用订单收集获取订单信息 对象或订单存储库对象以及该订单信息显示 在magento2的管理员方面。
以下是我的观点或info.phtml
文件:
<?php $_order = $block->getOrder(3);
$orderAdminDate = $block->formatDate(
$block->getOrderAdminDate($_order->getCreatedAt()),
\IntlDateFormatter::MEDIUM, true );
echo 'Order Status = '.$_order->getStatusLabel();
echo 'Order Id = '. $_order->getRealOrderId(); ?>
这是我的Info.php
阻止文件:
class Info extends \Magento\Sales\Block\Adminhtml\Order\AbstractOrder
{
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Sales\Model\Order\Address\Renderer $addressRenderer,
array $data = []
) { $this->addressRenderer = $addressRenderer;
parent::__construct($context, $adminHelper, $data);
}
public function getOrder()
{ return $this->_coreRegistry->registry('current_order'); } }
答案 0 :(得分:4)
您可以查看以下代码
<?php
namespace 'moduleNameSpace';
class ModelClass extends \Magento\Framework\View\Element\Template
{
protected $_orderCollectionFactory;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
array $data = []
) {
$this->_orderCollectionFactory = $orderCollectionFactory;
parent::__construct($context, $data);
$this->_isScopePrivate = true;
}
protected function _construct()
{
parent::_construct();
$this->_orderCollectionFactory->create()->addAttributeToSelect('*')
}
public function getSalesOrderCollection(array $filters = []){
return $this->orderCollectionFactory;
}
}
答案 1 :(得分:3)
获取所有订单信息
public function __construct(
\Psr\Log\LoggerInterface $_logger,
\Magento\Sales\Model\Order $_orderModel
) {
$this->_logger = $_logger;
$this->_orderModel = $_orderModel;
}
public function getOrders($store){
$orders = $this->_orderModel->getCollection();
$orders->join(array('item' => 'sales_order_item'), 'main_table.entity_id = item.order_id AND main_table.store_id='.$store.' ');
$orders->getSelect()->group('main_table.entity_id');
$orders->getSelect()->order('main_table.created_at DESC');
$this->_logger->info($orders); //find your query in system.log
$order_array = array();
foreach($orders as $k=>$order) {
$order_array[$k] = array(
'order_id' => $order->getId(),
'order_incremental_id' => $order->getIncrementId(),
'order_status' => $order->getStatusLabel(),
'order_date' => $order->getCreatedAt(),
'customer_name' => $order->getCustomerName()
//as your need
);
}
return json_encode($order_array);
}
答案 2 :(得分:2)
在您的阻止文件中
namespace "Your Module namespace";
class modelclass extends \Magento\Framework\View\Element\Template {
/** @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory */
protected $_orderCollectionFactory;
/** @var \Magento\Sales\Model\ResourceModel\Order\Collection */
protected $orders;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context, \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory, array $data = []
) {
$this->_orderCollectionFactory = $orderCollectionFactory;
parent::__construct($context, $data);
}
public function getOrders() {
if (!$this->orders)
{
$this->orders = $this->_orderCollectionFactory->create()->addFieldToSelect('*');
}
return $this->orders;
}
在你的phtml文件中
$_orders = $block->getOrders();
if ($_orders && count($_orders)) {
$complete = $pending = $closed = $canceled = $processing = $onHold = 0;
foreach ($_orders as $_order) {
$label = $_order->getStatusLabel();
switch ($label) {
case 'Complete' : $complete++;
break;
case 'Pending' : $pending++;
break;
case 'Processing' : $processing++;
break;
case 'Canceled' : $canceled++;
break;
case 'Closed' : $closed++;
break;
}
}
echo "Order Status <br>";
echo "Completed Order " . $complete . "<br>";
echo "Pending Order " . $pending . "<br>";
echo "Closed Order " . $closed . "<br>";
echo "Canceled Order " . $canceled . "<br>";
echo "Processing Order" . $processing . "<br>";
}
else{
echo "You have no Orders";
}
答案 3 :(得分:0)
<?php
namespace namespace\modulename\Model\Config\Source;
use Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory;
class OrderStatus implements \Magento\Framework\Option\ArrayInterface
{
protected $options = null;
protected $collectionFactory;
public function __construct(
CollectionFactory $collectionFactory
) {
$this->collectionFactory = $collectionFactory;
}
public function toOptionArray()
{
if ($this->options === null) {
$status = $this->collectionFactory->create();
foreach ($status as $stat) {
$this->options[] = [
'value' => $stat->getStatus(),
'label' => $stat->getLabel(),
];
}
}
return $this->options;
}
}
答案 4 :(得分:0)
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderDatamodel = $objectManager->get('Magento\Sales\Model\Order')->getCollection();
foreach($orderDatamodel as $orderDatamodel1){
print_r($orderDatamodel1->getData());
}
答案 5 :(得分:0)
我正在共享SQL查询,您将在其中相应地逐项获取下订单数据明细,这意味着您将通过SQL查询在Magento2中明智地获得报表项:-
选择so.increment_id
AS OrderId, 状态状态 订单状态, soi.sku AS Sku, soi.original_price AS MRP_WithTax , soi.qty_ordered AS 数量顺序 soi.price AS MRP_WTP, soi.discount_percent AS Discount_Percent, soi.discount_amount AS Discount_Amount, so.shipping_amount AS 运费, soi.price_incl_tax * soi.qty_ordered-soi.discount_amount AS RowTotal, soi.tax_percent AS Taxpercent, soi.tax_amount AS TaxAmount, sig.increment_id AS InvoiceId, sig.created_at AS发票日期 , 案件 当sig.increment_id = sig.increment_id然后“已付费” ELSE“非付费” END AS的状态,来自sales_order,因此 LEFT JOIN sales_order_address soa 开启so.shipping_address_id = soa.entity_id LEFT JOIN sales_order_item soi 开启so.entity_id = soi.order_id 左联接catalog_category_product ccp 开启soi.product_id = ccp.product_id 左联接catalog_category_entity_varchar ccev 开启ccp.category_id = ccev.entity_id 左联接sales_invoice_grid sig 在sig.order_increment_id = so.increment_id上(so.created_at在'2017-03-01'和'2019-03-01'之间)GROUP BY soi.item_id
您只需复制并粘贴SQL格式化程序中的PHPMYADMIN,然后根据日期过滤器获取数据即可。
希望对您有帮助或疑问。