diff -Nuar a/vendor/magento/module-wishlist/Block/AddToWishlist.php b/vendor/magento/module-wishlist/Block/AddToWishlist.php index 3ba350af941..cedd39d90d7 100644 --- a/vendor/magento/module-wishlist/Block/AddToWishlist.php +++ b/vendor/magento/module-wishlist/Block/AddToWishlist.php @@ -6,13 +6,15 @@ namespace Magento\Wishlist\Block; +use Magento\Framework\View\Element\Template; + /** * Wishlist js plugin initialization block * * @api * @since 100.1.0 */ -class AddToWishlist extends \Magento\Framework\View\Element\Template +class AddToWishlist extends Template { /** * Product types @@ -22,20 +24,6 @@ class AddToWishlist extends \Magento\Framework\View\Element\Template private $productTypes; /** - * @param \Magento\Framework\View\Element\Template\Context $context - * @param array $data - */ - public function __construct( - \Magento\Framework\View\Element\Template\Context $context, - array $data = [] - ) { - parent::__construct( - $context, - $data - ); - } - - /** * Returns wishlist widget options * * @return array @@ -43,7 +31,10 @@ class AddToWishlist extends \Magento\Framework\View\Element\Template */ public function getWishlistOptions() { - return ['productType' => $this->getProductTypes()]; + return [ + 'productType' => $this->getProductTypes(), + 'isProductList' => (bool)$this->getData('is_product_list') + ]; } /** @@ -56,7 +47,7 @@ class AddToWishlist extends \Magento\Framework\View\Element\Template { if ($this->productTypes === null) { $this->productTypes = []; - $block = $this->getLayout()->getBlock('category.products.list'); + $block = $this->getLayout()->getBlock($this->getProductListBlockName()); if ($block) { $productCollection = $block->getLoadedProductCollection(); $productTypes = []; @@ -71,6 +62,16 @@ class AddToWishlist extends \Magento\Framework\View\Element\Template } /** + * Get product list block name in layout + * + * @return string + */ + private function getProductListBlockName(): string + { + return $this->getData('product_list_block') ?: 'category.products.list'; + } + + /** * {@inheritdoc} * @since 100.1.0 */ diff -Nuar a/vendor/magento/module-wishlist/view/frontend/layout/catalog_category_view.xml b/vendor/magento/module-wishlist/view/frontend/layout/catalog_category_view.xml index a4860ace166..8b784cfd317 100644 --- a/vendor/magento/module-wishlist/view/frontend/layout/catalog_category_view.xml +++ b/vendor/magento/module-wishlist/view/frontend/layout/catalog_category_view.xml @@ -21,7 +21,15 @@ template="Magento_Wishlist::catalog/product/list/addto/wishlist.phtml"/> - + + + true + + diff -Nuar a/vendor/magento/module-wishlist/view/frontend/layout/catalogsearch_result_index.xml b/vendor/magento/module-wishlist/view/frontend/layout/catalogsearch_result_index.xml index c293175ccce..affd5c77360 100644 --- a/vendor/magento/module-wishlist/view/frontend/layout/catalogsearch_result_index.xml +++ b/vendor/magento/module-wishlist/view/frontend/layout/catalogsearch_result_index.xml @@ -14,5 +14,18 @@ template="Magento_Wishlist::catalog/product/list/addto/wishlist.phtml"/> + + + + true + search_result_list + + + + diff -Nuar a/vendor/magento/module-wishlist/view/frontend/web/js/add-to-wishlist.js b/vendor/magento/module-wishlist/view/frontend/web/js/add-to-wishlist.js index 7ce93431726..32623ececaa 100644 --- a/vendor/magento/module-wishlist/view/frontend/web/js/add-to-wishlist.js +++ b/vendor/magento/module-wishlist/view/frontend/web/js/add-to-wishlist.js @@ -16,7 +16,11 @@ define([ groupedInfo: '#super-product-table input', downloadableInfo: '#downloadable-links-list input', customOptionsInfo: '.product-custom-option', - qtyInfo: '#qty' + qtyInfo: '#qty', + actionElement: '[data-action="add-to-wishlist"]', + productListItem: '.item.product-item', + productListPriceBox: '.price-box', + isProductList: false }, /** @inheritdoc */ @@ -30,8 +34,10 @@ define([ _bind: function () { var options = this.options, dataUpdateFunc = '_updateWishlistData', + validateProductQty = '_validateWishlistQty', changeCustomOption = 'change ' + options.customOptionsInfo, changeQty = 'change ' + options.qtyInfo, + updateWishlist = 'click ' + options.actionElement, events = {}, key; @@ -45,6 +51,7 @@ define([ events[changeCustomOption] = dataUpdateFunc; events[changeQty] = dataUpdateFunc; + events[updateWishlist] = validateProductQty; for (key in options.productType) { if (options.productType.hasOwnProperty(key) && options.productType[key] + 'Info' in options) { @@ -61,6 +68,7 @@ define([ _updateWishlistData: function (event) { var dataToAdd = {}, isFileUploaded = false, + productId = null, self = this; if (event.handleObj.selector == this.options.qtyInfo) { //eslint-disable-line eqeqeq @@ -79,7 +87,21 @@ define([ $(element).is('textarea') || $('#' + element.id + ' option:selected').length ) { - dataToAdd = $.extend({}, dataToAdd, self._getElementData(element)); + if (!($(element).data('selector') || $(element).attr('name'))) { + return; + } + + if (self.options.isProductList) { + productId = self.retrieveListProductId(this); + + dataToAdd[productId] = $.extend( + {}, + dataToAdd[productId] ? dataToAdd[productId] : {}, + self._getElementData(element) + ); + } else { + dataToAdd = $.extend({}, dataToAdd, self._getElementData(element)); + } return; } @@ -101,10 +123,17 @@ define([ * @private */ _updateAddToWishlistButton: function (dataToAdd) { - var self = this; + var productId = null, + self = this; $('[data-action="add-to-wishlist"]').each(function (index, element) { - var params = $(element).data('post'); + var params = $(element).data('post'), + dataToAddObj = dataToAdd; + + if (self.options.isProductList) { + productId = self.retrieveListProductId(element); + dataToAddObj = typeof dataToAdd[productId] !== 'undefined' ? dataToAdd[productId] : {}; + } if (!params) { params = { @@ -112,7 +141,7 @@ define([ }; } - params.data = $.extend({}, params.data, dataToAdd, { + params.data = $.extend({}, params.data, dataToAddObj, { 'qty': $(self.options.qtyInfo).val() }); $(element).data('post', params); @@ -224,6 +253,38 @@ define([ $(form).attr('action', action).submit(); }); + }, + + /** + * Validate product quantity before updating Wish List + * + * @param {jQuery.Event} event + * @private + */ + _validateWishlistQty: function (event) { + var element = $(this.options.qtyInfo); + + if (!(element.validation() && element.validation('isValid'))) { + event.preventDefault(); + event.stopPropagation(); + + return; + } + }, + + /** + * Retrieve product id from element on products list + * + * @param {jQuery.Object} element + * @private + */ + retrieveListProductId: function (element) { + return parseInt( + $(element).closest(this.options.productListItem) + .find(this.options.productListPriceBox) + .data('product-id'), + 10 + ); } });