1

Here this is the Magento 1 code would like to convert to M2 code.

$customerId = 1;
$orderCollection = Mage::getResourceModel('sales/order_collection')
        ->addFieldToSelect('*')
        ->addFieldToFilter('customer_id', $customerId)
        ->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()))
        ->setOrder('created_at', 'desc');

Here mainly need to know how to add ->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates())) statement in Magento2

Thanks

2

You can find the reference in this class Magento\Sales\Block\Order\History

class Orders
{
    protected $_customerSession;

    protected $_orderConfig;

    private $_orderCollectionFactory;

    public function __construct(
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $_orderCollectionFactory,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Sales\Model\Order\Config $orderConfig,
        array $data = []
    ) {
         $this->_orderCollectionFactory = $_orderCollectionFactory;
        $this->_customerSession = $customerSession;
        $this->_orderConfig = $orderConfig;
    }

   public function getOrders()
   {
    $orders = null;
        if (!($customerId = $this->_customerSession->getCustomerId())) {
            return false;
        }

    $orders = $this->_orderCollectionFactory->create()
            ->addAttributeToSelect('*');
        $orders->addFieldToFilter("customer_id", $customerId);
        $orders->addFieldToSelect(
                '*'
        )->addFieldToFilter(
            'status',
            ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
        )->setOrder(
           'created_at',
           'desc'
        );
        return $orders;
    }
}
1

You can try this one:

in constructor:

protected $orderCollectionFactory;
public function __construct(
...
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
...
){
    $this->orderCollectionFactory = $orderCollectionFactory;
}

Then in any of your function you can use this:

$collection = $this->orderCollectionFactory->create()
            ->addAttributeToSelect('*');
$collection->addFieldToFilter("customer_id", $customerId);

where $this->orderCollectionFactory is the instance of \Magento\Sales\Model\ResourceModel\Order\CollectionFactory

You can add more filters to it

  • thanks @shoaibmunir would like to know >addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates())) – Nagaraju K Dec 2 '19 at 8:04
  • getVisibleOnFrontStates is used for the comments that are only visible on frontend. Like you can add multiple comments on admin order but it wont visible on frontend, but when you check "Visible on Frontend" before adding comments to order, then it would visible on frontend – Shoaib Munir Dec 2 '19 at 8:06
  • You don't need to add this filter in order collection, state of order is somethings else. and visible_on_frontend is only for comments – Shoaib Munir Dec 2 '19 at 8:07
1

You can try once below code,

protected $_orderCollectionFactory;
protected $orders;
protected $_salesConfig;
public function __construct(
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory 
$orderCollectionFactory,
        \Magento\Sales\Model\Order\Config $salesConfig
    ) {
   $this->_orderCollectionFactory = $orderCollectionFactory;
   $this->_salesConfig = $salesConfig;
   parent::__construct($context, $data);
}


 public function getOrderCollection()
 {

if (!$this->orders) {
    $this->orders = $this->_orderCollectionFactory->create()->addFieldToSelect(
        '*'
    )->addFieldToFilter(
        'customer_id',
        $customerId
    )->addFieldToFilter('state', array('in' => $this->_salesConfig->getVisibleOnFrontStatuses()))
    ->setOrder(
        'created_at',
        'desc'
     );
 }
  return $this->orders;

 }

Hope this will help in your case!!

  • thanks let me check – Nagaraju K Dec 2 '19 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.