Skip to content

Commit e8c31f5

Browse files
authored
Merge pull request #7313 from magento-l3/L3_PR_21-12-10
L3_PR_21-12-10
2 parents 82b7dba + 0f3d7e4 commit e8c31f5

File tree

80 files changed

+3235
-524
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+3235
-524
lines changed
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+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
11+
<actionGroup name="AdminAssertSpecialPriceAttributeValueOnProductGridPageActionGroup">
12+
<annotations>
13+
<description>Assert special price attribute value from the catalog product grid page</description>
14+
</annotations>
15+
<arguments>
16+
<argument name="specialPriceColumn" type="string" defaultValue="Special Price"/>
17+
<argument name="expectedValue" type="string" defaultValue=""/>
18+
</arguments>
19+
<!-- Check the special price column are present in catalog product grid -->
20+
<seeElement selector="{{AdminProductGridSection.columnHeader(specialPriceColumn)}}" stepKey="seeSpecialPriceColumn"/>
21+
<!-- Grab the special price value from the catalog product grid -->
22+
<grabTextFrom selector="{{AdminProductGridSection.productGridCell('1', specialPriceColumn)}}" stepKey="getSpecialPrice"/>
23+
<assertStringContainsString stepKey="assertSpecialPricePercentageSymbol">
24+
<expectedResult type="string">{{expectedValue}}</expectedResult>
25+
<actualResult type="variable">$getSpecialPrice</actualResult>
26+
</assertStringContainsString>
27+
</actionGroup>
28+
</actionGroups>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
11+
<test name="AdminBundleProductPriceSymbolValidationInGridTest">
12+
<annotations>
13+
<features value="Bundle"/>
14+
<stories value="Bundle Products Special Price Column in admin Grid should have % sign not currency sign"/>
15+
<title value="Admin to validate the bundle products special price column in grid should display percentage symbol instead of currency sign"/>
16+
<description value="Admin to validate the bundle products special price column in grid should display percentage symbol instead of currency sign"/>
17+
<severity value="AVERAGE"/>
18+
<testCaseId value="AC-1378"/>
19+
<useCaseId value="ACP2E-64"/>
20+
<group value="Bundle"/>
21+
</annotations>
22+
<before>
23+
<!-- Create a simple product -->
24+
<createData entity="SimpleProduct2" stepKey="simpleProduct1"/>
25+
<!-- Admin login -->
26+
<actionGroup ref="AdminLoginActionGroup" stepKey="loginToAdminPanel"/>
27+
<!-- Navigate to catalog product grid page -->
28+
<actionGroup ref="AdminOpenProductIndexPageActionGroup" stepKey="navigateToProductIndexPage"/>
29+
<!-- Open the column dropdown to add the special price from the catalog product grid -->
30+
<actionGroup ref="ToggleAdminProductGridColumnsDropdownActionGroup" stepKey="openColumnsDropdownSpecialPrice"/>
31+
<actionGroup ref="CheckAdminProductGridColumnOptionActionGroup" stepKey="checkSpecialPriceOption">
32+
<argument name="optionName" value="Special Price"/>
33+
</actionGroup>
34+
<actionGroup ref="ToggleAdminProductGridColumnsDropdownActionGroup" stepKey="closeColumnsDropdownSpecialPrice"/>
35+
<!-- It takes a few seconds for column update to be saved -->
36+
<!-- waitForPageLoad won't work here since saving is happening with a short delay -->
37+
<wait time="5" stepKey="waitForColumnUpdateToSave"/>
38+
</before>
39+
<after>
40+
<!-- Navigate to catalog product grid page -->
41+
<actionGroup ref="AdminOpenProductIndexPageActionGroup" stepKey="navigateToProductIndexPage"/>
42+
<!-- Clean applied product filters before delete -->
43+
<actionGroup ref="AdminClearGridFiltersActionGroup" stepKey="clearAppliedFilters"/>
44+
<!-- Delete all the products from the catalog product grid -->
45+
<actionGroup ref="DeleteProductsIfTheyExistActionGroup" stepKey="deleteAllProducts"/>
46+
<!-- Set product grid to default columns -->
47+
<actionGroup ref="ResetProductGridToDefaultViewActionGroup" stepKey="setProductGridToDefaultColumns"/>
48+
<!-- Logging out -->
49+
<actionGroup ref="AdminLogoutActionGroup" stepKey="logout"/>
50+
</after>
51+
<!-- Go to bundle product creation page -->
52+
<actionGroup ref="AdminOpenNewProductFormPageActionGroup" stepKey="openNewBundleProductPage">
53+
<argument name="productType" value="{{BundleProduct.type}}"/>
54+
<argument name="attributeSetId" value="{{BundleProduct.set}}"/>
55+
</actionGroup>
56+
<!-- Sets the provided Special Price on the Admin Product creation/edit page. -->
57+
<actionGroup ref="AddSpecialPriceToProductActionGroup" stepKey="addSpecialPrice">
58+
<argument name="price" value="{{SimpleProductWithSpecialPrice.special_price}}"/>
59+
</actionGroup>
60+
<!-- Fill up the new product form with data -->
61+
<actionGroup ref="CreateBasicBundleProductActionGroup" stepKey="createBundledProduct">
62+
<argument name="bundleProduct" value="BundleProduct"/>
63+
</actionGroup>
64+
<!-- Add the bundle option to the product -->
65+
<actionGroup ref="AddBundleOptionWithOneProductActionGroup" stepKey="addBundleOption">
66+
<argument name="x" value="0"/>
67+
<argument name="n" value="1"/>
68+
<argument name="prodOneSku" value="$$simpleProduct1.sku$$"/>
69+
<argument name="prodTwoSku" value=""/>
70+
<argument name="optionTitle" value="{{BundleProduct.optionTitle1}}"/>
71+
<argument name="inputType" value="{{BundleProduct.optionInputType1}}"/>
72+
</actionGroup>
73+
<!-- Save the bundle product -->
74+
<actionGroup ref="SaveProductFormActionGroup" stepKey="saveProductForm"/>
75+
<!-- Navigate to catalog product grid page -->
76+
<actionGroup ref="AdminOpenProductIndexPageActionGroup" stepKey="navigateToProductIndexPageAfterProdSave"/>
77+
<!-- Search the created bundle product with sku -->
78+
<actionGroup ref="FilterProductGridBySku2ActionGroup" stepKey="filterBundleProductGridBySku">
79+
<argument name="sku" value="{{BundleProduct.sku}}"/>
80+
</actionGroup>
81+
<!-- Asserting with the special price value contains the percentage value -->
82+
<actionGroup ref="AdminAssertSpecialPriceAttributeValueOnProductGridPageActionGroup" stepKey="assertSpecialPricePercentageSymbol">
83+
<argument name="expectedValue" value="90.00%"/>
84+
</actionGroup>
85+
</test>
86+
</tests>

0 commit comments

Comments
 (0)