Observer when updating products from Checkout

    <event name="checkout_cart_update_items_after">
        <observer name="Cartupdate" instance="JJ\Backorder\Observer\Cartupdate" />
    </event>

Using this observer we can monitor the update or trigger any conditions on updating the cart
here we’re setting the backorders on update

<?php
namespace JJ\Backorder\Observer;

use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\Framework\Event\ObserverInterface;
use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;
use Magento\Quote\Api\CartItemRepositoryInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product\Type\PriceFactory;
use Magento\Customer\Model\Session;

class Cartupdate implements ObserverInterface
{
    protected $getProductSalableQty;
    protected $stockRegistry;
    protected $cartItemRepository;
    protected $quoteRepository;

    public function __construct(
        GetProductSalableQtyInterface $getProductSalableQty,
        StockRegistryInterface $stockRegistry,
        CartItemRepositoryInterface $cartItemRepository,
        CartRepositoryInterface $quoteRepository,
        ProductRepositoryInterface $productRepository,
        PriceFactory $priceModelFactory,
        Session $customerSession
    ) {
        $this->getProductSalableQty = $getProductSalableQty;
        $this->stockRegistry = $stockRegistry;
        $this->cartItemRepository = $cartItemRepository;
        $this->quoteRepository = $quoteRepository;
        $this->productRepository = $productRepository;
        $this->priceModelFactory = $priceModelFactory;
        $this->customerSession = $customerSession;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/backordercartupdate.log');
        $logger = new \Zend_Log();
        $logger->addWriter($writer);
        //get the items just added to cart
        $items = $observer->getCart()->getQuote()->getAllItems();
        $cartId = $observer->getCart()->getQuote()->getId();
        $quote = $observer->getCart()->getQuote();
        $logger->info("cart_id" . $cartId);
        $listtotal = 0;
        foreach ($items as $item) {
            $productId = $item->getProductId();
            $product = $this->productRepository->getById($productId);
            $logger->info("ID:" . $productId);
            $sku = $item->getSku();
            $logger->info("Sku:" . $sku);
            $qty = $item->getQty();
            $logger->info("Updating qty:" . $qty);
            $stockItem = $this->stockRegistry->getStockItemBySku($sku);
            $defaultStockQty = $stockItem->getQty();
            $salableQty = $this->getProductSalableQty->execute($sku, 2);   // Calling '2' as the id of the stock to get the stock collection from Netsuite ($stockItem->getStockId()));
            $item = ($item->getParentItem() ? $item->getParentItem() : $item);
            $stockItem = $this->stockRegistry->getStockItemBySku($item->getSku());
            $defaultQty = $stockItem->getQty();
            $reducedQty = $defaultQty - $qty;
            $this->getProductSalableQty->execute($item->getSku(), $stockItem->getStockId(), $reducedQty);
            $logger->info("saleable" . $salableQty);
            $customerGroupId = $this->customerSession->getCustomerGroupId();
            $priceModel = $this->priceModelFactory->create();
            $productPrice = $priceModel->getFinalPrice($customerGroupId, $product);
            $item->setStockQty($qty);
//        Checking if there is salable Qty for the product
            if ($salableQty <= 0) {
                $backorder = - $item->getQty();
            } else {
                $backorder = $salableQty - $item->getQty();
            }
            $logger->info($backorder . "backorder");
//        Calculating the Backorder total price and qty
            if ($backorder < 0) {
                $qty = $item->getQty() - abs($backorder);
//             Checks if the product has no qty to order and sets the price to zero
                if ($qty == 0) {
                    $price = 0;
                    $item->setQty(1);
                    $item->setStockQty($qty);
                    $item->setCustomPrice($price);
                    $item->setOriginalCustomPrice($price);
                    $item->setBackorderQty($backorder);
                    $backordertotal = $productPrice * ($item->getBackorderQty());
                    $item->setBackorderTotal($backordertotal);
                } else {
                    $price = $item->getPrice() * $qty;
                    $backordertotal = $item->getPrice() * ($backorder);
                    $item->setBackorderQty($backorder);
                    $item->setBackorderTotal($backordertotal);
                    $item->setQty($qty);
                    $item->setStockQty($qty);
                }
                $item->getProduct()->setIsSuperMode(true);
            } else {
//              Condition for the In-Stock products
                $item->setBackorderQty(0);
                $backordertotal = $productPrice * ($item->getBackorderQty());
                $item->setBackorderTotal($backordertotal);
            }
        }
    }
    private function getProductStock($productId)
    {
        $stockRegistry = $this->stockRegistry->getStockItem($productId);
        return $stockRegistry;
    }
}

Considering all the stocks on the inventory and store

Leave a comment

Your email address will not be published. Required fields are marked *