Last active
March 1, 2024 12:15
-
-
Save sandipklevu/36c7460b12e69ebabf8bb15af7e0c772 to your computer and use it in GitHub Desktop.
Plugin for Child products stock status change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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\StockRegistryInterface"> | |
<plugin name="Custom_Erp::addStockProductsInQueue" | |
type="Custom\Es\Plugin\StockUpdatePlugin"/> | |
</type> | |
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Custom\Es\Plugin; | |
use Klevu\Search\Model\Product\MagentoProductActionsInterface; | |
use Magento\Catalog\Api\ProductRepositoryInterface; | |
use Magento\CatalogInventory\Api\StockRegistryInterface; | |
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable; | |
use Magento\Store\Model\StoreManagerInterface; | |
class StockUpdatePlugin | |
{ | |
/** | |
* @var StoreManagerInterface | |
*/ | |
private $storeManager; | |
/** | |
* @var ProductRepositoryInterface | |
*/ | |
private $productRepository; | |
/** | |
* @var MagentoProductActionsInterface | |
*/ | |
private $magentoProductActions; | |
/** | |
* @var Configurable | |
*/ | |
private $catalogProductTypeConfigurable; | |
/** | |
* StockUpdatePlugin constructor. | |
* | |
* @param StoreManagerInterface $storeManager | |
* @param ProductRepositoryInterface $productRepository | |
* @param MagentoProductActionsInterface $magentoProductActions | |
* @param Configurable $catalogProductTypeConfigurable | |
*/ | |
public function __construct( | |
StoreManagerInterface $storeManager, | |
ProductRepositoryInterface $productRepository, | |
MagentoProductActionsInterface $magentoProductActions, | |
Configurable $catalogProductTypeConfigurable | |
) { | |
$this->storeManager = $storeManager; | |
$this->productRepository = $productRepository; | |
$this->magentoProductActions = $magentoProductActions; | |
$this->catalogProductTypeConfigurable = $catalogProductTypeConfigurable; | |
} | |
/** | |
* @param StockRegistryInterface $subject | |
* @param $result | |
* | |
* @return mixed | |
*/ | |
public function afterUpdateStockItemBySku( | |
StockRegistryInterface $subject, | |
$result, | |
string $productSku, | |
$stockItem, | |
) { | |
try { | |
$storeId = (int)$this->storeManager->getStore()->getId(); | |
$product = $this->productRepository->get($productSku); | |
if ($product instanceof \Magento\Catalog\Api\Data\ProductInterface) { | |
$parentId = $this->getParentIdByChild($product->getId()); | |
if (!$parentId) { | |
return $result; | |
} | |
$this->magentoProductActions->markRecordIntoQueue([$parentId], 'products', $storeId); | |
} | |
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) { | |
return $result; | |
} | |
return $result; | |
} | |
private function getParentIdByChild(int $childId) | |
{ | |
$parentId = null; | |
$parentIds = $this->catalogProductTypeConfigurable->getParentIdsByChild($childId); | |
if (!$parentIds) { | |
return null; | |
} | |
if (is_array($parentIds) && isset($parentIds[0])) { | |
$parentId = $parentIds[0]; | |
} | |
return $parentId; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment