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
1 2 3 4 5 6 7 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
<?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!