Magento 2使用过滤器获取所有订单集合

在今天的文章中,我想向您展示如何使用客户,日期,状态和付款方式过滤所有订单

如何获得所有订单收集

  • 方法1:使用过滤器获取所有订单集合
  • 方法2:按客户筛选所有订单
  • 方法3:按日期获取订单收集过滤
  • 方法4:按状态获取订单收集过滤
  • 方法5:通过付款方式获取订单收集过滤器

方法1:使用过滤器获取所有订单集合

要使用过滤器获取所有订单集合,请转到以下路径Mageplaza/HelloWorld/Block/Orders.php并创建Orders.php文件。

<?php
namespace Mageplaza\HelloWorld\Block;
class Products extends \Magento\Framework\View\Element\Template
{    
  
     protected $_orderCollectionFactory;

    public function __construct(
        Magento\Framework\App\Action\Context $context,
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory 
    ) {
        $this->_orderCollectionFactory = $orderCollectionFactory;
        parent::__construct($context);

    }


   public function getOrderCollection()
   {
       $collection = $this->_orderCollectionFactory->create()
         ->addAttributeToSelect('*')
         ->addFieldToFilter($field, $condition); //Add condition if you wish
     
     return $collection;
     
    }


   public function getOrderCollectionByCustomerId($customerId)
   {
       $collection = $this->_orderCollectionFactory()->create($customerId)
         ->addFieldToSelect('*')
         ->addFieldToFilter('status',
                ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
            )
         ->setOrder(
                'created_at',
                'desc'
            );
 
     return $collection;

    }
}

方法2:按客户筛选所有订单

在像我的帐户这样的前端开发Magento 2扩展时,您可能需要在后端的网格中显示现有客户订单。下面是我们如何通过客户获得所有订单过滤器:

   public function getOrderCollectionByCustomerId($customerId)
   {
       $collection = $this->_orderCollectionFactory()->create($customerId)
         ->addFieldToSelect('*')
         ->addFieldToFilter('status',
                ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
            )
         ->setOrder(
                'created_at',
                'desc'
            );
 
     return $collection;

    }

方法3:按日期获取订单收集过滤

除了按客户获取订单收集过滤器之外,您还可以按日期获取它们。以下是我们的工作方式:

 public function getOrderCollectionByDate($from, $to)
   {
       $collection = $this->_orderCollectionFactory()->create($customerId)
         ->addFieldToSelect('*')
         ->addFieldToFilter('status',
                ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
            )
         ->setOrder(
                'created_at',
                'desc'
            );
 
     return $collection;

    }

方法4:按状态获取订单收集过滤

获取订单收集过滤器的第四种方法是按状态。

public function getOrderCollectionByStatus($statuses = [])
   {
       $collection = $this->_orderCollectionFactory()->create($customerId)
         ->addFieldToSelect('*')
         ->addFieldToFilter('status',
                ['in' => $statuses]
            )
         ;
 
     return $collection;

    }

方法5:通过付款方式获取订单收集过滤器

除了以上解决方案,以帮助您获得订单收集过滤器,还有另一种方法。以下是如何通过付款方式获取订单收集过滤器。

public function getOrderCollectionPaymentMethod($paymentMethod)
   {
       $collection = $this->_orderCollectionFactory()->create($customerId)
         ->addFieldToSelect('*')
         ->addFieldToFilter('created_at',
                ['gteq' => $from]
            )
         ->addFieldToFilter('created_at',
                ['lteq' => $to]
            );
 

     /* join with payment table */
    $collection->getSelect()
    ->join(
        ["sop" => "sales_order_payment"],
        'main_table.entity_id = sop.parent_id',
        array('method')
    )
    ->where('sop.method = ?',$paymentMethod); //E.g: ccsave

    $collection->setOrder(
        'created_at',
        'desc'
    );

     return $collection;

    }

结论

以下是获取所有订单集合的 5种简单方法,这些订单按客户,日期,状态和付款方式等不同属性进行过滤。我希望在阅读这篇文章之后,您将能够在特定情况下应用适当的方法。

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论