Skip to content
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

Grouped product frontend quantity validation added and code refactor #39480

Open
wants to merge 9 commits into
base: 2.4-develop
Choose a base branch
from
36 changes: 14 additions & 22 deletions app/code/Magento/CatalogInventory/Block/Plugin/ProductView.php
Original file line number Diff line number Diff line change
@@ -1,26 1,26 @@
<?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\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;
}

/**
Expand All @@ -34,20 34,12 @@ public function afterGetQuantityValidators(
\Magento\Catalog\Block\Product\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,53 @@
<?php
/**
* Copyright 2024 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\CatalogInventory\Model\Product;

use Magento\CatalogInventory\Api\StockRegistryInterface;

class QuantityValidator
{
/**
* @var StockRegistryInterface
*/
private $stockRegistry;

/**
* @param StockRegistryInterface $stockRegistry
*/
public function __construct(
StockRegistryInterface $stockRegistry
) {
$this->stockRegistry = $stockRegistry;
}

/**
* To get quantity validators
*
* @param int $productId
* @param int $websiteId
*
* @return array
*/
public function getData($productId, $websiteId): array
{
$stockItem = $this->stockRegistry->getStockItem($productId, $websiteId);

$params = [];
$validators = [];
$params['minAllowed'] = (float)$stockItem->getMinSaleQty();
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
@@ -1,7 1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2015 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

Expand All @@ -12,6 12,7 @@
use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\CatalogInventory\Block\Plugin\ProductView;
use Magento\CatalogInventory\Model\Product\QuantityValidator;
use Magento\CatalogInventory\Model\Stock\Item;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Model\Store;
Expand All @@ -35,6 36,11 @@ class ProductViewTest extends TestCase
*/
protected $stockRegistry;

/**
* @var QuantityValidator|MockObject
*/
protected $productQuantityValidator;

protected function setUp(): void
{
$objectManager = new ObjectManager($this);
Expand All @@ -47,10 53,17 @@ protected function setUp(): void
$this->stockRegistry = $this->getMockBuilder(StockRegistryInterface::class)
->getMock();

$this->productQuantityValidator = $objectManager->getObject(
QuantityValidator::class,
[
'stockRegistry' => $this->stockRegistry
]
);

$this->block = $objectManager->getObject(
ProductView::class,
[
'stockRegistry' => $this->stockRegistry
'productQuantityValidator' => $this->productQuantityValidator
]
);
}
Expand All @@ -59,6 72,7 @@ public function testAfterGetQuantityValidators()
{
$result = [
'validate-item-quantity' => [
'minAllowed' => 1.0,
'maxAllowed' => 5.0,
'qtyIncrements' => 3.0
]
Expand Down Expand Up @@ -86,9 100,9 @@ public function testAfterGetQuantityValidators()
->method('getStockItem')
->with('productId', 'websiteId')
->willReturn($this->stockItem);
$this->stockItem->expects($this->any())->method('getMinSaleQty')->willReturn(1);
$this->stockItem->expects($this->any())->method('getMaxSaleQty')->willReturn(5);
$this->stockItem->expects($this->any())->method('getQtyIncrements')->willReturn(3);

$this->assertEquals($result, $this->block->afterGetQuantityValidators($productViewBlock, $validators));
}
}
58 changes: 58 additions & 0 deletions app/code/Magento/GroupedProduct/ViewModel/ValidateQuantity.php
Original file line number Diff line number Diff line change
@@ -0,0 1,58 @@
<?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
{
/**
* @var Json
*/
private $serializer;

/**
* @var QuantityValidator
*/
private $productQuantityValidator;

/**
* @param Json $serializer
* @param QuantityValidator $productQuantityValidator
*/
public function __construct(
Json $serializer,
QuantityValidator $productQuantityValidator,
) {
$this->serializer = $serializer;
$this->productQuantityValidator = $productQuantityValidator;
}

/**
* To get the quantity validators
*
* @param int $productId
* @param int $websiteId
*
* @return string
*/
public function getQuantityValidators($productId, $websiteId): string
{
return $this->serializer->serialize(
array_merge(
['validate-grouped-qty' => '#super-product-table'],
$this->productQuantityValidator->getData($productId, $websiteId)
)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 1,19 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Copyright 2015 Adobe
* All Rights Reserved.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<attribute name="class" value="page-product-grouped"/>
<referenceContainer name="product.info.form.content">
<block class="Magento\GroupedProduct\Block\Product\View\Type\Grouped" name="product.info.grouped" before="product.info.addtocart" template="Magento_GroupedProduct::product/view/type/grouped.phtml"/>
<block class="Magento\GroupedProduct\Block\Product\View\Type\Grouped" name="product.info.grouped" before="product.info.addtocart" template="Magento_GroupedProduct::product/view/type/grouped.phtml">
<arguments>
<argument name="validateQuantityViewModel" xsi:type="object">Magento\GroupedProduct\ViewModel\ValidateQuantity</argument>
</arguments>
</block>
<container name="product.info.grouped.extra" after="product.info.grouped" before="product.info.grouped" as="product_type_data_extra" label="Product Extra Info"/>
</referenceContainer>
<referenceContainer name="product.info.grouped.extra">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 1,34 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2015 Adobe
* All Rights Reserved.
*/

/**
* Grouped product data template
*
* @var $block \Magento\Catalog\Block\Product\View\BaseImage
* @var $block \Magento\GroupedProduct\Block\Product\View\Type\Grouped
* @var $escaper \Magento\Framework\Escaper
*/
?>
<?php $block->setPreconfiguredValue(); ?>
<?php $_product = $block->getProduct(); ?>
<?php $_associatedProducts = $block->getAssociatedProducts(); ?>
<?php $_hasAssociatedProducts = count($_associatedProducts) > 0; ?>
<?php
$block->setPreconfiguredValue();
$_product = $block->getProduct();
$_associatedProducts = $block->getAssociatedProducts();
$_hasAssociatedProducts = count($_associatedProducts) > 0;
$viewModel = $block->getData('validateQuantityViewModel');
?>

<div class="table-wrapper grouped">
<table class="table data grouped"
id="super-product-table"
data-mage-init='{ "Magento_GroupedProduct/js/product-ids-resolver": {} }'>
<caption class="table-caption"><?= $block->escapeHtml(__('Grouped product items')) ?></caption>
<caption class="table-caption"><?= $escaper->escapeHtml(__('Grouped product items')) ?></caption>
<thead>
<tr>
<th class="col item" scope="col"><?= $block->escapeHtml(__('Product Name')) ?></th>
<th class="col item" scope="col"><?= $escaper->escapeHtml(__('Product Name')) ?></th>
<?php if ($_product->isSaleable()): ?>
<th class="col qty" scope="col"><?= $block->escapeHtml(__('Qty')) ?></th>
<th class="col qty" scope="col"><?= $escaper->escapeHtml(__('Qty')) ?></th>
<?php endif; ?>
</tr>
</thead>
Expand All @@ -34,30 37,34 @@
<tbody>
<?php foreach ($_associatedProducts as $_item): ?>
<tr>
<td data-th="<?= $block->escapeHtml(__('Product Name')) ?>" class="col item">
<strong class="product-item-name"><?= $block->escapeHtml($_item->getName()) ?></strong>
<td data-th="<?= $escaper->escapeHtml(__('Product Name')) ?>" class="col item">
<strong class="product-item-name"><?= $escaper->escapeHtml($_item->getName()) ?></strong>
<?php if ($block->getCanShowProductPrice($_product)): ?>
<?php if ($block->getCanShowProductPrice($_item)): ?>
<?= /* @noEscape */ $block->getProductPrice($_item) ?>
<?php endif; ?>
<?php endif; ?>
</td>
<?php if ($_product->isSaleable()): ?>
<td data-th="<?= $block->escapeHtml(__('Qty')) ?>" class="col qty">
<td data-th="<?= $escaper->escapeHtml(__('Qty')) ?>" class="col qty">
<?php if ($_item->isSaleable()): ?>
<div class="control qty">
<input type="number"
name="super_group[<?= $block->escapeHtmlAttr($_item->getId()) ?>]"
data-selector="super_group[<?= $block->escapeHtmlAttr($_item->getId()) ?>]"
value="<?= $block->escapeHtmlAttr($_item->getQty() * 1) ?>"
title="<?= $block->escapeHtmlAttr(__('Qty')) ?>"
name="super_group[<?= $escaper->escapeHtmlAttr($_item->getId()) ?>]"
data-selector="super_group[<?= $escaper->escapeHtmlAttr($_item->getId()) ?>]"
value="<?= $escaper->escapeHtmlAttr($_item->getQty() * 1) ?>"
title="<?= $escaper->escapeHtmlAttr(__('Qty')) ?>"
class="input-text qty"
data-validate="{'validate-grouped-qty':'#super-product-table'}"
data-validate="<?= $escaper->escapeHtmlAttr($viewModel->getQuantityValidators(
$_item->getId(),
$_item->getWebsiteId()
)) ?>"
data-no-validation-for-zero-qty="true"
data-errors-message-box="#validation-message-box"/>
</div>
<?php else: ?>
<div class="stock unavailable" title="<?= $block->escapeHtmlAttr(__('Availability')) ?>">
<span><?= $block->escapeHtml(__('Out of stock')) ?></span>
<div class="stock unavailable" title="<?= $escaper->escapeHtmlAttr(__('Availability')) ?>">
<span><?= $escaper->escapeHtml(__('Out of stock')) ?></span>
</div>
<?php endif; ?>
</td>
Expand Down Expand Up @@ -85,7 92,7 @@
<tr>
<td class="unavailable"
colspan="<?php if ($_product->isSaleable()): ?>4<?php else: ?>3<?php endif; ?>">
<?= $block->escapeHtml(__('No options of this product are available.')) ?>
<?= $escaper->escapeHtml(__('No options of this product are available.')) ?>
</td>
</tr>
</tbody>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 1,19 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2015 Adobe
* All Rights Reserved.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<attribute name="class" value="page-product-grouped"/>
<referenceContainer name="product.info.form.content">
<block class="Magento\GroupedProduct\Block\Product\View\Type\Grouped" name="product.info.grouped" before="product.info.addtocart" template="Magento_GroupedProduct::product/view/type/grouped.phtml"/>
<block class="Magento\GroupedProduct\Block\Product\View\Type\Grouped" name="product.info.grouped" before="product.info.addtocart" template="Magento_GroupedProduct::product/view/type/grouped.phtml">
<arguments>
<argument name="validateQuantityViewModel" xsi:type="object">Magento\GroupedProduct\ViewModel\ValidateQuantity</argument>
</arguments>
</block>
<container name="product.info.grouped.extra" after="product.info.grouped" before="product.info.grouped" as="product_type_data_extra" label="Product Extra Info"/>
</referenceContainer>
</body>
Expand Down
3 changes: 3 additions & 0 deletions lib/web/mage/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 1643,9 @@ define([
isQtyIncrementsValid = typeof params.qtyIncrements === 'undefined' ||
resolveModulo(qty, $.mage.parseNumber(params.qtyIncrements)) === 0.0;

if ($(element).data('no-validation-for-zero-qty') === true && qty === 0) {
return true;
}
result = qty > 0;

if (result === false) {
Expand Down