Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed- configurable product not back in stock after updating qty for the variant product by API/import #3393

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryConfigurableProduct\Plugin\CatalogInventory\Api\StockRegistry;

use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\Catalog\Model\ProductFactory;
use Magento\ConfigurableProduct\Model\Inventory\ChangeParentStockStatus;

/***
* Update stock status of configurable products when update children products stock status
*/
class UpdateLegacyStockStatusForConfigurableProduct
{
/**
* @var ChangeParentStockStatus
*/
private $changeParentStockStatus;

/**
* @var ProductFactory
*/
private $productFactory;

/**
* @param ChangeParentStockStatus $changeParentStockStatus
* @param ProductFactory $productFactory
*/
public function __construct(
ChangeParentStockStatus $changeParentStockStatus,
ProductFactory $productFactory
) {
$this->changeParentStockStatus = $changeParentStockStatus;
$this->productFactory = $productFactory;
}

/**
* Update product stock item by product SKU
*
* @param StockRegistryInterface $subject
* @param int $result
* @param string $productSku
* @param StockItemInterface $stockItem
* @return int
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterUpdateStockItemBySku(
StockRegistryInterface $subject,
$result,
string $productSku,
StockItemInterface $stockItem
) {
$productIds = $this->resolveProductId($productSku);
if ($productIds) {
$this->changeParentStockStatus->execute([$productIds]);
}
return $result;
}

/**
* Resolve and retrieve the product ID based on the SKU
*
* @param string $productSku
* @return int
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
private function resolveProductId($productSku)
{
$product = $this->productFactory->create();
$productId = $product->getIdBySku($productSku);
if (!$productId) {
throw new \Magento\Framework\Exception\NoSuchEntityException(
__(
'The Product with the "%1" SKU doesn\'t exist.',
$productSku
)
);
}
return $productId;
}
}
2 changes: 2 additions & 0 deletions InventoryConfigurableProduct/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
<type name="Magento\CatalogInventory\Api\StockRegistryInterface">
<plugin name="inventory_catalog_add_qty_to_legacy_stock_configurable"
type="Magento\InventoryConfigurableProduct\Plugin\CatalogInventory\Api\StockRegistry\SetQtyToLegacyStock" sortOrder="100"/>
<plugin name="inventory_stockupdate_legacy_stock_configurable"
type="Magento\InventoryConfigurableProduct\Plugin\CatalogInventory\Api\StockRegistry\UpdateLegacyStockStatusForConfigurableProduct"/>
</type>
<type name="Magento\CatalogInventory\Model\ResourceModel\Stock\Item">
<plugin name="after_update_stock_item_for_new_configurable_product"
Expand Down