Skip to content

Commit e3fd246

Browse files
author
Gabriel Galvao da Gama
committed
Small changes to increase performance
1 parent 8eead95 commit e3fd246

File tree

4 files changed

+56
-66
lines changed

4 files changed

+56
-66
lines changed

app/code/Magento/Wishlist/Block/AddToWishlist.php

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
namespace Magento\Wishlist\Block;
88

9+
use Magento\Catalog\Api\Data\ProductTypeInterface;
10+
use Magento\Catalog\Api\ProductTypeListInterface;
11+
use Magento\Framework\App\ObjectManager;
912
use Magento\Framework\View\Element\Template;
13+
use Magento\Framework\View\Element\Template\Context;
1014

1115
/**
1216
* Wishlist js plugin initialization block
@@ -24,63 +28,50 @@ class AddToWishlist extends Template
2428
private $productTypes;
2529

2630
/**
27-
* Returns wishlist widget options
28-
*
29-
* @return array
30-
* @since 100.1.0
31+
* @var ProductTypeListInterface
3132
*/
32-
public function getWishlistOptions()
33-
{
34-
return [
35-
'productType' => $this->getProductTypes(),
36-
'isProductList' => (bool)$this->getData('is_product_list')
37-
];
38-
}
33+
private $productTypeList;
3934

4035
/**
41-
* Returns an array of product types
42-
*
43-
* @return array|null
44-
* @throws \Magento\Framework\Exception\LocalizedException
36+
* AddToWishlist constructor.
37+
* @param ProductTypeListInterface $productTypeList
38+
* @param Context $context
39+
* @param array $data
4540
*/
46-
private function getProductTypes()
47-
{
48-
if ($this->productTypes === null) {
49-
$this->productTypes = [];
50-
$block = $this->getLayout()->getBlock($this->getProductListBlockName());
51-
if ($block) {
52-
$productCollection = $block->getLoadedProductCollection();
53-
$productTypes = [];
54-
/** @var $product \Magento\Catalog\Model\Product */
55-
foreach ($productCollection as $product) {
56-
$productTypes[] = $this->escapeHtml($product->getTypeId());
57-
}
58-
$this->productTypes = array_unique($productTypes);
59-
}
60-
}
61-
return $this->productTypes;
41+
public function __construct(
42+
Context $context,
43+
array $data = [],
44+
?ProductTypeListInterface $productTypeList = null
45+
) {
46+
parent::__construct($context, $data);
47+
$this->productTypes = [];
48+
$this->productTypeList = $productTypeList ?: ObjectManager::getInstance()->get(ProductTypeListInterface::class);
6249
}
6350

6451
/**
65-
* Get product list block name in layout
52+
* Returns wishlist widget options
6653
*
67-
* @return string
54+
* @return array
55+
* @since 100.1.0
6856
*/
69-
private function getProductListBlockName(): string
57+
public function getWishlistOptions()
7058
{
71-
return $this->getData('product_list_block') ?: 'category.products.list';
59+
return ['productType' => $this->getProductTypes()];
7260
}
7361

7462
/**
75-
* @inheritDoc
63+
* Returns an array of product types
7664
*
77-
* @since 100.1.0
65+
* @return array
7866
*/
79-
protected function _toHtml()
67+
private function getProductTypes(): array
8068
{
81-
if (!$this->getProductTypes()) {
82-
return '';
69+
if (count($this->productTypes) === 0) {
70+
/** @var ProductTypeInterface productTypes */
71+
$this->productTypes = array_map(function ($productType) {
72+
return $productType->getName();
73+
}, $this->productTypeList->getProductTypes());
8374
}
84-
return parent::_toHtml();
75+
return $this->productTypes;
8576
}
8677
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@
2323
<referenceContainer name="category.product.list.additional">
2424
<block class="Magento\Wishlist\Block\AddToWishlist"
2525
name="category.product.list.additional.wishlist_addto"
26-
template="Magento_Wishlist::addto.phtml">
27-
<arguments>
28-
<argument name="is_product_list" xsi:type="boolean">true</argument>
29-
</arguments>
30-
</block>
26+
template="Magento_Wishlist::addto.phtml"/>
3127
</referenceContainer>
3228
</referenceContainer>
3329
</body>

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@
1717
<referenceBlock name="wishlist_page_head_components">
1818
<block class="Magento\Wishlist\Block\AddToWishlist"
1919
name="catalogsearch.wishlist_addto"
20-
template="Magento_Wishlist::addto.phtml">
21-
<arguments>
22-
<argument name="is_product_list" xsi:type="boolean">true</argument>
23-
<argument name="product_list_block" xsi:type="string">search_result_list</argument>
24-
</arguments>
25-
</block>
20+
template="Magento_Wishlist::addto.phtml"/>
2621
</referenceBlock>
2722
</body>
2823
</page>

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ define([
1818
customOptionsInfo: '.product-custom-option',
1919
qtyInfo: '#qty',
2020
actionElement: '[data-action="add-to-wishlist"]',
21-
productListItem: '.item.product-item',
22-
isProductList: false
21+
productListWrapper: '.product-item-info',
22+
productPageWrapper: '.product-info-main'
2323
},
2424

2525
/** @inheritdoc */
@@ -67,23 +67,17 @@ define([
6767
_updateWishlistData: function (event) {
6868
var dataToAdd = {},
6969
isFileUploaded = false,
70-
productItem = null,
7170
handleObjSelector = null,
7271
self = this;
7372

7473
if (event.handleObj.selector == this.options.qtyInfo) { //eslint-disable-line eqeqeq
75-
this._updateAddToWishlistButton({}, productItem);
74+
this._updateAddToWishlistButton({}, event);
7675
event.stopPropagation();
7776

7877
return;
7978
}
8079

81-
if (this.options.isProductList) {
82-
productItem = $(event.target).closest(this.options.productListItem);
83-
handleObjSelector = productItem.find(event.handleObj.selector);
84-
} else {
85-
handleObjSelector = $(event.handleObj.selector);
86-
}
80+
handleObjSelector = $(event.currentTarget).closest('form').find(event.handleObj.selector)
8781

8882
handleObjSelector.each(function (index, element) {
8983
if ($(element).is('input[type=text]') ||
@@ -110,18 +104,18 @@ define([
110104
if (isFileUploaded) {
111105
this.bindFormSubmit();
112106
}
113-
this._updateAddToWishlistButton(dataToAdd, productItem);
107+
this._updateAddToWishlistButton(dataToAdd, event);
114108
event.stopPropagation();
115109
},
116110

117111
/**
118112
* @param {Object} dataToAdd
119-
* @param {Object} productItem
113+
* @param {jQuery.Event} event
120114
* @private
121115
*/
122-
_updateAddToWishlistButton: function (dataToAdd, productItem) {
116+
_updateAddToWishlistButton: function (dataToAdd, event) {
123117
var self = this,
124-
buttons = productItem ? productItem.find(this.options.actionElement) : $(this.options.actionElement);
118+
buttons = this._getAddToWishlistButton(event);
125119

126120
buttons.each(function (index, element) {
127121
var params = $(element).data('post');
@@ -139,6 +133,20 @@ define([
139133
});
140134
},
141135

136+
/**
137+
* @param {jQuery.Event} event
138+
* @private
139+
*/
140+
_getAddToWishlistButton: function (event) {
141+
var productListWrapper = $(event.currentTarget).closest(this.options.productListWrapper);
142+
143+
if (productListWrapper.length) {
144+
return productListWrapper.find(this.options.actionElement);
145+
}
146+
147+
return $(event.currentTarget).closest(this.options.productPageWrapper).find(this.options.actionElement);
148+
},
149+
142150
/**
143151
* @param {Object} array1
144152
* @param {Object} array2

0 commit comments

Comments
 (0)