0

To get order collection by customer wise i.e.., customer A and customer B have placed order and it should retrieve by customer wise per collection?

public function getOrdersByCustomer() {  

    $collect = $this->_orderCollectionFactory->create()->addFieldToSelect('*');

    $collection = $collect->getSelect()->group('customer_id');

    return $collection;
}

The above returns null.Thanks in advance

4
  • 1
    You want to get customer's order data? Commented Jan 4, 2019 at 8:53
  • No, I want to fetch the order collection group by customer_id. Hope you understand the question? Commented Jan 4, 2019 at 9:00
  • 1
    Please share full code .Seems code is right
    – Amit Bera
    Commented Jan 4, 2019 at 9:08
  • That's it from block and retrieving on phtml $_ordersbycustomer = $block->getOrdersByCustomer(); print_r($_ordersbycustomer->getData()); exit(); Commented Jan 4, 2019 at 9:11

1 Answer 1

1

Try using these cods:

 public function getOrders()
    {
        if (!($customerId = $this->_customerSession->getCustomerId())) {
            return false;
        }
        if (!$this->orders) {
            $this->orders = $this->getOrderCollectionFactory()->create($customerId)->addFieldToSelect(
                '*'
            )->addFieldToFilter(
                'status',
                ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
            )->setOrder(
                'created_at',
                'desc'
            );
        }
        return $this->orders;
    }
1
  • thanks for the answer I'll try it out and let you know. Commented Jan 7, 2019 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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