Skip to content

Commit 203ba70

Browse files
committed
Reverted the msi modules approach
1 parent 4a9d850 commit 203ba70

File tree

7 files changed

+42
-116
lines changed

7 files changed

+42
-116
lines changed

app/code/Magento/CatalogInventory/Block/Plugin/ProductView.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function afterGetQuantityValidators(
3838
return array_merge(
3939
$validators,
4040
$this->productQuantityValidator->getData(
41-
$block->getProduct()->getSku(),
41+
$block->getProduct()->getId(),
4242
$block->getProduct()->getStore()->getWebsiteId()
4343
)
4444
);

app/code/Magento/CatalogInventory/Model/Product/QuantityValidator.php

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,79 +7,41 @@
77

88
namespace Magento\CatalogInventory\Model\Product;
99

10-
use Magento\Framework\Exception\LocalizedException;
11-
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
12-
use Magento\InventorySalesApi\Api\StockResolverInterface;
13-
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
14-
use Magento\Store\Model\StoreManagerInterface;
10+
use Magento\CatalogInventory\Api\StockRegistryInterface;
1511

1612
class QuantityValidator
1713
{
1814
/**
19-
* @param GetStockItemConfigurationInterface $getStockItemConfiguration
20-
* @param StoreManagerInterface $storeManager
21-
* @param StockResolverInterface $stockResolver
15+
* @param StockRegistryInterface $stockRegistry
2216
*/
2317
public function __construct(
24-
private readonly GetStockItemConfigurationInterface $getStockItemConfiguration,
25-
private readonly StoreManagerInterface $storeManager,
26-
private readonly StockResolverInterface $stockResolver
18+
private readonly StockRegistryInterface $stockRegistry
2719
) {
2820
}
2921

3022
/**
3123
* To get quantity validators
3224
*
33-
* @param string $sku
25+
* @param int $productId
3426
* @param int|null $websiteId
3527
*
3628
* @return array
3729
*/
38-
public function getData(string $sku, int|null $websiteId): array
30+
public function getData(int $productId, int|null $websiteId): array
3931
{
40-
try {
41-
$stockItemConfig = $this->getStockItemConfiguration->execute(
42-
$sku,
43-
$this->getStockId($websiteId)
44-
);
32+
$stockItem = $this->stockRegistry->getStockItem($productId, $websiteId);
4533

46-
$params = [];
47-
$validators = [];
48-
$params['minAllowed'] = $stockItemConfig->getMinSaleQty();
49-
50-
if ($stockItemConfig->getMaxSaleQty()) {
51-
$params['maxAllowed'] = $stockItemConfig->getMaxSaleQty();
52-
}
53-
if ($stockItemConfig->getQtyIncrements() > 0) {
54-
$params['qtyIncrements'] = $stockItemConfig->getQtyIncrements();
55-
}
56-
$validators['validate-item-quantity'] = $params;
57-
58-
return $validators;
59-
} catch (\Exception $e) {
60-
return [];
34+
$params = [];
35+
$validators = [];
36+
$params['minAllowed'] = $stockItem->getMinSaleQty();
37+
if ($stockItem->getMaxSaleQty()) {
38+
$params['maxAllowed'] = $stockItem->getMaxSaleQty();
6139
}
62-
}
63-
64-
/**
65-
* Get Stock ID by Website ID
66-
*
67-
* @param int|null $websiteId
68-
* @return int
69-
* @throws LocalizedException
70-
*/
71-
private function getStockId(?int $websiteId): int
72-
{
73-
if ($websiteId === null) {
74-
$websiteId = $this->storeManager->getWebsite()->getId();
40+
if ($stockItem->getQtyIncrements() > 0) {
41+
$params['qtyIncrements'] = (float) $stockItem->getQtyIncrements();
7542
}
43+
$validators['validate-item-quantity'] = $params;
7644

77-
$websiteCode = $this->storeManager->getWebsite($websiteId)->getCode();
78-
$stock = $this->stockResolver->execute(
79-
SalesChannelInterface::TYPE_WEBSITE,
80-
$websiteCode
81-
);
82-
83-
return (int) $stock->getStockId();
45+
return $validators;
8446
}
8547
}

app/code/Magento/CatalogInventory/Test/Unit/Model/Product/QuantityValidatorTest.php

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,9 @@
77

88
namespace Magento\CatalogInventory\Test\Unit\Model\Product;
99

10+
use Magento\CatalogInventory\Api\StockRegistryInterface;
1011
use Magento\CatalogInventory\Model\Product\QuantityValidator;
11-
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
12-
use Magento\InventoryConfigurationApi\Api\Data\StockItemConfigurationInterface;
13-
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
14-
use Magento\InventoryApi\Api\Data\StockInterface;
15-
use Magento\InventorySalesApi\Api\StockResolverInterface;
16-
use Magento\Store\Api\Data\WebsiteInterface;
17-
use Magento\Store\Model\StoreManagerInterface;
12+
use Magento\CatalogInventory\Api\Data\StockItemInterface;
1813
use PHPUnit\Framework\MockObject\MockObject;
1914
use PHPUnit\Framework\TestCase;
2015

@@ -23,82 +18,55 @@
2318
*/
2419
class QuantityValidatorTest extends TestCase
2520
{
21+
private const PRODUCT_ID = 42;
2622
private const WEBSITE_ID = 1;
27-
private const WEBSITE_CODE = 'base';
28-
private const STOCK_ID = 1;
29-
private const TEST_SKU = 'TEST-SKU';
3023

3124
/**
3225
* @var QuantityValidator
3326
*/
3427
private $quantityValidator;
3528

3629
/**
37-
* @var GetStockItemConfigurationInterface|MockObject
30+
* @var StockRegistryInterface|MockObject
3831
*/
39-
private $getStockItemConfiguration;
40-
41-
/**
42-
* @var StoreManagerInterface|MockObject
43-
*/
44-
private $storeManager;
45-
46-
/**
47-
* @var StockResolverInterface|MockObject
48-
*/
49-
private $stockResolver;
32+
private $stockRegistry;
5033

5134
protected function setUp(): void
5235
{
53-
$this->getStockItemConfiguration = $this->createMock(GetStockItemConfigurationInterface::class);
54-
$this->storeManager = $this->createMock(StoreManagerInterface::class);
55-
$this->stockResolver = $this->createMock(StockResolverInterface::class);
36+
$this->stockRegistry = $this->createMock(StockRegistryInterface::class);
5637

5738
$this->quantityValidator = new QuantityValidator(
58-
$this->getStockItemConfiguration,
59-
$this->storeManager,
60-
$this->stockResolver
39+
$this->stockRegistry
6140
);
6241
}
6342

64-
public function testGetDataWithValidators(): void
43+
public function testGetDataWithMinMaxAndIncrements(): void
6544
{
66-
$website = $this->createMock(WebsiteInterface::class);
67-
$website->method('getCode')->willReturn(self::WEBSITE_CODE);
68-
69-
$stock = $this->createMock(StockInterface::class);
70-
$stock->method('getStockId')->willReturn(self::STOCK_ID);
45+
$stockItem = $this->createMock(StockItemInterface::class);
7146

72-
$stockItemConfiguration = $this->createMock(StockItemConfigurationInterface::class);
73-
$stockItemConfiguration->method('getMinSaleQty')->willReturn(2.0);
74-
$stockItemConfiguration->method('getMaxSaleQty')->willReturn(10.0);
75-
$stockItemConfiguration->method('getQtyIncrements')->willReturn(2.0);
47+
$stockItem->method('getMinSaleQty')
48+
->willReturn(2.0);
7649

77-
// Set expectations
78-
$this->storeManager->expects($this->once())
79-
->method('getWebsite')
80-
->with(self::WEBSITE_ID)
81-
->willReturn($website);
50+
$stockItem->method('getMaxSaleQty')
51+
->willReturn(10.0);
8252

83-
$this->stockResolver->expects($this->once())
84-
->method('execute')
85-
->with(SalesChannelInterface::TYPE_WEBSITE, self::WEBSITE_CODE)
86-
->willReturn($stock);
53+
$stockItem->method('getQtyIncrements')
54+
->willReturn(2.0);
8755

88-
$this->getStockItemConfiguration->expects($this->once())
89-
->method('execute')
90-
->with(self::TEST_SKU, self::STOCK_ID)
91-
->willReturn($stockItemConfiguration);
56+
$this->stockRegistry->expects($this->once())
57+
->method('getStockItem')
58+
->with(self::PRODUCT_ID, self::WEBSITE_ID)
59+
->willReturn($stockItem);
9260

9361
$expected = [
9462
'validate-item-quantity' => [
95-
'minAllowed' => 2,
96-
'maxAllowed' => 10,
63+
'minAllowed' => 2.0,
64+
'maxAllowed' => 10.0,
9765
'qtyIncrements' => 2.0
9866
]
9967
];
10068

101-
$result = $this->quantityValidator->getData(self::TEST_SKU, self::WEBSITE_ID);
69+
$result = $this->quantityValidator->getData(self::PRODUCT_ID, self::WEBSITE_ID);
10270
$this->assertEquals($expected, $result);
10371
}
10472
}

app/code/Magento/CatalogInventory/composer.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
"magento/module-config": "*",
1212
"magento/module-customer": "*",
1313
"magento/module-eav": "*",
14-
"magento/module-inventory-configuration-api": "*",
15-
"magento/module-inventory-sales-api": "*",
1614
"magento/module-quote": "*",
1715
"magento/module-store": "*",
1816
"magento/module-ui": "*"

app/code/Magento/CatalogInventory/etc/module.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
<module name="Magento_CatalogInventory" >
1010
<sequence>
1111
<module name="Magento_Catalog"/>
12-
<module name="Magento_InventoryConfigurationApi"/>
13-
<module name="Magento_InventorySalesApi"/>
1412
</sequence>
1513
</module>
1614
</config>

app/code/Magento/GroupedProduct/ViewModel/ValidateQuantity.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ public function __construct(
2929
/**
3030
* To get the quantity validators
3131
*
32-
* @param string $sku
32+
* @param int $productId
3333
* @param int|null $websiteId
3434
*
3535
* @return string
3636
*/
37-
public function getQuantityValidators(string $sku, int|null $websiteId): string
37+
public function getQuantityValidators(int $productId, int|null $websiteId): string
3838
{
3939
return $this->serializer->serialize(
4040
array_merge(
4141
['validate-grouped-qty' => '#super-product-table'],
42-
$this->productQuantityValidator->getData($sku, $websiteId)
42+
$this->productQuantityValidator->getData($productId, $websiteId)
4343
)
4444
);
4545
}

app/code/Magento/GroupedProduct/view/frontend/templates/product/view/type/grouped.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
title="<?= $escaper->escapeHtmlAttr(__('Qty')) ?>"
5757
class="input-text qty"
5858
data-validate="<?= $escaper->escapeHtmlAttr($viewModel->getQuantityValidators(
59-
$_item->getSku(),
59+
$_item->getId(),
6060
$_item->getWebsiteId()
6161
)) ?>"
6262
data-no-validation-for-zero-qty="true"

0 commit comments

Comments
 (0)