Skip to content
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,152 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryConfigurableProduct\Model\Inventory;

use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\RequestInterface;
use Magento\Inventory\Model\ResourceModel\Source\CollectionFactory;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
use Magento\InventoryCatalogApi\Model\GetSkusByProductIdsInterface;

/**
* @inheritDoc
*/
class ChangeParentProductStockStatus
{
/**
* @var RequestInterface
*/
private RequestInterface $request;

/**
* @var Configurable
*/
private Configurable $configurableType;

/**
* @var StockRegistryInterface
*/
private StockRegistryInterface $stockRegistry;

/**
* @var SearchCriteriaBuilder
*/
private SearchCriteriaBuilder $searchCriteriaBuilder;

/**
* @var SourceItemRepositoryInterface
*/
private SourceItemRepositoryInterface $sourceItemRepository;

/**
* @var StockItemRepositoryInterface
*/
private StockItemRepositoryInterface $stockItemRepository;

/**
* @var GetSkusByProductIdsInterface
*/
private GetSkusByProductIdsInterface $getSkusByProductIds;

/**
* @var CollectionFactory
*/
private CollectionFactory $collectionFactory;

/**
* @param RequestInterface $request
* @param Configurable $configurableType
*/
public function __construct(
RequestInterface $request,
Configurable $configurableType,
StockRegistryInterface $stockRegistry,
SearchCriteriaBuilder $searchCriteriaBuilder,
SourceItemRepositoryInterface $sourceItemRepository,
StockItemRepositoryInterface $stockItemRepository,
GetSkusByProductIdsInterface $getSkusByProductIds,
CollectionFactory $collectionFactory
) {
$this->request = $request;
$this->configurableType = $configurableType;
$this->stockRegistry = $stockRegistry;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->sourceItemRepository = $sourceItemRepository;
$this->stockItemRepository = $stockItemRepository;
$this->getSkusByProductIds = $getSkusByProductIds;
$this->collectionFactory = $collectionFactory;
}

/**
* @inheritDoc
*/
public function execute($childproductId): void
{
$parentIds = $this->configurableType->getParentIdsByChild($childproductId);
foreach (array_unique($parentIds) as $productId) {
$this->processStockForParent((int)$childproductId, (int)$productId);
}
}

/**
* Update stock status of configurable product based on children products stock status
*
* @param int $childproductId
* @param int $productId
* @return void
*/
private function processStockForParent(int $childproductId, int $productId): void
{
$childrenIsInStock = false;

if ($sources = $this->request->getParam('sources')) {
if ($currentsourceItems = $sources['assigned_sources']) {
foreach ($currentsourceItems as $childItem) {
if ($childItem['status'] && $childItem['quantity'] > 0 && $childItem['source_status']) {
$childrenIsInStock = true;
break;
}
}
}
}

if (!$childrenIsInStock) {
$sourceCodes = $this->collectionFactory->create()
->addFieldToFilter(SourceInterface::ENABLED, 1)
->addFieldToSelect('source_code')
->getColumnValues('source_code');
$childrenIds = $this->configurableType->getChildrenIds($productId);
$childrenSkus = $this->getSkusByProductIds->execute($childrenIds[0]);

$searchCriteria = $this->searchCriteriaBuilder
->addFilter(SourceItemInterface::SOURCE_CODE, $sourceCodes, 'in')
->addFilter(SourceItemInterface::SKU, $childrenSkus, 'in')
->addFilter(SourceItemInterface::SKU, $childrenSkus[$childproductId], 'neq')
->addFilter(SourceItemInterface::STATUS, 1)
->create();

$sourceItems = $this->sourceItemRepository->getList($searchCriteria)->getItems();
foreach ($sourceItems as $childItem) {
if ($childItem->getStatus()) {
$childrenIsInStock = true;
break;
}
}
}

$parentStockItem = $this->stockRegistry->getStockItem($productId);
$parentStockItem->setIsInStock($childrenIsInStock);
$parentStockItem->setStockStatusChangedAuto(1);
$this->stockItemRepository->save($parentStockItem);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Magento\InventoryCatalogApi\Model\GetSkusByProductIdsInterface;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\CatalogInventory\Model\Stock;
use Magento\InventoryConfigurableProduct\Model\Inventory\ChangeParentProductStockStatus;
use Magento\InventoryConfigurableProduct\Model\IsProductSalableCondition\IsConfigurableProductChildrenSalable;
use Magento\InventoryConfiguration\Model\GetLegacyStockItem;

Expand Down Expand Up @@ -48,20 +49,28 @@ class UpdateLegacyStockStatusForConfigurableProduct
*/
private $isConfigurableProductChildrenSalable;

/**
* @var ChangeParentProductStockStatus
*/
private $changeParentProductStockStatus;

/**
* @param GetProductTypeById $getProductTypeById
* @param SetDataToLegacyStockStatus $setDataToLegacyStockStatus
* @param GetSkusByProductIdsInterface $getSkusByProductIds
* @param GetLegacyStockItem $getLegacyStockItem
* @param IsConfigurableProductChildrenSalable $isConfigurableProductChildrenSalable
* @param ChangeParentProductStockStatus $changeParentProductStockStatus
*/
public function __construct(
ChangeParentProductStockStatus $changeParentProductStockStatus,
GetProductTypeById $getProductTypeById,
SetDataToLegacyStockStatus $setDataToLegacyStockStatus,
GetSkusByProductIdsInterface $getSkusByProductIds,
GetLegacyStockItem $getLegacyStockItem,
IsConfigurableProductChildrenSalable $isConfigurableProductChildrenSalable
) {
$this->changeParentProductStockStatus = $changeParentProductStockStatus;
$this->getProductTypeById = $getProductTypeById;
$this->setDataToLegacyStockStatus = $setDataToLegacyStockStatus;
$this->getSkusByProductIds = $getSkusByProductIds;
Expand All @@ -82,11 +91,12 @@ public function __construct(
*/
public function afterSave(ItemResourceModel $subject, ItemResourceModel $result, StockItem $stockItem)
{
$stockProductId = $stockItem->getProductId();
if ($stockItem->getIsInStock() &&
$this->getProductTypeById->execute($stockItem->getProductId()) === Configurable::TYPE_CODE
$this->getProductTypeById->execute($stockProductId) === Configurable::TYPE_CODE
) {
$productSku = $this->getSkusByProductIds
->execute([$stockItem->getProductId()])[$stockItem->getProductId()];
->execute([$stockProductId])[$stockItem->getProductId()];

if ($stockItem->getStockStatusChangedAuto() ||
($this->stockStatusChange($productSku)
Expand All @@ -100,6 +110,7 @@ public function afterSave(ItemResourceModel $subject, ItemResourceModel $result,
);
}
}
$this->changeParentProductStockStatus->execute($stockItem->getProductId());

return $result;
}
Expand Down
Loading