I would like to override core plugin method
Core plugin path
MagentoInventorySalesPluginStockStateCheckQuoteItemQtyPlugin.php
In this file I would like to override aroundCheckQuoteItemQty()
function and I refer all the related links solutions in magento.stackexchange.com
but no luck
in my di.xml
file
<type name="MagentoInventorySalesPluginStockStateCheckQuoteItemQtyPlugin">
<plugin name="check_quote_item_qty" disabled="true" />
<plugin name="vendor_name_check_quote_item_qty" type="VendorNameModelPluginStockStateCheckQuoteItemQtyPlugin" sortOrder="99" />
</type>
Here is my custom plugin code and I put die()
function in aroundCheckQuoteItemQty
function but why it is not overrding
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace VendorNameModelPluginStockState;
use MagentoCatalogInventoryApiStockStateInterface;
use MagentoFrameworkDataObject;
use MagentoFrameworkDataObjectFactory as ObjectFactory;
use MagentoFrameworkExceptionLocalizedException;
use MagentoFrameworkExceptionNoSuchEntityException;
use MagentoFrameworkLocaleFormatInterface;
use MagentoInventoryCatalogApiModelGetSkusByProductIdsInterface;
use MagentoInventorySalesModelIsProductSalableConditionBackOrderNotifyCustomerCondition;
use MagentoInventorySalesModelIsProductSalableForRequestedQtyConditionProductSalabilityError;
use MagentoInventorySalesApiApiAreProductsSalableForRequestedQtyInterface;
use MagentoInventorySalesApiApiDataIsProductSalableForRequestedQtyRequestInterfaceFactory;
use MagentoInventorySalesApiApiDataSalesChannelInterface;
use MagentoInventorySalesApiApiStockResolverInterface;
use MagentoStoreModelStoreManagerInterface;
use MagentoInventorySalesPluginStockStateCheckQuoteItemQtyPlugin as coreCheckQuoteItemQtyPlugin;
/**
* 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
) { die('call plugin');
$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
) {die('call plugin');
$result = $this->objectFactory->create();
$result->setHasError(false);
$qty = max($this->getNumber($itemQty), $this->getNumber($qtyToCheck));
$skus = $this->getSkusByProductIds->execute([$productId]);
$productSku = $skus[$productId];
$websiteCode = $this->storeManager->getWebsite($scopeId)->getCode();
$stock = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode);
$stockId = $stock->getStockId();
$request = $this->isProductSalableForRequestedQtyRequestInterfaceFactory->create(
[
'sku' => $productSku,
'qty' => $qty,
]
);
$productsSalableResult = $this->areProductsSalableForRequestedQty->execute([$request], (int)$stockId);
$productsSalableResult = current($productsSalableResult);
if ($productsSalableResult->isSalable() === false) {
/** @var ProductSalabilityError $error */
foreach ($productsSalableResult->getErrors() as $error) {
$result->setHasError(true)->setMessage($error->getMessage())->setQuoteMessage("Some of the products are out of stock.")
->setQuoteMessageIndex('qty');
}
} else {
$productSalableResult = $this->backOrderNotifyCustomerCondition->execute($productSku, (int)$stockId, $qty);
if ($productSalableResult->getErrors()) {
/** @var ProductSalabilityError $error */
foreach ($productSalableResult->getErrors() as $error) {
$result->setMessage($error->getMessage());
}
}
}
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;
}
}