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.
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; } }
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!
Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…
In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…
October was an exciting month for MageComp! From significant updates across our Magento 2 extension…
In modern web development, seamless navigation and state management are crucial for delivering a smooth…
Magento Open Source 2.4.8 beta version released on October 8, 2024. The latest release of…
Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…