3

I need All order Product Items in User My account section.

How I can get All order items with selected options.

enter image description here

  • Get the ordered item collection of user, Filter the ordered item collection. Use ID's for Fetching the product details in displaying item in grid. – Mandar Parbate Dec 18 '17 at 5:35
  • can you post your answer in code so I can give you +1 point. – Magento2 Devloper Dec 18 '17 at 5:37
0

This can help you out

Create a custom block and add this code :

<?php
namespace Company\Slider\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
    protected $_orderModel;   
    protected $_orderCollectionFactory;
    protected $orders;


    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Sales\Model\Order $orderModel,
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
        array $data = []
        )
        {       
            $this->_orderModel = $orderModel;
            $this->_orderCollectionFactory = $orderCollectionFactory;
            parent::__construct($context, $data);
        }

        public function getOrderItems($orderId) 
        {
            $order = $this->_orderModel->load($orderId);
            return $order->getAllVisibleItems();        
        }

    public function getUserId()
    {   
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSession = $objectManager->create('Magento\Customer\Model\Session');
        if($customerSession->isLoggedIn()) { 
            return $customerSession->getCustomer()->getId();
        }
    }

    public function getOrders($customerId)
    {

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

Create a template file and put code in it

<?php
$customerID = $block->getUserId();
$orderCollection = $block->getOrders($customerID);
foreach($orderCollection as $order){    
    $items = $block->getOrderItems($order->getId());     
    foreach ($items as $item) {     
        $options = $item->getProductOptions();        

        if (isset($options['options']) && !empty($options['options'])) {        
        foreach ($options['options'] as $option) {
            echo 'Title: ' . $option['label'] . '<br />';
            echo 'ID: ' . $option['option_id'] . '<br />';
            echo 'Type: ' . $option['option_type'] . '<br />';
            echo 'Value: ' . $option['option_value'] . '<br />' . '<br />';
        }
        }
    }
}

This code work for me, hope so works for you too.

For reference check the links

http://blog.chapagain.com.np/magento-2-get-products-custom-option-value-from-cart-order/

http://blog.chapagain.com.np/magento-2-load-product-by-id-and-sku/

Sorry for late replay

Thanks

  • you can check the product details also use this code : echo "<pre>";print_r($item->getData()) – Mandar Parbate Dec 18 '17 at 10:15
  • two order config product but same options don't skip any idea? – Magento2 Devloper Dec 18 '17 at 10:54
  • can you elaborate your ques? – Mandar Parbate Dec 18 '17 at 11:09
  • Ex- Order -111 in one Simple product Custom option- Color - red, another one Custom option Color - green so no need to skip but same option selection need to skip. – Magento2 Devloper Dec 18 '17 at 12:05
  • you need to customize the code for that – Mandar Parbate Dec 21 '17 at 10:29

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.