4

In Product we have Qty. Once we Order product & order is completed.

In sales_order_item table we have qty_backordered, qty_canceled, qty_invoiced, qty_ordered, qty_refunded, qty_shipped

Once order is completed majorly we need to check invoiced & shipped qty.

If i have X Product: It's original qty is 100, Ordered qty & shipped to 5. So total remaining qty is 95 out of 100.

How to get 100 using code? So we can get that out of 100 now 95 is remaining.

[UPDATE]

namespace Custom\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\ObjectManager;

class OrderPlaceAfter implements ObserverInterface {

    /** @var \Magento\Framework\Logger\Monolog */
    protected $logger;
    protected $resources;
    protected $orderRepository;
    protected $stockItemRepository;

    public function __construct(\Psr\Log\LoggerInterface $logger, \Magento\Framework\App\ResourceConnection $resources, \Magento\Sales\Api\OrderRepositoryInterface $orderRepository, \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository) {
        $this->logger = $logger;
        $this->resources = $resources;
        $this->orderRepository = $orderRepository;
        $this->stockItemRepository = $stockItemRepository;
    }

    public function execute(Observer $observer) {

        $orderId = 110; // My Order Number

        $order = $this->orderRepository->get($orderId);

        if ($order->getEntityId()) {
            $items = $order->getItems();
            foreach ($items as $item) {
                $stock = $this->stockItemRepository->get($item->getProductId());
                echo $stock->getQty().'-'.$item->getQtyShipped();
                //echo $remainingShipQty = $stock->getQty() - $item->getQtyShipped();
            }
        }
          exit;

      return $this;
    }
}
5
  • Which event, you try to observe?
    – Sohel Rana
    Jan 31, 2017 at 9:46
  • controller_action_predispatch_checkout_cart_add
    – Jackson
    Jan 31, 2017 at 9:47
  • Stock Item with id "XXX" does not exist. giving this error
    – Jackson
    Jan 31, 2017 at 9:54
  • Check updated answer.
    – Sohel Rana
    Jan 31, 2017 at 10:08
  • Need to use this \Magento\CatalogInventory\Api\StockRegistryInterface $StockRegistryInterface $stockItem = $this->stockItemRepository->getStockItemBySku($item->getSku()); if ($stockItem) { $stockItem->getQty(); }
    – Jackson
    Jan 31, 2017 at 10:26

1 Answer 1

1

Try following way:

public function __construct(
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
    \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository
) {
    $this->orderRepository = $orderRepository;
    $this->stockItemRepository =$stockItemRepository;
}

And your calculation should be:

$order = $this->orderRepository->get(13);
if($order->getEntityId()) {
    $items = $order->getItems();
    foreach ($items as $item) {
        $stock = $this->stockItemRepository->get($item->getProductId());
        $remainingShipQty = $stock->getQty()-$item->getQtyShipped();
    }
}

[Update]

You need to log data into file. Echo not working for observer.

namespace Custom\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\ObjectManager;

class OrderPlaceAfter implements ObserverInterface 
{
    /** @var \Magento\Framework\Logger\Monolog */
    protected $logger;
    protected $resources;
    protected $orderRepository;
    protected $stockItemRepository;

    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\App\ResourceConnection $resources,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository
    ) {
        $this->logger = $logger;
        $this->resources = $resources;
        $this->orderRepository = $orderRepository;
        $this->stockItemRepository = $stockItemRepository;
    }

    public function execute(Observer $observer)
    {

        $orderId = 110; // My Order Number

        $order = $this->orderRepository->get($orderId);

        if ($order->getEntityId()) {
            $items = $order->getItems();
            foreach ($items as $item) {
                $stock = $this->stockItemRepository->get($item->getProductId());
                //echo $stock->getQty().'-'.$item->getQtyShipped();
                $remainingShipQty = $stock->getQty() - $item->getQtyShipped();
                $this->logger->info($remainingShipQty);
            }
        }
        return $this;
    }
}
11
  • magento\vendor\magento\module-reports\Model\ResourceModel\Product\Sold\Collection.php they are only dealing with qty_ordered
    – Jackson
    Jan 31, 2017 at 6:20
  • qty_ordered means how many item you order, But you need product stock qty that's why here pick product stock qty.
    – Sohel Rana
    Jan 31, 2017 at 6:28
  • If you want only how many qty order - how many ship, then no need to load product stock. Simply $remainingShipQty = $item->getQtyOrdered()-$item->getQtyShipped();
    – Sohel Rana
    Jan 31, 2017 at 6:30
  • $stock->getQty() not giving any value
    – Jackson
    Jan 31, 2017 at 9:11
  • Strange! It's tested code. $stock is an \Magento\CatalogInventory\Api\Data\StockItemInterface object.
    – Sohel Rana
    Jan 31, 2017 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.