Skip to content

Commit a12e8f3

Browse files
committed
MC-42249: No form validation for negative quantity on bundle products
- Added the validation for quantity text box in product detail page for bundle products.
1 parent 867f3e3 commit a12e8f3

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Bundle\ViewModel;
9+
10+
use Magento\CatalogInventory\Model\StockRegistry;
11+
use Magento\Framework\View\Element\Block\ArgumentInterface;
12+
use Magento\Catalog\Block\Product\View as ProductView;
13+
14+
/**
15+
* ViewModel for Bundle Option Block
16+
*/
17+
18+
class ValidateQuantity implements ArgumentInterface
19+
{
20+
/**
21+
* @var ProductView
22+
*/
23+
private $productView;
24+
25+
/**
26+
* @param ProductView $productView
27+
*/
28+
public function __construct(ProductView $productView)
29+
{
30+
$this->productView = $productView;
31+
}
32+
33+
public function getQuantityValidators(): array
34+
{
35+
return $this->productView->getQuantityValidators();
36+
}
37+
}

app/code/Magento/Bundle/view/frontend/layout/catalog_product_view_type_bundle.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@
3232
<block class="Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Select" name="product.info.bundle.options.select" as="select">
3333
<arguments>
3434
<argument name="tier_price_renderer" xsi:type="object">\Magento\Bundle\Block\DataProviders\OptionPriceRenderer</argument>
35+
<argument name="validateQuantityViewModel" xsi:type="object">Magento\Bundle\ViewModel\ValidateQuantity</argument>
3536
</arguments>
3637
</block>
3738
<block class="Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Multi" name="product.info.bundle.options.multi" as="multi"/>
3839
<block class="Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Radio" name="product.info.bundle.options.radio" as="radio">
3940
<arguments>
4041
<argument name="tier_price_renderer" xsi:type="object">\Magento\Bundle\Block\DataProviders\OptionPriceRenderer</argument>
42+
<argument name="validateQuantityViewModel" xsi:type="object">Magento\Bundle\ViewModel\ValidateQuantity</argument>
4143
</arguments>
4244
</block>
4345
<block class="Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Checkbox" name="product.info.bundle.options.checkbox" as="checkbox">
@@ -49,7 +51,7 @@
4951
</referenceBlock>
5052
<referenceBlock name="product.info.form.options">
5153
<container name="bundle.product.options.wrapper" htmlTag="div" htmlClass="bundle-options-wrapper"/>
52-
</referenceBlock>
54+
</referenceBlock>
5355
<move element="product.info.options.wrapper" destination="bundle.product.options.wrapper" before="-" />
5456
<move element="product.info.options.wrapper.bottom" destination="bundle.product.options.wrapper" after="product.info.options.wrapper" />
5557
<move element="product.price.tier" destination="product.info.options.wrapper.bottom" before="-" />

app/code/Magento/Bundle/view/frontend/templates/catalog/product/view/type/bundle/option/radio.phtml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
use Magento\Bundle\ViewModel\ValidateQuantity;
67
?>
78
<?php /* @var $block \Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Radio */ ?>
89
<?php $_option = $block->getOption(); ?>
910
<?php $_selections = $_option->getSelections(); ?>
1011
<?php $_default = $_option->getDefaultSelection(); ?>
1112
<?php list($_defaultQty, $_canChangeQty) = $block->getDefaultValues(); ?>
12-
13+
<?php
14+
/** @var ValidateQuantity $viewModel */
15+
$viewModel = $block->getData('validateQuantityViewModel');
16+
?>
1317
<div class="field option <?= ($_option->getRequired()) ? ' required': '' ?>">
1418
<label class="label">
1519
<span><?= $block->escapeHtml($_option->getTitle()) ?></span>
@@ -71,6 +75,8 @@
7175
id="bundle-option-<?= $block->escapeHtmlAttr($_option->getId()) ?>-qty-input"
7276
class="input-text qty<?php if (!$_canChangeQty) { echo ' qty-disabled'; } ?>"
7377
type="number"
78+
min="0"
79+
data-validate="<?= $block->escapeHtml(json_encode($viewModel->getQuantityValidators())) ?>"
7480
name="bundle_option_qty[<?= $block->escapeHtmlAttr($_option->getId()) ?>]"
7581
data-selector="bundle_option_qty[<?= $block->escapeHtmlAttr($_option->getId()) ?>]"
7682
value="<?= $block->escapeHtmlAttr($_defaultQty) ?>"/>

app/code/Magento/Bundle/view/frontend/templates/catalog/product/view/type/bundle/option/select.phtml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
use Magento\Bundle\ViewModel\ValidateQuantity;
67
?>
7-
88
<?php /* @var $block \Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Select */ ?>
99
<?php $_option = $block->getOption(); ?>
1010
<?php $_selections = $_option->getSelections(); ?>
1111
<?php $_default = $_option->getDefaultSelection(); ?>
1212
<?php list($_defaultQty, $_canChangeQty) = $block->getDefaultValues(); ?>
13+
<?php
14+
/** @var ValidateQuantity $viewModel */
15+
$viewModel = $block->getData('validateQuantityViewModel');
16+
?>
1317
<div class="field option <?= ($_option->getRequired()) ? ' required': '' ?>">
1418
<label class="label" for="bundle-option-<?= $block->escapeHtmlAttr($_option->getId()) ?>">
1519
<span><?= $block->escapeHtml($_option->getTitle()) ?></span>
@@ -57,6 +61,8 @@
5761
id="bundle-option-<?= $block->escapeHtmlAttr($_option->getId()) ?>-qty-input"
5862
class="input-text qty<?php if (!$_canChangeQty) { echo ' qty-disabled'; } ?>"
5963
type="number"
64+
min="0"
65+
data-validate="<?= $block->escapeHtml(json_encode($viewModel->getQuantityValidators())) ?>"
6066
name="bundle_option_qty[<?= $block->escapeHtmlAttr($_option->getId()) ?>]"
6167
data-selector="bundle_option_qty[<?= $block->escapeHtmlAttr($_option->getId()) ?>]"
6268
value="<?= $block->escapeHtmlAttr($_defaultQty) ?>"/>

0 commit comments

Comments
 (0)