Magento – How to get admin order items from admin order quote

magento2

I'm trying to get the items which are present in quote of admin order.

\Magento\Backend\Model\Session\Quote $adminQuote
\Magento\Sales\Model\AdminOrder\Create $adminCreate

to get the details but whenever i call call getItems, getAllvisibleItems, getAllItems its returning null value.

I need to get the item details which are under quote when you are placing an order from admin.
Can any one let me know how to do that.

Best Answer

You should use the \Magento\Backend\Model\Session\Quote to get the item details which are under quote when you are placing an order from admin.

    use \Magento\Backend\Model\Session\Quote;

    public function __construct( 
            ... 
            \Magento\Backend\Model\Session\Quote $backendQuoteSession 
            ... 
        ) { 
            ... 
            $this->backendQuoteSession = $backendQuoteSession; 
            ... 

        }

    public function yourFunction() { 
       $items = $this->backendQuoteSession->getQuote()->getAllVisibleItems();

        foreach ($items as $item) { 
        { 
            echo 'ID: '.$item->getProductId().'<br />';
            echo 'Name: '.$item->getName().'<br />';
            echo 'Sku: '.$item->getSku().'<br />';
            echo 'Quantity: '.$item->getQty().'<br />';
            echo 'Price: '.$item->getPrice().'<br />';
            echo "<br />";   
        }
    }
Related Topic