0

I have successfully got the order details and below is my code to get the details. Now, I need to get the ordered item details. Please guide

$collection = $this->_orderCollectionFactory->create()
            ->addAttributeToSelect('*');
2

3 Answers 3

3

Try this code

protected $_orderCollectionFactory;
protected $orderRepository;

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

public function getAllOrderProducts()
{
    $orderCollection = $this->_orderCollectionFactory->create()->addAttributeToSelect('*');
    $orderData = $orderCollection->getData();
    if (count($orderCollection->getData())) {
        if (isset($orderData[0])) {
            foreach ($orderData as $attribute) {
                $order_id = $attribute['entity_id'];
                $order_data = $this->orderRepository->get($order_id);
                if (!empty($order_data)) {
                    foreach ($order_data->getAllItems() as $value) {
                        print_r($value->debug());
                    }
                }
            }
        } else {
            $order_id = $orderData['entity_id'];
            $order_data = $this->orderRepository->get($order_id);
            if (!empty($order_data)) {
                foreach ($order_data->getAllItems() as $value) {
                    print_r($value->debug());
                }
            }
        }
    }
}
1

You can use this below code for get ordered item details :

$collection = $this->orderCollectionFactory->create()->addAttributeToSelect('*'); // It returns all order collection
        foreach ($collection as $key => $value) {
            foreach ($value->getAllItems() as $key1 => $items) {
                echo $items->getName() . "<br/>";
                echo $items->getPrice() . "<br/>";
                echo $items->getSku() . "<br/>";
            }
        }
0
foreach ($collection as $order) {
            foreach ($order->getAllVisibleItems() as $item) {
                echo $item->getName();
                echo $item->getQtyOrdered();
                ..............
                ..............
            }

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.