-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Grouped product frontend quantity validation added and code refactor #39480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.4-develop
Are you sure you want to change the base?
Changes from all commits
f990b58
5f2b740
52bd8e8
94fadc8
9ac7ef5
eea8ae5
a192377
ceb8597
3e0c5a7
4213997
1ca6c6d
96bfa59
cc2409a
baeb177
9a46f52
2e64918
b0ad802
7545bdb
841ffaa
559d0f8
8578cb7
92c7ad3
0a8daf1
4a9d850
203ba70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,46 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
* Copyright 2015 Adobe | ||
* All Rights Reserved. | ||
*/ | ||
namespace Magento\CatalogInventory\Block\Plugin; | ||
|
||
use Magento\CatalogInventory\Api\StockRegistryInterface; | ||
use Magento\Catalog\Block\Product\View; | ||
use Magento\CatalogInventory\Model\Product\QuantityValidator; | ||
|
||
class ProductView | ||
{ | ||
/** | ||
* @var StockRegistryInterface | ||
* @var QuantityValidator | ||
*/ | ||
private $stockRegistry; | ||
private $productQuantityValidator; | ||
|
||
/** | ||
* @param StockRegistryInterface $stockRegistry | ||
* @param QuantityValidator $productQuantityValidator | ||
*/ | ||
public function __construct( | ||
StockRegistryInterface $stockRegistry | ||
QuantityValidator $productQuantityValidator | ||
) { | ||
$this->stockRegistry = $stockRegistry; | ||
$this->productQuantityValidator = $productQuantityValidator; | ||
} | ||
|
||
/** | ||
* Adds quantities validator. | ||
* | ||
* @param \Magento\Catalog\Block\Product\View $block | ||
* @param View $block | ||
* @param array $validators | ||
* @return array | ||
*/ | ||
public function afterGetQuantityValidators( | ||
\Magento\Catalog\Block\Product\View $block, | ||
View $block, | ||
array $validators | ||
) { | ||
$stockItem = $this->stockRegistry->getStockItem( | ||
$block->getProduct()->getId(), | ||
$block->getProduct()->getStore()->getWebsiteId() | ||
return array_merge( | ||
$validators, | ||
$this->productQuantityValidator->getData( | ||
$block->getProduct()->getId(), | ||
$block->getProduct()->getStore()->getWebsiteId() | ||
) | ||
); | ||
|
||
$params = []; | ||
if ($stockItem->getMaxSaleQty()) { | ||
$params['maxAllowed'] = (float)$stockItem->getMaxSaleQty(); | ||
} | ||
if ($stockItem->getQtyIncrements() > 0) { | ||
$params['qtyIncrements'] = (float)$stockItem->getQtyIncrements(); | ||
} | ||
$validators['validate-item-quantity'] = $params; | ||
|
||
return $validators; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/** | ||
* Copyright 2024 Adobe | ||
* All Rights Reserved. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogInventory\Model\Product; | ||
|
||
use Magento\CatalogInventory\Api\StockRegistryInterface; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. interface StockRegistryInterface has been deprecated. Please use alternate. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @engcom-Hotel The alternative for that interface is the MSI module. However, some users might have the MSI module disabled, can i use that ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes @Mohamed-Asar you can use that. BTW its not recommended to disable the MSI module. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @engcom-Hotel MSI module is not included in this repo. how do i proceed ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please refer to this repo: https://github.com/magento/inventory There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @engcom-Hotel I used MSI and remvoed the deprecated methods, please review |
||
|
||
class QuantityValidator | ||
{ | ||
/** | ||
* @param StockRegistryInterface $stockRegistry | ||
*/ | ||
public function __construct( | ||
private readonly StockRegistryInterface $stockRegistry | ||
) { | ||
} | ||
|
||
/** | ||
* To get quantity validators | ||
* | ||
* @param int $productId | ||
* @param int|null $websiteId | ||
* | ||
* @return array | ||
*/ | ||
public function getData(int $productId, int|null $websiteId): array | ||
{ | ||
$stockItem = $this->stockRegistry->getStockItem($productId, $websiteId); | ||
|
||
$params = []; | ||
$validators = []; | ||
$params['minAllowed'] = $stockItem->getMinSaleQty(); | ||
if ($stockItem->getMaxSaleQty()) { | ||
$params['maxAllowed'] = $stockItem->getMaxSaleQty(); | ||
} | ||
if ($stockItem->getQtyIncrements() > 0) { | ||
$params['qtyIncrements'] = (float) $stockItem->getQtyIncrements(); | ||
} | ||
$validators['validate-item-quantity'] = $params; | ||
|
||
return $validators; | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* Copyright 2025 Adobe | ||
* All Rights Reserved. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogInventory\Test\Unit\Model\Product; | ||
|
||
use Magento\CatalogInventory\Api\StockRegistryInterface; | ||
use Magento\CatalogInventory\Model\Product\QuantityValidator; | ||
use Magento\CatalogInventory\Api\Data\StockItemInterface; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \Magento\CatalogInventory\Model\Product\QuantityValidator | ||
*/ | ||
class QuantityValidatorTest extends TestCase | ||
{ | ||
private const PRODUCT_ID = 42; | ||
private const WEBSITE_ID = 1; | ||
|
||
/** | ||
* @var QuantityValidator | ||
*/ | ||
private $quantityValidator; | ||
|
||
/** | ||
* @var StockRegistryInterface|MockObject | ||
*/ | ||
private $stockRegistry; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->stockRegistry = $this->createMock(StockRegistryInterface::class); | ||
|
||
$this->quantityValidator = new QuantityValidator( | ||
$this->stockRegistry | ||
); | ||
} | ||
|
||
public function testGetDataWithMinMaxAndIncrements(): void | ||
{ | ||
$stockItem = $this->createMock(StockItemInterface::class); | ||
|
||
$stockItem->method('getMinSaleQty') | ||
->willReturn(2.0); | ||
|
||
$stockItem->method('getMaxSaleQty') | ||
->willReturn(10.0); | ||
|
||
$stockItem->method('getQtyIncrements') | ||
->willReturn(2.0); | ||
|
||
$this->stockRegistry->expects($this->once()) | ||
->method('getStockItem') | ||
->with(self::PRODUCT_ID, self::WEBSITE_ID) | ||
->willReturn($stockItem); | ||
|
||
$expected = [ | ||
'validate-item-quantity' => [ | ||
'minAllowed' => 2.0, | ||
'maxAllowed' => 10.0, | ||
'qtyIncrements' => 2.0 | ||
] | ||
]; | ||
|
||
$result = $this->quantityValidator->getData(self::PRODUCT_ID, self::WEBSITE_ID); | ||
$this->assertEquals($expected, $result); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
/** | ||
* Copyright 2024 Adobe | ||
* All Rights Reserved. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GroupedProduct\ViewModel; | ||
|
||
use Magento\CatalogInventory\Model\Product\QuantityValidator; | ||
use Magento\Framework\Serialize\Serializer\Json; | ||
use Magento\Framework\View\Element\Block\ArgumentInterface; | ||
|
||
/** | ||
* ViewModel for Grouped Products Block | ||
*/ | ||
class ValidateQuantity implements ArgumentInterface | ||
{ | ||
/** | ||
* @param Json $serializer | ||
* @param QuantityValidator $productQuantityValidator | ||
*/ | ||
public function __construct( | ||
private readonly Json $serializer, | ||
private readonly QuantityValidator $productQuantityValidator, | ||
) { | ||
} | ||
|
||
/** | ||
* To get the quantity validators | ||
* | ||
* @param int $productId | ||
* @param int|null $websiteId | ||
* | ||
* @return string | ||
*/ | ||
public function getQuantityValidators(int $productId, int|null $websiteId): string | ||
{ | ||
return $this->serializer->serialize( | ||
array_merge( | ||
['validate-grouped-qty' => '#super-product-table'], | ||
$this->productQuantityValidator->getData($productId, $websiteId) | ||
) | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use ObjectManager to initialize as this change is BiC.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done