Skip to content

Commit f990b58

Browse files
committed
Grouped product frontend quantity validation added and code refactor
1 parent 7d5e390 commit f990b58

File tree

6 files changed

+151
-37
lines changed

6 files changed

+151
-37
lines changed
Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\CatalogInventory\Block\Plugin;
77

8-
use Magento\CatalogInventory\Api\StockRegistryInterface;
8+
use Magento\CatalogInventory\Model\Product\QuantityValidator;
99

1010
class ProductView
1111
{
1212
/**
13-
* @var StockRegistryInterface
13+
* @var QuantityValidator
1414
*/
15-
private $stockRegistry;
15+
private $productQuantityValidator;
1616

1717
/**
18-
* @param StockRegistryInterface $stockRegistry
18+
* @param QuantityValidator $productQuantityValidator
1919
*/
2020
public function __construct(
21-
StockRegistryInterface $stockRegistry
21+
QuantityValidator $productQuantityValidator
2222
) {
23-
$this->stockRegistry = $stockRegistry;
23+
$this->productQuantityValidator = $productQuantityValidator;
2424
}
2525

2626
/**
@@ -34,20 +34,12 @@ public function afterGetQuantityValidators(
3434
\Magento\Catalog\Block\Product\View $block,
3535
array $validators
3636
) {
37-
$stockItem = $this->stockRegistry->getStockItem(
38-
$block->getProduct()->getId(),
39-
$block->getProduct()->getStore()->getWebsiteId()
37+
return array_merge(
38+
$validators,
39+
$this->productQuantityValidator->getData(
40+
$block->getProduct()->getId(),
41+
$block->getProduct()->getStore()->getWebsiteId()
42+
)
4043
);
41-
42-
$params = [];
43-
if ($stockItem->getMaxSaleQty()) {
44-
$params['maxAllowed'] = (float)$stockItem->getMaxSaleQty();
45-
}
46-
if ($stockItem->getQtyIncrements() > 0) {
47-
$params['qtyIncrements'] = (float)$stockItem->getQtyIncrements();
48-
}
49-
$validators['validate-item-quantity'] = $params;
50-
51-
return $validators;
5244
}
5345
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogInventory\Model\Product;
9+
10+
use Magento\CatalogInventory\Api\StockRegistryInterface;
11+
12+
class QuantityValidator
13+
{
14+
/**
15+
* @var StockRegistryInterface
16+
*/
17+
private $stockRegistry;
18+
19+
/**
20+
* @param StockRegistryInterface $stockRegistry
21+
*/
22+
public function __construct(
23+
StockRegistryInterface $stockRegistry
24+
) {
25+
$this->stockRegistry = $stockRegistry;
26+
}
27+
28+
/**
29+
* To get quantity validators
30+
*
31+
* @param int $productId
32+
* @param int $websiteId
33+
*
34+
* @return array
35+
*/
36+
public function getData($productId, $websiteId): array
37+
{
38+
$stockItem = $this->stockRegistry->getStockItem($productId, $websiteId);
39+
40+
$params = [];
41+
$validators = [];
42+
$params['minAllowed'] = (float)$stockItem->getMinSaleQty();
43+
if ($stockItem->getMaxSaleQty()) {
44+
$params['maxAllowed'] = (float)$stockItem->getMaxSaleQty();
45+
}
46+
if ($stockItem->getQtyIncrements() > 0) {
47+
$params['qtyIncrements'] = (float)$stockItem->getQtyIncrements();
48+
}
49+
$validators['validate-item-quantity'] = $params;
50+
51+
return $validators;
52+
}
53+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GroupedProduct\ViewModel;
9+
10+
use Magento\CatalogInventory\Model\Product\QuantityValidator;
11+
use Magento\Framework\Serialize\Serializer\Json;
12+
use Magento\Framework\View\Element\Block\ArgumentInterface;
13+
14+
/**
15+
* ViewModel for Grouped Products Block
16+
*/
17+
class ValidateQuantity implements ArgumentInterface
18+
{
19+
/**
20+
* @var Json
21+
*/
22+
private $serializer;
23+
24+
/**
25+
* @var QuantityValidator
26+
*/
27+
private $productQuantityValidator;
28+
29+
/**
30+
* @param Json $serializer
31+
* @param QuantityValidator $productQuantityValidator
32+
*/
33+
public function __construct(
34+
Json $serializer,
35+
QuantityValidator $productQuantityValidator,
36+
) {
37+
$this->serializer = $serializer;
38+
$this->productQuantityValidator = $productQuantityValidator;
39+
}
40+
41+
/**
42+
* To get the quantity validators
43+
*
44+
* @param int $productId
45+
* @param int $websiteId
46+
*
47+
* @return string
48+
*/
49+
public function getQuantityValidators($productId, $websiteId): string
50+
{
51+
return $this->serializer->serialize(
52+
array_merge(
53+
['validate-grouped-qty' => '#super-product-table'],
54+
$this->productQuantityValidator->getData($productId, $websiteId)
55+
)
56+
);
57+
}
58+
}

app/code/Magento/GroupedProduct/view/frontend/layout/catalog_product_view_type_grouped.xml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
<?xml version="1.0"?>
22
<!--
3-
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
6-
*/
3+
/**
4+
* Copyright 2011 Adobe
5+
* All Rights Reserved.
6+
*/
77
-->
88
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
99
<body>
1010
<attribute name="class" value="page-product-grouped"/>
1111
<referenceContainer name="product.info.form.content">
12-
<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"/>
12+
<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">
13+
<arguments>
14+
<argument name="validateQuantityViewModel" xsi:type="object">Magento\GroupedProduct\ViewModel\ValidateQuantity</argument>
15+
</arguments>
16+
</block>
1317
<container name="product.info.grouped.extra" after="product.info.grouped" before="product.info.grouped" as="product_type_data_extra" label="Product Extra Info"/>
1418
</referenceContainer>
1519
<referenceContainer name="product.info.grouped.extra">

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66

77
/**
@@ -11,10 +11,13 @@
1111
* @var $block \Magento\GroupedProduct\Block\Product\View\Type\Grouped
1212
*/
1313
?>
14-
<?php $block->setPreconfiguredValue(); ?>
15-
<?php $_product = $block->getProduct(); ?>
16-
<?php $_associatedProducts = $block->getAssociatedProducts(); ?>
17-
<?php $_hasAssociatedProducts = count($_associatedProducts) > 0; ?>
14+
<?php
15+
$block->setPreconfiguredValue();
16+
$_product = $block->getProduct();
17+
$_associatedProducts = $block->getAssociatedProducts();
18+
$_hasAssociatedProducts = count($_associatedProducts) > 0;
19+
$viewModel = $block->getData('validateQuantityViewModel');
20+
?>
1821

1922
<div class="table-wrapper grouped">
2023
<table class="table data grouped"
@@ -52,7 +55,7 @@
5255
value="<?= $block->escapeHtmlAttr($_item->getQty() * 1) ?>"
5356
title="<?= $block->escapeHtmlAttr(__('Qty')) ?>"
5457
class="input-text qty"
55-
data-validate="{'validate-grouped-qty':'#super-product-table'}"
58+
data-validate="<?= $block->escapeHtmlAttr($viewModel->getQuantityValidators($_item->getId(), $_item->getWebsiteId())) ?>"
5659
data-errors-message-box="#validation-message-box"/>
5760
</div>
5861
<?php else: ?>

app/code/Magento/Wishlist/view/frontend/layout/wishlist_index_configure_type_grouped.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2011 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
99
<body>
1010
<attribute name="class" value="page-product-grouped"/>
1111
<referenceContainer name="product.info.form.content">
12-
<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"/>
12+
<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">
13+
<arguments>
14+
<argument name="validateQuantityViewModel" xsi:type="object">Magento\GroupedProduct\ViewModel\ValidateQuantity</argument>
15+
</arguments>
16+
</block>
1317
<container name="product.info.grouped.extra" after="product.info.grouped" before="product.info.grouped" as="product_type_data_extra" label="Product Extra Info"/>
1418
</referenceContainer>
1519
</body>

0 commit comments

Comments
 (0)