Magento 2 - \Magento\Продажи\Модель\Заказ попытка получить товары в заказе не работает


Я пытаюсь получить элементы заказа и отобразить его в файле phtml. Но не получаю результата.

Ниже приведен код, который я предоставил в блоке

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;
 }

И код, представленный в phtml, является

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

Но он не выводит никакого результата. Пожалуйста, помогите в том же.

Author: Teja Bhagavan Kollepara, 2017-07-04

2 answers

Вы можете попробовать использовать интерфейс заказа и получить элементы заказа,

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;
}

В файле шаблона,

$items = $block->getOrderItems();
foreach($items as $item){
    print_r($item);
}
 0
Author: Rakesh Jesadiya, 2017-07-04 05:34:25

Попробуйте использовать приведенный ниже код для вызова шаблона

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;
     }
 0
Author: Learing_Coder, 2017-07-04 05:46:35