Add Custom developments to add to cart

This is based on the event happening before adding a product to the cart.

Using this event we may modify the qty and price of the product if the requirements are

pointed out.

This can be achieved with a help of a module

Create a module with etc/frontend/di.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="checkout_cart_product_add_after">
        <observer name="customPriceInCart" instance="Vendor\Module\Observer\file" />
    </event>
</config>

After that create an observer to modify the quote table or quote item table with qty / price or any other requirement

<?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;

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

    public function __construct(
        GetProductSalableQtyInterface $getProductSalableQty,
        StockRegistryInterface $stockRegistry,
        CartItemRepositoryInterface $cartItemRepository,
        CartRepositoryInterface $quoteRepository
    ) {
        $this->getProductSalableQty = $getProductSalableQty;
        $this->stockRegistry = $stockRegistry;
        $this->cartItemRepository = $cartItemRepository;
        $this->quoteRepository = $quoteRepository;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
//execute
}
}

Leave a comment

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