How To

How to Override Core Plugin Method in Magento 2

Hello Magento Friends,

Today’s Magento tutorial for the “How To” category defines How to Override Core Plugin Method in Magento 2. 

While customizing orders, Magento uses a plugin to verify the order was placed or not. Modifying the core file is not an appropriate option. In this situation, you can override core plugin methods.

Let’s dive in for the steps to Override Core Plugin Method in Magento 2.

Steps to Override Core Plugin Method in Magento 2:

Step 1: Create a file in the below path

app\code\Vendor\Extension\etc\di.xml

Now, add the below code

<?xml version="1.0"?> 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
     <type name="Magento\CatalogInventory\Api\StockStateInterface">
     <plugin name="check_quote_item_qty" disabled="true" />
            <plugin name="check_quote_item_qty_custom" type="Vendor\Extension\Plugin\StockState\CheckQuoteItemQtyPlugin"/>
     </type>
</config>

Step 2: Now go to the below path

app\code\Vendor\Extension\Plugin\StockState\CheckQuoteItemQtyPlugin.php

And add the code as follows

<?php
declare(strict_types=1);

namespace Vendor\Extension\Plugin\StockState;

use Magento\CatalogInventory\Api\StockStateInterface;
use Magento\Framework\DataObject;
use Magento\Framework\DataObject\Factory as ObjectFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Locale\FormatInterface;
use Magento\InventoryCatalogApi\Model\GetSkusByProductIdsInterface;
use Magento\InventorySales\Model\IsProductSalableCondition\BackOrderNotifyCustomerCondition;
use Magento\InventorySales\Model\IsProductSalableForRequestedQtyCondition\ProductSalabilityError;
use Magento\InventorySalesApi\Api\AreProductsSalableForRequestedQtyInterface;
use Magento\InventorySalesApi\Api\Data\IsProductSalableForRequestedQtyRequestInterfaceFactory;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
 * Replace legacy quote item check
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */class CheckQuoteItemQtyPlugin
{
    /**
     * @var ObjectFactory
     */    private $objectFactory;

    /**
     * @var FormatInterface
     */    private $format;

    /**
     * @var AreProductsSalableForRequestedQtyInterface
     */    private $areProductsSalableForRequestedQty;

    /**
     * @var IsProductSalableForRequestedQtyRequestInterfaceFactory
     */    private $isProductSalableForRequestedQtyRequestInterfaceFactory;

    /**
     * @var GetSkusByProductIdsInterface
     */    private $getSkusByProductIds;

    /**
     * @var StockResolverInterface
     */    private $stockResolver;

    /**
     * @var StoreManagerInterface
     */    private $storeManager;

    /**
     * @var BackOrderNotifyCustomerCondition
     */    private $backOrderNotifyCustomerCondition;

    /**
     * @param ObjectFactory $objectFactory
     * @param FormatInterface $format
     * @param AreProductsSalableForRequestedQtyInterface $areProductsSalableForRequestedQty
     * @param IsProductSalableForRequestedQtyRequestInterfaceFactory $isProductSalableForRequestedQtyRequestFactory
     * @param GetSkusByProductIdsInterface $getSkusByProductIds
     * @param StockResolverInterface $stockResolver
     * @param StoreManagerInterface $storeManager
     * @param BackOrderNotifyCustomerCondition $backOrderNotifyCustomerCondition
     * @SuppressWarnings(PHPMD.LongVariable)
     */    public function __construct(
        ObjectFactory $objectFactory,
        FormatInterface $format,
        AreProductsSalableForRequestedQtyInterface $areProductsSalableForRequestedQty,
        IsProductSalableForRequestedQtyRequestInterfaceFactory $isProductSalableForRequestedQtyRequestFactory,
        GetSkusByProductIdsInterface $getSkusByProductIds,
        StockResolverInterface $stockResolver,
        StoreManagerInterface $storeManager,
        BackOrderNotifyCustomerCondition $backOrderNotifyCustomerCondition
    ) {
        $this->objectFactory = $objectFactory;
        $this->format = $format;
        $this->areProductsSalableForRequestedQty = $areProductsSalableForRequestedQty;
        $this->isProductSalableForRequestedQtyRequestInterfaceFactory = $isProductSalableForRequestedQtyRequestFactory;
        $this->getSkusByProductIds = $getSkusByProductIds;
        $this->stockResolver = $stockResolver;
        $this->storeManager = $storeManager;
        $this->backOrderNotifyCustomerCondition = $backOrderNotifyCustomerCondition;
    }

    /**
     * Replace legacy quote item check
     *
     * @param StockStateInterface $subject
     * @param \Closure $proceed
     * @param int $productId
     * @param float $itemQty
     * @param float $qtyToCheck
     * @param float $origQty
     * @param int|null $scopeId
     *
     * @return DataObject
     * @throws LocalizedException
     * @throws NoSuchEntityException
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */    public function aroundCheckQuoteItemQty(
        StockStateInterface $subject,
        \Closure $proceed,
        $productId,
        $itemQty,
        $qtyToCheck,
        $origQty,
        $scopeId = null
    ) {
        $result = $this->objectFactory->create();
        $result->setHasError(false);

        die('core plugin');

        return $result;
    }

    /**
     * Convert quantity to a valid float
     *
     * @param string|float|int|null $qty
     *
     * @return float|null
     */    private function getNumber($qty)
    {
        if (!is_numeric($qty))
        {
            return $this->format->getNumber($qty);
        }

        return $qty;
    }
}

Conclusion:

This way you can Override Core Plugin in Magento 2. Check out the related blog – How to Override core code using Custom Plugin in Magento 2.

If you have any difficulty, connect with me via the comment box. Share the article with your friends and see you in the next article.

Happy Coding!

Click to rate this post!
[Total: 5 Average: 4.8]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

2 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

4 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

4 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

5 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

6 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago