I'm using Magento 2.4.3-p1 and I'm trying to decrease stock quantity with custom observer while placing an order, but salable quantity is decreasing twice, my events and observer file are this:
etc/events.xml
<?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_after">
<observer name="update_qty_with_saleable_qty" instance="Vendor\Module\Observer\AfterPlaceOrderUpdateQty" />
</event>
</config>
Vendor\Module\Observer\AfterPlaceOrderUpdateQty
<?php
declare(strict_types=1);
namespace Vendor\Module\Observer;
use Magento\Catalog\Model\Product as Product;
use Magento\CatalogInventory\Model\Stock\StockItemRepository;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Validation\ValidationException;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\SourceItemsSaveInterface;
/**
* After Place Order Update Stock Quantity and Salable Quantity
*/
class AfterPlaceOrderUpdateQty implements ObserverInterface
{
/**
* @var SourceItemInterface
*/
protected SourceItemInterface $sourceItemInterface;
/**
* @var SourceItemsSaveInterface
*/
protected SourceItemsSaveInterface $sourceItemsSave;
/**
* @var Product
*/
protected Product $product;
/**
* @var StockItemRepository
*/
protected StockItemRepository $stockRepository;
/**
* @param SourceItemsSaveInterface $sourceItemsSave
* @param SourceItemInterface $sourceItemInterface
* @param StockItemRepository $stockRepository
* @param Product $product
*/
public function __construct(
SourceItemsSaveInterface $sourceItemsSave,
SourceItemInterface $sourceItemInterface,
StockItemRepository $stockRepository,
Product $product
) {
$this->sourceItemsSave = $sourceItemsSave;
$this->sourceItemInterface = $sourceItemInterface;
$this->product = $product;
$this->stockRepository = $stockRepository;
}
/**
* @param $sku
* @param $qty
* @return AfterPlaceOrderUpdateQty
* @throws NoSuchEntityException
* @throws CouldNotSaveException
* @throws InputException
* @throws ValidationException
*/
public function setQtyToProduct($sku, $qty): AfterPlaceOrderUpdateQty
{
$product = $this->product->getIdBySku($sku);
$productStock = $this->stockRepository->get($product);
$productQty = $productStock->getQty();
$totalQty = $productQty - $qty;
$this->sourceItemInterface->setSku($sku);
$this->sourceItemInterface->setQuantity($totalQty);
$this->sourceItemInterface->setStatus(1);
$this->sourceItemInterface->setSourceCode('default');
$this->sourceItemsSave->execute([$this->sourceItemInterface]);
return $this;
}
/**
* @param Observer $observer
* @return void
* @throws NoSuchEntityException
* @throws CouldNotSaveException
* @throws InputException
* @throws ValidationException
*/
public function execute(Observer $observer)
{
$order = $observer->getEvent()->getOrder();
foreach ($order->getAllVisibleItems() as $item) {
$productSku = $item->getSku();
$productQty = $item->getQtyOrdered();
$this->setQtyToProduct($productSku, $productQty);
}
}
}