2

I want to get the Ordered quantity for each product for that order When action is being performed on any order. How can I achieve this.

This is code:

<?php

namespace Custom\Confirm\Controller\Adminhtml\Order;

use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Custom\Confirm\Model\ResourceModel\Saleslist\CollectionFactory;

/**
 * Class Payseller.
 */
class MassConfirm extends \Magento\Backend\App\Action
{
     /**
     * @var \Magento\Sales\Api\OrderRepositoryInterface
     */
    protected $_orderRepository;

    /**
     * @var \Magento\Sales\Model\Service\InvoiceService
     */
    protected $_invoiceService;

    /**
     * @var \Magento\Framework\DB\Transaction
     */
    protected $_transaction;

    protected $_order;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Sales\Model\Service\InvoiceService $invoiceService,
        \Magento\Sales\Model\Order $orderInfo,
        \Magento\Framework\DB\Transaction $transaction
    ) {
        $this->_orderRepository = $orderRepository;
        $this->_order = $orderInfo;
        $this->_invoiceService = $invoiceService;
        $this->_transaction = $transaction;
        parent::__construct($context);
    }

    /**
     * Execute action.
     *
     * @return \Magento\Backend\Model\View\Result\Redirect
     *
     * @throws \Magento\Framework\Exception\LocalizedException|\Exception
     */

    /**
     * Get seller for the product purchased.
     *
     * Nitin Pawar 9/11/16.
     */ 
    protected function _getSellersForProduct($pid)
    {   
        $collection = $this->_objectManager->create(
                'Webkul\Marketplace\Model\Product'
            )->getCollection()
            ->addFieldToFilter(
                'mageproduct_id', 
                $pid
            );

            $count = $collection->getData();
            return $count; 

    } 

    public function execute()
    {
            /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();

        $wholedata = $this->getRequest()->getPost();

        foreach ($wholedata['selected'] as $orderId){

         $order = $this->_orderRepository->get($orderId);
                  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $order_ = $objectManager->create('\Magento\Sales\Model\Order')
                                           ->load($orderId); 

                 $orderItems = $order_->getAllItems();
            foreach ($orderItems as $item) {
                    $proID = $item->getProductId();
                    $itemSku = $item->getSku();
                    echo '<br>product id: '.$proID;
                    echo '<br>product sku: '.$itemSku;
                    $sellers = $this->_getSellersForProduct($proID);
                    var_dump($sellers);
                    foreach($sellers as $seller){
                        var_dump($seller); echo '<br>';
                    }
            }
                 exit;
                $order_->setState("processing")->setStatus("processing");

                $order_->save();  

        }   
        $this->messageManager->addSuccess(__('The order has been confirm. '));
        return $resultRedirect->setPath('sales/order/index');
    }

}

Here i can get the order id and product id, and i want to display the each product ordered quantity in the cart for that order.

Thanks in advance.

1

you can get Order Quantity from $item

        $orderItems = $order_->getAllItems();
        foreach ($orderItems as $item) {
           $itemQty = $item->getQtyOrdered();
        }  
  • Thanks for your answer, will try this and get back to you. – Nitin Pawar Nov 9 '16 at 10:21
  • @NitinPawar i updated the answer kindly look at that. – Fme Extensions Nov 9 '16 at 10:28
  • $itemQty = $item->getQtyOrdered(); is working where as $itemQty = $item->getQty(); gives the empty result. Thanks again. – Nitin Pawar Nov 9 '16 at 10:34

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.