Skip to content

Commit 661f39a

Browse files
committed
Merge remote-tracking branch 'magento-l3/ACP2E-262' into L3_PR_21-12-10
2 parents da2b7e2 + 2f4ff1d commit 661f39a

File tree

11 files changed

+542
-85
lines changed

11 files changed

+542
-85
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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\Bundle\Model\Inventory;
9+
10+
use Magento\Bundle\Model\Product\Type;
11+
use Magento\CatalogInventory\Api\Data\StockItemInterface;
12+
use Magento\CatalogInventory\Api\StockConfigurationInterface;
13+
use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
14+
use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
15+
16+
/***
17+
* Update stock status of bundle products based on children products stock status
18+
*/
19+
class ChangeParentStockStatus
20+
{
21+
/**
22+
* @var Type
23+
*/
24+
private $bundleType;
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 StockItemCriteriaInterfaceFactory $criteriaInterfaceFactory
43+
* @param StockItemRepositoryInterface $stockItemRepository
44+
* @param StockConfigurationInterface $stockConfiguration
45+
* @param Type $bundleType
46+
*/
47+
public function __construct(
48+
StockItemCriteriaInterfaceFactory $criteriaInterfaceFactory,
49+
StockItemRepositoryInterface $stockItemRepository,
50+
StockConfigurationInterface $stockConfiguration,
51+
Type $bundleType
52+
) {
53+
$this->bundleType = $bundleType;
54+
$this->criteriaInterfaceFactory = $criteriaInterfaceFactory;
55+
$this->stockItemRepository = $stockItemRepository;
56+
$this->stockConfiguration = $stockConfiguration;
57+
}
58+
59+
/**
60+
* Update stock status of bundle 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->bundleType->getParentIdsByChild($childrenIds);
68+
foreach (array_unique($parentIds) as $productId) {
69+
$this->processStockForParent((int)$productId);
70+
}
71+
}
72+
73+
/**
74+
* Update stock status of bundle 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+
$stockItems = $this->getStockItems([$productId]);
82+
$parentStockItem = $stockItems[$productId] ?? null;
83+
if ($parentStockItem) {
84+
$childrenIsInStock = $this->isChildrenInStock($productId);
85+
if ($this->isNeedToUpdateParent($parentStockItem, $childrenIsInStock)) {
86+
$parentStockItem->setIsInStock($childrenIsInStock);
87+
$parentStockItem->setStockStatusChangedAuto(1);
88+
$this->stockItemRepository->save($parentStockItem);
89+
}
90+
}
91+
}
92+
93+
/**
94+
* Returns stock status of bundle product based on children stock status
95+
*
96+
* Returns TRUE if any of the following conditions is true:
97+
* - At least one product is in-stock in each required option
98+
* - Any product is in-stock (if all options are optional)
99+
*
100+
* @param int $productId
101+
* @return bool
102+
*/
103+
private function isChildrenInStock(int $productId) : bool
104+
{
105+
$childrenIsInStock = false;
106+
$childrenIds = $this->bundleType->getChildrenIds($productId, true);
107+
$stockItems = $this->getStockItems(array_merge(...array_values($childrenIds)));
108+
foreach ($childrenIds as $childrenIdsPerOption) {
109+
$childrenIsInStock = false;
110+
foreach ($childrenIdsPerOption as $id) {
111+
$stockItem = $stockItems[$id] ?? null;
112+
if ($stockItem && $stockItem->getIsInStock()) {
113+
$childrenIsInStock = true;
114+
break;
115+
}
116+
}
117+
if (!$childrenIsInStock) {
118+
break;
119+
}
120+
}
121+
122+
return $childrenIsInStock;
123+
}
124+
125+
/**
126+
* Check if parent item should be updated
127+
*
128+
* @param StockItemInterface $parentStockItem
129+
* @param bool $childrenIsInStock
130+
* @return bool
131+
*/
132+
private function isNeedToUpdateParent(
133+
StockItemInterface $parentStockItem,
134+
bool $childrenIsInStock
135+
): bool {
136+
return $parentStockItem->getIsInStock() !== $childrenIsInStock &&
137+
($childrenIsInStock === false || $parentStockItem->getStockStatusChangedAuto());
138+
}
139+
140+
/**
141+
* Get stock items for provided product IDs
142+
*
143+
* @param array $productIds
144+
* @return StockItemInterface[]
145+
*/
146+
private function getStockItems(array $productIds): array
147+
{
148+
$criteria = $this->criteriaInterfaceFactory->create();
149+
$criteria->setScopeFilter($this->stockConfiguration->getDefaultScopeId());
150+
$criteria->setProductsFilter(array_unique($productIds));
151+
$stockItemCollection = $this->stockItemRepository->getList($criteria);
152+
153+
$stockItems = [];
154+
foreach ($stockItemCollection->getItems() as $stockItem) {
155+
$stockItems[$stockItem->getProductId()] = $stockItem;
156+
}
157+
158+
return $stockItems;
159+
}
160+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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\Bundle\Model\Inventory;
9+
10+
use Magento\Catalog\Api\Data\ProductInterface as Product;
11+
use Magento\CatalogInventory\Observer\ParentItemProcessorInterface;
12+
13+
/**
14+
* Bundle product stock item processor
15+
*/
16+
class ParentItemProcessor implements ParentItemProcessorInterface
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+
* @inheritdoc
34+
*/
35+
public function process(Product $product)
36+
{
37+
$this->changeParentStockStatus->execute([$product->getId()]);
38+
}
39+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,11 @@
278278
</argument>
279279
</arguments>
280280
</type>
281+
<type name="Magento\CatalogInventory\Observer\SaveInventoryDataObserver">
282+
<arguments>
283+
<argument name="parentItemProcessorPool" xsi:type="array">
284+
<item name="bundle" xsi:type="object">Magento\Bundle\Model\Inventory\ParentItemProcessor</item>
285+
</argument>
286+
</arguments>
287+
</type>
281288
</config>
Lines changed: 49 additions & 0 deletions
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\BundleImportExport\Plugin\Import\Product;
9+
10+
use Magento\CatalogImportExport\Model\StockItemImporterInterface;
11+
use Magento\Bundle\Model\Inventory\ChangeParentStockStatus;
12+
13+
/**
14+
* Update bundle products stock item status based on children products stock status after import
15+
*/
16+
class UpdateBundleProductsStockItemStatusPlugin
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 bundle 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/BundleImportExport/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_bundle_products_stock_item_status"
18+
type="Magento\BundleImportExport\Plugin\Import\Product\UpdateBundleProductsStockItemStatusPlugin"
19+
sortOrder="200"/>
20+
</type>
1621
</config>

0 commit comments

Comments
 (0)