2

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.

  • Could you paste your code here? This is only going to work if the action is executed by the same admin user. At the \Magento\Backend\Model\Session\Quote session, you would be able to get the quote calling the method getQuote. – Thiago Lima Feb 11 '18 at 14:25
2

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 />";   
        }
    }
1

You can use the \Magento\Sales\Model\AdminOrder\Create . I have used this class to get the added product in cart.

In my case i need to restrict the place order based on the product items in cart.To achieve this i have created a custom Block and checking the items in cart.

Step 1 :Added My custom Block in sales_order_create_index.xml

     <referenceContainer name="content">
          <block class="[Package]\[Namespace]\Block\Adminhtml\Cart\CustomBlock"  template="Package_NameSpace::[template_Name]"/>
     </referenceContainer>

Step 2 : Created a Block name as "CustomBlock.php"

     namespace [Package]\[Namespace]\Block\Adminhtml\Cart;

     use Magento\Backend\Block\Template\Context;
     use Magento\Sales\Model\AdminOrder\Create  as OrderCreate;

 class CustomBlock extends \Magento\Backend\Block\Template
 {
       /** @var  OrderCreate */
       protected $_orderCreate;

       /**
        * CustomBlock constructor.
        * @param Context     $context
        * @param OrderCreate $orderCreate
        * @param array       $data
        */
       public function __construct(
        Context $context,
        OrderCreate $orderCreate,
        array $data = []
      )
     {
        $this->_orderCreate = $orderCreate;
        parent::__construct($context, $data);
     }

   /**
    * Get Items Details 
    */
   public function getItemsDetails()
   {
      $quoteData =  $this->_orderCreate->getQuote();
      foreach ($quoteData->getAllItems() as $item) {
         echo "Product Sku".$item->getSku();
         // Insert Your code here 
      }

  }
}

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.