Skip to content

Commit 78857c9

Browse files
committed
MAGE-897 add productIds parameter to widget
1 parent c80f2b8 commit 78857c9

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

Block/Widget/LookingSimilar.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public function generateUniqueToken()
4646
return $this->mathRandom->getRandomString(5);
4747
}
4848

49+
/**
50+
* @return string
51+
*/
52+
public function getProductIds()
53+
{
54+
return json_encode(explode(",", $this->getData('productIds')));
55+
}
56+
4957
/**
5058
* @return int
5159
*/

etc/widget.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
<description>How many products do you want to display in the Looking Similar Items?.</description>
2929
<value>6</value>
3030
</parameter>
31+
<parameter name="productIds" sort_order="20" visible="true" xsi:type="text">
32+
<label>Product Ids (separated by commas)</label>
33+
<description>Which product(s) should be taken as reference ? (mandatory if you don't plan to use this widget on a product page or on the cart page) </description>
34+
</parameter>
3135
</parameters>
3236
</widget>
3337
</widgets>

view/frontend/templates/recommend/widget/looking-similar.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ if ($isEnabled):
1010
"Algolia_AlgoliaSearch/recommend" : {
1111
"recommendLSContainer" : "<?= $lsContainer ?>",
1212
"numOfLookingSimilarItem" : "<?= $block->getData('numOfLookingSimilarItem') ?>",
13+
"productIds" : <?= $block->getProductIds() ?>
1314
}
1415
}
1516
}

view/frontend/web/recommend.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ define([
1919
const apiKey = algoliaConfig.apiKey;
2020
const recommendClient = recommend(appId, apiKey);
2121
const indexName = this.defaultIndexName;
22+
const objectIds = config.productIds ? config.productIds : config.algoliObjectId;
2223
if (
2324
$('body').hasClass('catalog-product-view') ||
2425
$('body').hasClass('checkout-cart-index')
@@ -34,7 +35,7 @@ define([
3435
container : '#frequentlyBoughtTogether',
3536
recommendClient,
3637
indexName,
37-
objectIDs : config.algoliObjectId,
38+
objectIDs : objectIds,
3839
maxRecommendations: algoliaConfig.recommend.limitFBTProducts,
3940
transformItems : function (items) {
4041
return items.map((item, index) => ({
@@ -67,7 +68,7 @@ define([
6768
container : '#relatedProducts',
6869
recommendClient,
6970
indexName,
70-
objectIDs : config.algoliObjectId,
71+
objectIDs : objectIds,
7172
maxRecommendations: algoliaConfig.recommend.limitRelatedProducts,
7273
transformItems : function (items) {
7374
return items.map((item, index) => ({
@@ -175,7 +176,7 @@ define([
175176
container: '#lookingSimilar',
176177
recommendClient,
177178
indexName,
178-
objectIDs: config.algoliObjectId,
179+
objectIDs: objectIds,
179180
maxRecommendations: algoliaConfig.recommend.limitLookingSimilar,
180181
transformItems: function (items) {
181182
return items.map((item, index) => ({
@@ -199,15 +200,15 @@ define([
199200
});
200201
} else if (
201202
algoliaConfig.recommend.enabledLookingSimilar &&
202-
config.algoliObjectId &&
203+
objectIds &&
203204
typeof config.recommendLSContainer !== 'undefined'
204205
){
205206
let containerValue = '#' + config.recommendLSContainer;
206207
recommendJs.lookingSimilar({
207208
container: containerValue,
208209
recommendClient,
209210
indexName,
210-
objectIDs: config.algoliObjectId,
211+
objectIDs: objectIds,
211212
maxRecommendations: config.numOfLookingSimilarItem
212213
? parseInt(config.numOfLookingSimilarItem)
213214
: algoliaConfig.recommend.limitLookingSimilar,

0 commit comments

Comments
 (0)