Şu yazıyı okuyorsunuz: Sipariş Yerine Faturada Magento 2 Stok Envanteri Nasıl Azaltılır
ad alanı Sağlayıcı\Uzantı\Observer;
Magento\Framework\Event\ObserverInterface’i kullanın;
Magento\Framework\Event\Observer’ı kullanın;
Magento\CatalogInventory\Api\StockRegistryInterface’i kullanın;
Magento\Catalog\Model\ProductRepository’yi kullanın;
UpdateInventory sınıfı ObserverInterface’i uygular
{
$productRepository korumalı;
$stockRegistry korumalı;
genel işlev __construct(ProductRepository $productRepository,
StockRegistryInterface $stockRegistry)
{
$this->productRepository = $productRepository;
$this->stockRegistry = $stockRegistry;
}
genel işlev çalıştırması(Gözlemci $gözlemci)
{
girişim
{
$fatura = $observer->getEvent()->getInvoice();
$invoiceItems = $invoice->getAllItems();
foreach ($invoiceItems as $item)
{
$productId = $item->getProductId();
$product = $this->productRepository->getById($productId);
$sku = $ürün->getSku();
$stockItem = $this->stockRegistry->getStockItemBySku($sku);
$miktar = $stokta bulunan öğe->getQty() – $item->getQty();
$stockitem->setQty($qty);
$iteminstock->setIsInStock((bool)$qty);
$this->stockRegistry->updateStockItemBySku($sku, $stockItem);
}
}
yakala (\İstisna $e)
{
return $e->getMessage();
}
}
}