When customer tried to place an order , In my website i have only one Qty for each SKU. When two different customers tried to place an order on same time for same SKU one customer got an order number and other customer getting no order number bcz of qty unavailable. I am using magento 2.2.4 version.

How can i check the qty available or not before customer clicks on pay now button in magento page?

I have so many incidents and customer & me spending time to give returns for them as their order is not booked. Again i am loosing money from payment gateway. Is there any solution for this? I tried this observer but not worked.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">  
 <event name="sales_order_place_before">
    <observer name="my_order_status_check" instance="My\Checkout\Observer\CheckPosBeforePlaceOrder" shared="false" />

I tried below code with

 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.

namespace My\TestDiscount\Model\Quote;

use Magento\CatalogInventory\Model\Stock\StockItemRepository as StockItem;
use Magento\Catalog\Model\ProductRepository as Product;

class Discount extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
     * Discount calculation object
     * @var \Magento\SalesRule\Model\Validator
    protected $calculator;

     * Core event manager proxy
     * @var \Magento\Framework\Event\ManagerInterface
    protected $eventManager = null;

     * @var \Magento\Store\Model\StoreManagerInterface
    protected $storeManager;

     * @var \Magento\Framework\Pricing\PriceCurrencyInterface
    protected $priceCurrency;
protected $stockItem;

protected $product;
     * @param \Magento\Framework\Event\ManagerInterface $eventManager
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\SalesRule\Model\Validator $validator
     * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
    public function __construct(
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\SalesRule\Model\Validator $validator,
        \Magento\SalesRule\Model\Coupon $coupon,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\SalesRule\Model\Rule $saleRule,
        StockItem $stockItem,
        Product $product,
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
    ) {
        $this->eventManager = $eventManager;
        $this->calculator = $validator;
        $this->storeManager = $storeManager;
        $this->messageManager = $messageManager;
        $this->priceCurrency = $priceCurrency;
        $this->coupon = $coupon;
         $this->stockItem = $stockItem;
        $this->product = $product;
        $this->saleRule = $saleRule;

     * Collect address discount amount
     * @param \Magento\Quote\Model\Quote $quote
     * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
     * @param \Magento\Quote\Model\Quote\Address\Total $total
     * @return $this
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
    public function collect(
        \Magento\Quote\Model\Quote $quote,
        \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
        \Magento\Quote\Model\Quote\Address\Total $total
    ) {
        parent::collect($quote, $shippingAssignment, $total);
        $shippingitems = $shippingAssignment->getItems();
        if (!count($shippingitems)) {
            return $this;

        $quoteitems = $quote->getAllItems();

       // $logger->info(' '.count($quoteitems));
         foreach ($quoteitems as $item) {


    $productQty = $this->stockItem->get($item->getProductId())->getQty();

                $logger->info('stock details'.json_encode($productId->getQty()));



    }catch(Exception $ex){
        return $this;

  • have you tried checkout_cart_product_add_after
    – Ghulam.M
    Commented Nov 27, 2020 at 14:03
  • Yes , tried this one as well but it always showing 1 qty if i update in magento2 admin as 0 in inventory.
    – Dinesh
    Commented Nov 28, 2020 at 15:04
  • I updated my question with code that i am trying with checkout_cart_product_add_after , it is always return stock item as 1.
    – Dinesh
    Commented Nov 28, 2020 at 15:09
  • How are you updating the stock quantity, I mean when you place an order does your stock get updated from 1 to 0
    – Ghulam.M
    Commented Nov 30, 2020 at 6:10
  • Yes..when order is successful qty is updating finely.
    – Dinesh
    Commented Nov 30, 2020 at 6:18


Your Answer

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

Browse other questions tagged or ask your own question.