Skip to content

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

Open
wants to merge 32 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
f990b58
Grouped product frontend quantity validation added and code refactor
Mohamed-Asar Dec 15, 2024
5f2b740
Merge branch '2.4-develop' into bug/39479-grouped-product-qty-validation
Mohamed-Asar Dec 18, 2024
52bd8e8
Merge branch '2.4-develop' of https://github.com/Mohamed-Asar/magento…
Mohamed-Asar Dec 20, 2024
94fadc8
Ignore item qty validation if zero qty entered in grouped product
Mohamed-Asar Dec 20, 2024
9ac7ef5
Merge branch 'bug/39479-grouped-product-qty-validation' of https://gi…
Mohamed-Asar Dec 20, 2024
eea8ae5
Merge branch '2.4-develop' of https://github.com/Mohamed-Asar/magento…
Mohamed-Asar Dec 30, 2024
a192377
Unit test & static test cases issues fixed
Mohamed-Asar Dec 30, 2024
ceb8597
Static test fixed
Mohamed-Asar Dec 30, 2024
3e0c5a7
Indent issue fixed
Mohamed-Asar Dec 31, 2024
4213997
Merge branch '2.4-develop' into bug/39479-grouped-product-qty-validation
engcom-Hotel Jan 23, 2025
1ca6c6d
Code review changes
Mohamed-Asar Jan 23, 2025
96bfa59
Merge branch '2.4-develop' of https://github.com/Mohamed-Asar/magento…
Mohamed-Asar Feb 18, 2025
cc2409a
Merge branch '2.4-develop' of https://github.com/Mohamed-Asar/magento…
Mohamed-Asar Mar 5, 2025
baeb177
Removed deprecated methods, get data using MSI and unit test added
Mohamed-Asar Mar 7, 2025
9a46f52
Static test case fix
Mohamed-Asar Mar 10, 2025
2e64918
Merge branch '2.4-develop' into bug/39479-grouped-product-qty-validation
engcom-Hotel Mar 11, 2025
b0ad802
Merge branch '2.4-develop' into bug/39479-grouped-product-qty-validation
engcom-Hotel Mar 14, 2025
7545bdb
Merge branch '2.4-develop' into bug/39479-grouped-product-qty-validation
engcom-Hotel Mar 26, 2025
841ffaa
Merge branch '2.4-develop' of https://github.com/Mohamed-Asar/magento…
Mohamed-Asar Apr 3, 2025
559d0f8
Test case failures fix
Mohamed-Asar Apr 3, 2025
8578cb7
Merge branch '2.4-develop' of https://github.com/Mohamed-Asar/magento…
Mohamed-Asar Apr 7, 2025
92c7ad3
Composer dependency added
Mohamed-Asar Apr 7, 2025
0a8daf1
Mftf test case fix for min qty validation
Mohamed-Asar Apr 9, 2025
4a9d850
Merge branch '2.4-develop' of https://github.com/Mohamed-Asar/magento…
Mohamed-Asar Apr 16, 2025
203ba70
Reverted the msi modules approach
Mohamed-Asar Apr 16, 2025
13f8551
Merge branch '2.4-develop' into bug/39479-grouped-product-qty-validation
engcom-Hotel Apr 25, 2025
7afb893
Merge branch '2.4-develop' into bug/39479-grouped-product-qty-validation
engcom-Hotel Apr 28, 2025
42d9406
Code review changes
Mohamed-Asar May 23, 2025
ddcfdec
Null assigment removed
Mohamed-Asar May 23, 2025
7aed289
Merge branch '2.4-develop' into bug/39479-grouped-product-qty-validation
engcom-Hotel May 27, 2025
5330948
Conflicts fixed
Mohamed-Asar May 28, 2025
2fe7d64
Merge branch '2.4-develop' into bug/39479-grouped-product-qty-validation
engcom-Dash Jun 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<argument name="value" value="Yes"/>
</actionGroup>
<actionGroup ref="AdminFillAdvancedInventoryQtyActionGroup" stepKey="fillProductQty">
<argument name="qty" value="5"/>
<argument name="qty" value="8"/>
</actionGroup>
<comment userInput="Comment is added to preserve the step key for backward compatibility" stepKey="uncheckMiniQtyCheckBox"/>
<actionGroup ref="AdminSetMinAllowedQtyForProductActionGroup" stepKey="fillMiniAllowedQty">
Expand Down Expand Up @@ -111,17 +111,17 @@
<argument name="productQty" value="4"/>
</actionGroup>
<actionGroup ref="StorefrontAddProductToCartWithQtyActionGroup" stepKey="addAdditionalProductToCart">
<argument name="productQty" value="1"/>
<argument name="productQty" value="4"/>
</actionGroup>
<comment userInput="Comment is added to preserve the step key for backward compatibility" stepKey="seeSuccessSaveMessage"/>
<seeElement selector="{{StorefrontMinicartSection.quantity(6)}}" stepKey="seeAddedProductQuantityInCart"/>
<actionGroup ref="StorefrontClickOnMiniCartActionGroup" stepKey="clickOnMiniCart"/>
<executeJS function="return {{SimpleProduct.price}} * 5" stepKey="expectedCartSubtotal"/>
<executeJS function="return {{SimpleProduct.price}} * 8" stepKey="expectedCartSubtotal"/>
<actionGroup ref="AssertStorefrontMiniCartItemsActionGroup" stepKey="seeProductNameInMiniCart">
<argument name="productName" value="{{SimpleProduct.name}}"/>
<argument name="productPrice" value="{{SimpleProduct.price}}"/>
<argument name="cartSubtotal" value="{$expectedCartSubtotal}" />
<argument name="qty" value="5"/>
<argument name="qty" value="8"/>
</actionGroup>
<comment userInput="Comment is added to preserve the step key for backward compatibility" stepKey="seeProductPriceInMiniCart"/>
<comment userInput="Comment is added to preserve the step key for backward compatibility" stepKey="seeCheckOutButtonInMiniCart"/>
Expand Down
37 changes: 15 additions & 22 deletions app/code/Magento/CatalogInventory/Block/Plugin/ProductView.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,42 @@
*/
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
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not done yet, still cant see ObjectManager usages

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@engcom-Hotel i tried that, but the static test failed due to the object being initialized as null, so I had to revert the changes.

image

) {
$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,51 @@
<?php
/**
* Copyright 2024 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\CatalogInventory\Model\Product;

use Magento\CatalogInventory\Api\StockRegistryInterface;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interface StockRegistryInterface has been deprecated. Please use alternate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 ?

Copy link
Contributor

@engcom-Hotel engcom-Hotel Jan 24, 2025

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refer to this repo: https://github.com/magento/inventory

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);

if (!$stockItem) {
return [];
}

$params = [];
$validators = [];
$params['minAllowed'] = $stockItem->getMinSaleQty();
if ($stockItem->getMaxSaleQty()) {
$params['maxAllowed'] = $stockItem->getMaxSaleQty();
Comment on lines +40 to +42
Copy link
Preview

Copilot AI May 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider explicitly casting 'minAllowed' and 'maxAllowed' to a consistent numeric type (e.g., using (float)) to ensure uniform behavior, or document the expected types for clarity.

Suggested change
$params['minAllowed'] = $stockItem->getMinSaleQty();
if ($stockItem->getMaxSaleQty()) {
$params['maxAllowed'] = $stockItem->getMaxSaleQty();
$params['minAllowed'] = (float) $stockItem->getMinSaleQty();
if ($stockItem->getMaxSaleQty()) {
$params['maxAllowed'] = (float) $stockItem->getMaxSaleQty();

Copilot uses AI. Check for mistakes.

}
if ($stockItem->getQtyIncrements() > 0) {
Copy link
Preview

Copilot AI May 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider explicitly verifying that the return value of getQtyIncrements() is numeric before performing the comparison to ensure type safety.

Suggested change
if ($stockItem->getQtyIncrements() > 0) {
if (is_numeric($stockItem->getQtyIncrements()) && $stockItem->getQtyIncrements() > 0) {

Copilot uses AI. Check for mistakes.

$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,132 @@
<?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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Covering some edge case tests to ensure the code behaves correctly under unusual or extreme conditions. This could include scenarios like:

  • getMinSaleQty, getMaxSaleQty, or getQtyIncrements returning null or invalid values.
  • Testing with a productId or websiteId that does not exist in the stock registry.

{
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);
}

public function testReturnsEmptyArrayForNonExistentProductOrWebsite(): void
{
$this->stockRegistry->expects($this->once())
->method('getStockItem')
->with(self::PRODUCT_ID, self::WEBSITE_ID)
->willReturn(null);

$result = $this->quantityValidator->getData(self::PRODUCT_ID, self::WEBSITE_ID);
$this->assertSame([], $result, 'Should return empty array when StockItem is not found');
}

public function testHandlesNullValuesFromStockItem(): void
{
$stockItem = $this->createMock(StockItemInterface::class);
$stockItem->method('getMinSaleQty')
->willReturn(null);
$stockItem->method('getMaxSaleQty')
->willReturn(null);
$stockItem->method('getQtyIncrements')
->willReturn(null);

$this->stockRegistry->expects($this->once())
->method('getStockItem')
->with(self::PRODUCT_ID, self::WEBSITE_ID)
->willReturn($stockItem);

$expected = [
'validate-item-quantity' => [
'minAllowed' => null
],
];
$result = $this->quantityValidator->getData(self::PRODUCT_ID, self::WEBSITE_ID);
$this->assertEquals($expected, $result);
}

public function testHandlesInvalidValuesFromStockItem(): void
{
$stockItem = $this->createMock(StockItemInterface::class);
$stockItem->method('getMinSaleQty')
->willReturn('not-a-number');
$stockItem->method('getMaxSaleQty')
->willReturn(-5);
$stockItem->method('getQtyIncrements')
->willReturn(false);

$this->stockRegistry->expects($this->once())
->method('getStockItem')
->with(self::PRODUCT_ID, self::WEBSITE_ID)
->willReturn($stockItem);

$expected = [
'validate-item-quantity' => [
'minAllowed' => 'not-a-number',
'maxAllowed' => -5
],
];
$result = $this->quantityValidator->getData(self::PRODUCT_ID, self::WEBSITE_ID);
$this->assertEquals($expected, $result);
}
}
Loading