Skip to content

Commit a71bea8

Browse files
committed
MAGETWO-62728: My Wishlist - quantity input box issue
- Adding min/max qty checks - Adding js validation
1 parent 5725c92 commit a71bea8

File tree

3 files changed

+57
-28
lines changed

3 files changed

+57
-28
lines changed

app/code/Magento/Wishlist/Block/Customer/Wishlist/Item/Column/Cart.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Wishlist\Block\Customer\Wishlist\Item\Column;
88

9+
use Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter;
10+
911
/**
1012
* Wishlist block customer item cart column
1113
*
@@ -35,4 +37,28 @@ public function getProductItem()
3537
{
3638
return $this->getItem()->getProduct();
3739
}
40+
41+
/**
42+
* Get min and max qty for wishlist form.
43+
*
44+
* @return array
45+
*/
46+
public function getMinMaxQty()
47+
{
48+
$stockItem = $this->stockRegistry->getStockItem(
49+
$this->getItem()->getProduct()->getId(),
50+
$this->getItem()->getProduct()->getStore()->getWebsiteId()
51+
);
52+
53+
$params = [];
54+
55+
$params['minAllowed'] = (float)$stockItem->getMinSaleQty();
56+
if ($stockItem->getMaxSaleQty()) {
57+
$params['maxAllowed'] = (float)$stockItem->getMaxSaleQty();
58+
} else {
59+
$params['maxAllowed'] = (float)StockDataFilter::MAX_QTY_VALUE;
60+
}
61+
62+
return $params;
63+
}
3864
}

app/code/Magento/Wishlist/view/frontend/templates/item/column/cart.phtml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
/** @var \Magento\Wishlist\Model\Item $item */
1212
$item = $block->getItem();
1313
$product = $item->getProduct();
14+
$allowedQty = $block->getMinMaxQty();
1415
?>
1516
<?php foreach ($block->getChildNames() as $childName): ?>
1617
<?= /* @noEscape */ $block->getLayout()->renderElement($childName, false) ?>
@@ -21,7 +22,7 @@ $product = $item->getProduct();
2122
<div class="field qty">
2223
<label class="label" for="qty[<?= $block->escapeHtmlAttr($item->getId()) ?>]"><span><?= $block->escapeHtml(__('Qty')) ?></span></label>
2324
<div class="control">
24-
<input type="number" data-role="qty" id="qty[<?= $block->escapeHtmlAttr($item->getId()) ?>]" class="input-text qty" data-validate="{'required-number':true,'validate-greater-than-zero':true,'maxlength':8}"
25+
<input type="number" data-role="qty" id="qty[<?= $block->escapeHtmlAttr($item->getId()) ?>]" class="input-text qty" data-validate="{'required-number':true,'validate-greater-than-zero':true, 'validate-item-quantity':{'minAllowed':<?= $allowedQty['minAllowed'] ?>,'maxAllowed':<?= $allowedQty['maxAllowed'] ?>}}"
2526
name="qty[<?= $block->escapeHtmlAttr($item->getId()) ?>]" value="<?= /* @noEscape */ (int)($block->getAddToCartQty($item) * 1) ?>">
2627
</div>
2728
</div>

app/code/Magento/Wishlist/view/frontend/web/js/add-to-wishlist.js

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,6 @@ define([
6363
isFileUploaded = false,
6464
self = this;
6565

66-
if (event.handleObj.selector == this.options.qtyInfo) { //eslint-disable-line eqeqeq
67-
this._updateAddToWishlistButton({});
68-
event.stopPropagation();
69-
70-
return;
71-
}
7266
$(event.handleObj.selector).each(function (index, element) {
7367
if ($(element).is('input[type=text]') ||
7468
$(element).is('input[type=email]') ||
@@ -89,9 +83,7 @@ define([
8983
}
9084
});
9185

92-
if (isFileUploaded) {
93-
this.bindFormSubmit();
94-
}
86+
this.bindFormSubmit(isFileUploaded);
9587
this._updateAddToWishlistButton(dataToAdd);
9688
event.stopPropagation();
9789
},
@@ -195,34 +187,44 @@ define([
195187

196188
/**
197189
* Bind form submit.
190+
*
191+
@param {boolean} isFileUploaded
198192
*/
199-
bindFormSubmit: function () {
193+
bindFormSubmit: function (isFileUploaded) {
200194
var self = this;
201195

202196
$('[data-action="add-to-wishlist"]').on('click', function (event) {
203197
var element, params, form, action;
204198

205-
event.stopPropagation();
206-
event.preventDefault();
199+
if (!$($(self.options.qtyInfo).closest('form')).valid()) {
200+
event.stopPropagation();
201+
event.preventDefault();
202+
return;
203+
}
207204

208-
element = $('input[type=file]' + self.options.customOptionsInfo);
209-
params = $(event.currentTarget).data('post');
210-
form = $(element).closest('form');
211-
action = params.action;
205+
if (isFileUploaded) {
212206

213-
if (params.data.id) {
214-
$('<input>', {
215-
type: 'hidden',
216-
name: 'id',
217-
value: params.data.id
218-
}).appendTo(form);
219-
}
207+
element = $('input[type=file]' + self.options.customOptionsInfo);
208+
params = $(event.currentTarget).data('post');
209+
form = $(element).closest('form');
210+
action = params.action;
220211

221-
if (params.data.uenc) {
222-
action += 'uenc/' + params.data.uenc;
223-
}
212+
if (params.data.id) {
213+
$('<input>', {
214+
type: 'hidden',
215+
name: 'id',
216+
value: params.data.id
217+
}).appendTo(form);
218+
}
224219

225-
$(form).attr('action', action).submit();
220+
if (params.data.uenc) {
221+
action += 'uenc/' + params.data.uenc;
222+
}
223+
224+
$(form).attr('action', action).submit();
225+
event.stopPropagation();
226+
event.preventDefault();
227+
}
226228
});
227229
}
228230
});

0 commit comments

Comments
 (0)