1

I am trying to get items of an order and display it in phtml file. But not getting the result.

Below provided is the code which I have provided in block

protected $_order;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Customer\Model\Session $session,
    \Cubet\RMA\Model\ListReturnsFactory $db,
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
    \Magento\Sales\Model\Order $order
) {
    parent::__construct($context);
    $this->_session = $session;
    $this->_logger = $context->getLogger();
    $this->_db = $db;
    $this->_orderCollectionFactory = $orderCollectionFactory;
    $this->_order = $order;
}
 public function getOrderItems()
  { 
    $orderItems = $this->_order->create()->loadByIncrementId(000000003);
    $orderItems->getAllItems();
    return $orderItems;
 }

and the code provided in phtml is

$items = $block->getOrderItems();
print_r($items);

but it doesn't print any result. Please help on the same.

  • Try using a string as the Increment ID: $orderItems = $this->_order->create()->loadByIncrementId('000000003'); – Danny Nimmo Jul 4 '17 at 5:33
0

You can try with orderInterface and get items of order,

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Sales\Api\Data\OrderInterface $order,
    array $data = []
) {
    parent::__construct($context, $data);
    $this->order = $order;
}
public function getOrderItems(){
    $order = $this->order->loadByIncrementId(000000003);

    $items = $order->getAllItems();
    $product = array();
    foreach($items as $item){
        $product[] = $item->getName().'||'.$item->getQtyordered();
    }
    return $product;
}

in template file,

$items = $block->getOrderItems();
foreach($items as $item){
    print_r($item);
}
  • You can accept as solutions so other users have useful. – Rakesh Jesadiya Jul 4 '17 at 6:16
  • Rakesh, could you tell me how I can pass multiple increment id within loadByIncrementId()? – Bridgit Thomas Jul 4 '17 at 6:21
  • you can pass using foreach loop pass $block->getOrderItems($id) in template file and in block getOrderItems($id). get order data like, $order = $this->order->loadByIncrementId($id); – Rakesh Jesadiya Jul 4 '17 at 6:23
0

Try below Code to call in template

protected $_orderCollectionFactory;
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\Session $session,
        \Cubet\RMA\Model\ListReturnsFactory $db,
       \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
        \Magento\Sales\Model\Order $order
    ) {
        parent::__construct($context);
        $this->_session = $session;
        $this->_logger = $context->getLogger();
        $this->_db = $db;
        $this->_orderCollectionFactory = $orderCollectionFactory;
    }
     public function getOrders() {        
            $this->orders = $this->_orderCollectionFactory->create()->addFieldToSelect('*');
            return $this->orders;
     }

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.