Magento2 如何在magento 2中获取订单集合对象

Magento2 如何在magento 2中获取订单集合对象,magento2,Magento2,我是magento2的初学者,我将尝试检索所有与订单相关的内容 信息,这些信息只想显示在管理端 如订单Id、订单状态、门店名称、订单日期、, 付款方式等。 在这里,我尝试获取订单信息,如订单id、订单状态,但 页面将重定向到主页/仪表板 我使用product Collection对象获取产品相关信息,但我如何获取所有订单信息 我不知道如何使用订单收集获取订单信息 对象或订单存储库对象,并显示该订单信息 在magento2的管理端 这是我的视图或info.phtml文件是: <?php

我是magento2的初学者,我将尝试检索所有与订单相关的内容 信息,这些信息只想显示在管理端 如订单Id、订单状态、门店名称、订单日期、, 付款方式等。 在这里,我尝试获取订单信息,如订单id、订单状态,但 页面将重定向到主页/仪表板

我使用product 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();    ?>
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');    }    }

你可以通过下面的代码

<?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;
}       
}

获取所有订单信息

  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);
 }
在块文件中

在您的phtml文件中

$\u orders=$block->getOrders();
如果($订单和计数($订单)){
$complete=$pending=$closed=$cancelled=$processing=$onHold=0;
foreach($\订单为$\订单){
$label=$\订单->获取状态标签();
交换机($标签){
案例“Complete”:$Complete++;
打破
案例“未决”:$Pending++;
打破
案例“处理”:$Processing++;
打破
案例“已取消”:$Cancelled++;
打破
案例“已关闭”:$Closed++;
打破
}
}
回显“订单状态
”; 回显“已完成订单”。$complete。“
”; 回显“待定订单”。$Pending。“
”; 回显“已关闭订单”。$Closed。“
”; 回显“已取消订单”。$Canceled。“
”; 回显“处理订单”。$Processing。“
”; } 否则{ 回声“你没有命令”; }
我正在共享SQL查询,您将在其中以项目方式获得下单数据详细信息,这意味着您将通过SQL查询在Magento2中以项目方式获得报告:-

选择so.increment\u id
作为OrderId, 地位 订单状态, soi.sku作为sku, 原价为MRP(含税) , soi.QUOTE\U订购数量为 订单数量, soi.价格为MRP_WTP, soi.折扣率 百分之十的折扣, soi.折扣金额为 折扣金额, 因此,装运金额为 出货量, soi.含税价格*订购soi.数量-soi.折扣金额总计, soi.tax_%作为tax百分比, soi.税款金额作为税款金额, sig.increment_id作为发票id, 在发票日期创建的签名 , 案例 当sig.increment\u id=sig.increment\u id时,则为“已付” 其他“非援助” 销售订单so的结束状态 左连接销售\订单\地址soa ON so.shipping\u address\u id=soa.entity\u id 左连接销售\订单\项目soi ON so.entity_id=soi.order_id 左连接目录\类别\产品ccp 在soi.product_id=ccp.product_id上 左连接目录\类别\实体\ varchar ccev 在ccp.category\u id=ccev.entity\u id上 左连接销售\发票\网格标志 在sig.order_increment_id=so.increment_id上,其中(so.created_位于'2017-03-01'和'2019-03-01'之间)由 soi.item_id

您只需在SQL formatter中复制并粘贴PHPMYADMIN,即可根据日期过滤器获取数据


我希望这会有所帮助,如果您有任何问题或疑问,请告诉我。

Hi,@taps Thnks获取您的答复,但它对我无效。给出了错误。谢谢您的回答。这是可行的,但我找到了早期的解决方案,如下所示getSalesOrderCollection-function中缺少foreach。使用此代码在模块中加载销售订单集合。结果将显示在Magento2i中的store->config中。最好添加一些说明,而不仅仅是代码片段,所以其他人可以理解。这不再适用于M2.2。它给出错误“uncaughterror:调用null上的成员函数create()”
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;
}
$_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";
}
<?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;
    }
}
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
$orderDatamodel = $objectManager->get('Magento\Sales\Model\Order')->getCollection();
foreach($orderDatamodel as $orderDatamodel1){
print_r($orderDatamodel1->getData());

}