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 5 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
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 2011 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 2011 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;
}
}
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 2011 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 2011 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,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2011 Adobe
* All Rights Reserved.
*/

/**
Expand All @@ -11,10 +11,13 @@
* @var $block \Magento\GroupedProduct\Block\Product\View\Type\Grouped
*/
?>
<?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"
Expand Down Expand Up @@ -52,7 +55,8 @@
value="<?= $block->escapeHtmlAttr($_item->getQty() * 1) ?>"
title="<?= $block->escapeHtmlAttr(__('Qty')) ?>"
class="input-text qty"
data-validate="{'validate-grouped-qty':'#super-product-table'}"
data-validate="<?= $block->escapeHtmlAttr($viewModel->getQuantityValidators($_item->getId(), $_item->getWebsiteId())) ?>"
data-no-validation-for-zero-qty="true"
data-errors-message-box="#validation-message-box"/>
</div>
<?php else: ?>
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 2011 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