Skip to content

Commit 08916c4

Browse files
committed
Merge remote-tracking branch 'l3/MC-41184' into L3-PR-20210324
2 parents 1b82d65 + b2238c8 commit 08916c4

File tree

7 files changed

+308
-157
lines changed

7 files changed

+308
-157
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ConfigurableImportExport\Plugin\Import\Product;
9+
10+
use Magento\CatalogImportExport\Model\StockItemImporterInterface;
11+
use Magento\ConfigurableProduct\Model\Inventory\ChangeParentStockStatus;
12+
13+
/**
14+
* Update configurable products stock item status based on children products stock status after import
15+
*/
16+
class UpdateConfigurableProductsStockItemStatusPlugin
17+
{
18+
/**
19+
* @var ChangeParentStockStatus
20+
*/
21+
private $changeParentStockStatus;
22+
23+
/**
24+
* @param ChangeParentStockStatus $changeParentStockStatus
25+
*/
26+
public function __construct(
27+
ChangeParentStockStatus $changeParentStockStatus
28+
) {
29+
$this->changeParentStockStatus = $changeParentStockStatus;
30+
}
31+
32+
/**
33+
* Update configurable products stock item status based on children products stock status after import
34+
*
35+
* @param StockItemImporterInterface $subject
36+
* @param mixed $result
37+
* @param array $stockData
38+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
39+
*/
40+
public function afterImport(
41+
StockItemImporterInterface $subject,
42+
$result,
43+
array $stockData
44+
): void {
45+
if ($stockData) {
46+
$this->changeParentStockStatus->execute(array_column($stockData, 'product_id'));
47+
}
48+
}
49+
}

app/code/Magento/ConfigurableImportExport/etc/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@
1313
</argument>
1414
</arguments>
1515
</type>
16+
<type name="Magento\CatalogImportExport\Model\StockItemImporterInterface">
17+
<plugin name="update_configurable_products_stock_item_status"
18+
type="Magento\ConfigurableImportExport\Plugin\Import\Product\UpdateConfigurableProductsStockItemStatusPlugin"
19+
sortOrder="100"/>
20+
</type>
1621
</config>
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ConfigurableProduct\Model\Inventory;
9+
10+
use Magento\CatalogInventory\Api\Data\StockItemInterface;
11+
use Magento\CatalogInventory\Api\StockConfigurationInterface;
12+
use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
13+
use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
14+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
15+
16+
/***
17+
* Update stock status of configurable products based on children products stock status
18+
*/
19+
class ChangeParentStockStatus
20+
{
21+
/**
22+
* @var Configurable
23+
*/
24+
private $configurableType;
25+
26+
/**
27+
* @var StockItemCriteriaInterfaceFactory
28+
*/
29+
private $criteriaInterfaceFactory;
30+
31+
/**
32+
* @var StockItemRepositoryInterface
33+
*/
34+
private $stockItemRepository;
35+
36+
/**
37+
* @var StockConfigurationInterface
38+
*/
39+
private $stockConfiguration;
40+
41+
/**
42+
* @param Configurable $configurableType
43+
* @param StockItemCriteriaInterfaceFactory $criteriaInterfaceFactory
44+
* @param StockItemRepositoryInterface $stockItemRepository
45+
* @param StockConfigurationInterface $stockConfiguration
46+
*/
47+
public function __construct(
48+
Configurable $configurableType,
49+
StockItemCriteriaInterfaceFactory $criteriaInterfaceFactory,
50+
StockItemRepositoryInterface $stockItemRepository,
51+
StockConfigurationInterface $stockConfiguration
52+
) {
53+
$this->configurableType = $configurableType;
54+
$this->criteriaInterfaceFactory = $criteriaInterfaceFactory;
55+
$this->stockItemRepository = $stockItemRepository;
56+
$this->stockConfiguration = $stockConfiguration;
57+
}
58+
59+
/**
60+
* Update stock status of configurable products based on children products stock status
61+
*
62+
* @param array $childrenIds
63+
* @return void
64+
*/
65+
public function execute(array $childrenIds): void
66+
{
67+
$parentIds = $this->configurableType->getParentIdsByChild($childrenIds);
68+
foreach (array_unique($parentIds) as $productId) {
69+
$this->processStockForParent((int)$productId);
70+
}
71+
}
72+
73+
/**
74+
* Update stock status of configurable product based on children products stock status
75+
*
76+
* @param int $productId
77+
* @return void
78+
*/
79+
private function processStockForParent(int $productId): void
80+
{
81+
$criteria = $this->criteriaInterfaceFactory->create();
82+
$criteria->setScopeFilter($this->stockConfiguration->getDefaultScopeId());
83+
84+
$criteria->setProductsFilter($productId);
85+
$stockItemCollection = $this->stockItemRepository->getList($criteria);
86+
$allItems = $stockItemCollection->getItems();
87+
if (empty($allItems)) {
88+
return;
89+
}
90+
$parentStockItem = array_shift($allItems);
91+
92+
$childrenIds = $this->configurableType->getChildrenIds($productId);
93+
$criteria->setProductsFilter($childrenIds);
94+
$stockItemCollection = $this->stockItemRepository->getList($criteria);
95+
$allItems = $stockItemCollection->getItems();
96+
97+
$childrenIsInStock = false;
98+
99+
foreach ($allItems as $childItem) {
100+
if ($childItem->getIsInStock() === true) {
101+
$childrenIsInStock = true;
102+
break;
103+
}
104+
}
105+
106+
if ($this->isNeedToUpdateParent($parentStockItem, $childrenIsInStock)) {
107+
$parentStockItem->setIsInStock($childrenIsInStock);
108+
$parentStockItem->setStockStatusChangedAuto(1);
109+
$this->stockItemRepository->save($parentStockItem);
110+
}
111+
}
112+
113+
/**
114+
* Check if parent item should be updated
115+
*
116+
* @param StockItemInterface $parentStockItem
117+
* @param bool $childrenIsInStock
118+
* @return bool
119+
*/
120+
private function isNeedToUpdateParent(
121+
StockItemInterface $parentStockItem,
122+
bool $childrenIsInStock
123+
): bool {
124+
return $parentStockItem->getIsInStock() !== $childrenIsInStock &&
125+
($childrenIsInStock === false || $parentStockItem->getStockStatusChangedAuto());
126+
}
127+
}

app/code/Magento/ConfigurableProduct/Model/Inventory/ParentItemProcessor.php

Lines changed: 10 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,36 @@
1212
use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
1313
use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
1414
use Magento\CatalogInventory\Api\StockConfigurationInterface;
15-
use Magento\CatalogInventory\Api\Data\StockItemInterface;
1615
use Magento\CatalogInventory\Observer\ParentItemProcessorInterface;
16+
use Magento\Framework\App\ObjectManager;
1717

1818
/**
1919
* Process parent stock item
2020
*/
2121
class ParentItemProcessor implements ParentItemProcessorInterface
2222
{
2323
/**
24-
* @var Configurable
24+
* @var ChangeParentStockStatus
2525
*/
26-
private $configurableType;
27-
28-
/**
29-
* @var StockItemCriteriaInterfaceFactory
30-
*/
31-
private $criteriaInterfaceFactory;
32-
33-
/**
34-
* @var StockItemRepositoryInterface
35-
*/
36-
private $stockItemRepository;
37-
38-
/**
39-
* @var StockConfigurationInterface
40-
*/
41-
private $stockConfiguration;
26+
private $changeParentStockStatus;
4227

4328
/**
4429
* @param Configurable $configurableType
4530
* @param StockItemCriteriaInterfaceFactory $criteriaInterfaceFactory
4631
* @param StockItemRepositoryInterface $stockItemRepository
4732
* @param StockConfigurationInterface $stockConfiguration
33+
* @param ChangeParentStockStatus|null $changeParentStockStatus
34+
* @SuppressWarnings(PHPMD.UnusedFormalParameter) Deprecated dependencies
4835
*/
4936
public function __construct(
5037
Configurable $configurableType,
5138
StockItemCriteriaInterfaceFactory $criteriaInterfaceFactory,
5239
StockItemRepositoryInterface $stockItemRepository,
53-
StockConfigurationInterface $stockConfiguration
40+
StockConfigurationInterface $stockConfiguration,
41+
?ChangeParentStockStatus $changeParentStockStatus = null
5442
) {
55-
$this->configurableType = $configurableType;
56-
$this->criteriaInterfaceFactory = $criteriaInterfaceFactory;
57-
$this->stockItemRepository = $stockItemRepository;
58-
$this->stockConfiguration = $stockConfiguration;
43+
$this->changeParentStockStatus = $changeParentStockStatus
44+
?? ObjectManager::getInstance()->get(ChangeParentStockStatus::class);
5945
}
6046

6147
/**
@@ -66,64 +52,6 @@ public function __construct(
6652
*/
6753
public function process(Product $product)
6854
{
69-
$parentIds = $this->configurableType->getParentIdsByChild($product->getId());
70-
foreach ($parentIds as $productId) {
71-
$this->processStockForParent((int)$productId);
72-
}
73-
}
74-
75-
/**
76-
* Change stock item for parent product depending on children stock items
77-
*
78-
* @param int $productId
79-
* @return void
80-
*/
81-
private function processStockForParent(int $productId)
82-
{
83-
$criteria = $this->criteriaInterfaceFactory->create();
84-
$criteria->setScopeFilter($this->stockConfiguration->getDefaultScopeId());
85-
86-
$criteria->setProductsFilter($productId);
87-
$stockItemCollection = $this->stockItemRepository->getList($criteria);
88-
$allItems = $stockItemCollection->getItems();
89-
if (empty($allItems)) {
90-
return;
91-
}
92-
$parentStockItem = array_shift($allItems);
93-
94-
$childrenIds = $this->configurableType->getChildrenIds($productId);
95-
$criteria->setProductsFilter($childrenIds);
96-
$stockItemCollection = $this->stockItemRepository->getList($criteria);
97-
$allItems = $stockItemCollection->getItems();
98-
99-
$childrenIsInStock = false;
100-
101-
foreach ($allItems as $childItem) {
102-
if ($childItem->getIsInStock() === true) {
103-
$childrenIsInStock = true;
104-
break;
105-
}
106-
}
107-
108-
if ($this->isNeedToUpdateParent($parentStockItem, $childrenIsInStock)) {
109-
$parentStockItem->setIsInStock($childrenIsInStock);
110-
$parentStockItem->setStockStatusChangedAuto(1);
111-
$this->stockItemRepository->save($parentStockItem);
112-
}
113-
}
114-
115-
/**
116-
* Check is parent item should be updated
117-
*
118-
* @param StockItemInterface $parentStockItem
119-
* @param bool $childrenIsInStock
120-
* @return bool
121-
*/
122-
private function isNeedToUpdateParent(
123-
StockItemInterface $parentStockItem,
124-
bool $childrenIsInStock
125-
): bool {
126-
return $parentStockItem->getIsInStock() !== $childrenIsInStock &&
127-
($childrenIsInStock === false || $parentStockItem->getStockStatusChangedAuto());
55+
$this->changeParentStockStatus->execute([$product->getId()]);
12856
}
12957
}

0 commit comments

Comments
 (0)