Skip to content

Commit 4940120

Browse files
committed
add Looking Similar Widget
1 parent ccddef7 commit 4940120

File tree

8 files changed

+143
-2
lines changed

8 files changed

+143
-2
lines changed

Block/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ public function getConfiguration()
222222
'enabledRelated' => $config->isRecommendRelatedProductsEnabled(),
223223
'enabledFBTInCart' => $config->isRecommendFrequentlyBroughtTogetherEnabledOnCartPage(),
224224
'enabledRelatedInCart' => $config->isRecommendRelatedProductsEnabledOnCartPage(),
225+
'enabledLookingSimilar' => $config->isRecommendLookingSimilarEnabled(),
225226
'limitFBTProducts' => $config->getNumberOfFrequentlyBoughtTogetherProducts(),
226227
'limitRelatedProducts' => $config->getNumberOfRelatedProducts(),
227228
'limitTrendingItems' => $config->getNumberOfTrendingItems(),

Block/Widget/LookingSimilar.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Block\Widget;
4+
5+
use Magento\Framework\Math\Random;
6+
use Magento\Framework\View\Element\Template;
7+
use Magento\Framework\View\Element\Template\Context;
8+
use Magento\Widget\Block\BlockInterface;
9+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
10+
11+
class LookingSimilar extends Template implements BlockInterface
12+
{
13+
/**
14+
* @var ConfigHelper
15+
*/
16+
protected $configHelper;
17+
18+
protected $_template = 'recommend/widget/looking-similar.phtml';
19+
20+
/**
21+
* @param Context $context
22+
* @param ConfigHelper $configHelper
23+
* @param Random $mathRandom
24+
* @param array $data
25+
*/
26+
public function __construct(
27+
Context $context,
28+
ConfigHelper $configHelper,
29+
Random $mathRandom,
30+
array $data = []
31+
) {
32+
$this->mathRandom = $mathRandom;
33+
$this->configHelper = $configHelper;
34+
parent::__construct(
35+
$context,
36+
$data
37+
);
38+
}
39+
40+
/**
41+
* @return string
42+
* @throws \Magento\Framework\Exception\LocalizedException
43+
*/
44+
public function generateUniqueToken()
45+
{
46+
return $this->mathRandom->getRandomString(5);
47+
}
48+
49+
/**
50+
* @return int
51+
*/
52+
public function isLookingSimilarEnabled()
53+
{
54+
return $this->configHelper->isRecommendLookingSimilarEnabled();
55+
}
56+
}

Helper/ConfigHelper.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class ConfigHelper
127127
protected const IS_REMOVE_RELATED_PRODUCTS_BLOCK = 'algoliasearch_recommend/recommend/related_product/is_remove_core_related_products_block';
128128
protected const IS_REMOVE_UPSELL_PRODUCTS_BLOCK = 'algoliasearch_recommend/recommend/frequently_bought_together/is_remove_core_upsell_products_block';
129129
protected const IS_RECOMMEND_TRENDING_ITEMS_ENABLED = 'algoliasearch_recommend/recommend/trends_item/is_trending_items_enabled';
130+
protected const IS_RECOMMEND_LOOKING_SIMILAR_ENABLED = 'algoliasearch_recommend/recommend/looking_similar/is_looking_similar_enabled';
130131
protected const NUM_OF_LOOKING_SIMILAR = 'algoliasearch_recommend/recommend/looking_similar/num_of_products';
131132
protected const NUM_OF_TRENDING_ITEMS = 'algoliasearch_recommend/recommend/trends_item/num_of_trending_items';
132133
protected const TREND_ITEMS_FACET_NAME = 'algoliasearch_recommend/recommend/trends_item/facet_name';
@@ -851,6 +852,21 @@ public function getTrendingItemsFacetValue($storeId = null)
851852
);
852853
}
853854

855+
/**
856+
* Determines whether Looking Similar enabled (for widgets))
857+
*
858+
* @param $storeId
859+
* @return int
860+
*/
861+
public function isRecommendLookingSimilarEnabled($storeId = null)
862+
{
863+
return (int)$this->configInterface->getValue(
864+
self::IS_RECOMMEND_LOOKING_SIMILAR_ENABLED,
865+
ScopeInterface::SCOPE_STORE,
866+
$storeId
867+
);
868+
}
869+
854870
/**
855871
* Determines whether Looking Similar enabled on PDP
856872
*

ViewModel/Recommend/ProductView.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public function getAlgoliaRecommendConfiguration()
6767
return [
6868
'enabledFBT' => $this->configHelper->isRecommendFrequentlyBroughtTogetherEnabled(),
6969
'enabledRelated' => $this->configHelper->isRecommendRelatedProductsEnabled(),
70-
'isTrendItemsEnabledInPDP' => $this->configHelper->isTrendItemsEnabledInPDP()
70+
'isTrendItemsEnabledInPDP' => $this->configHelper->isTrendItemsEnabledInPDP(),
71+
'isLookingSimilarEnabledInPDP' => $this->configHelper->isLookingSimilarEnabledOnPDP()
7172
];
7273
}
7374
}

etc/widget.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,15 @@
1919
</parameter>
2020
</parameters>
2121
</widget>
22+
<widget class="Algolia\AlgoliaSearch\Block\Widget\LookingSimilar" id="algoliasearch_looking_similar_item">
23+
<label>Algolia Looking Similar Items</label>
24+
<description>You can allow Algolia Looking Similar items to appear anywhere you want</description>
25+
<parameters>
26+
<parameter name="numOfLookingSimilarItem" sort_order="10" visible="true" xsi:type="text">
27+
<label>Number Of Looking Similar Items</label>
28+
<description>How many products do you want to display in the Looking Similar Items?.</description>
29+
<value>6</value>
30+
</parameter>
31+
</parameters>
32+
</widget>
2233
</widgets>

view/frontend/templates/recommend/products.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ $recommendConfig = $viewModel->getAlgoliaRecommendConfiguration();
1212
if (!empty($recommendConfig['enabledFBT'])
1313
|| !empty($recommendConfig['enabledRelated'])
1414
|| !empty($recommendConfig['isTrendItemsEnabledInPDP'])
15-
|| !empty($recommendConfig['isLookingSimilarEnabledOnPDP'])):
15+
|| !empty($recommendConfig['isLookingSimilarEnabledInPDP'])):
1616
$product = $viewModel->getProduct(); ?>
1717
<div id="frequentlyBoughtTogether" class="recommend-component"></div>
1818
<div id="relatedProducts" class="recommend-component"></div>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/** @var \Algolia\AlgoliaSearch\Block\Widget\LookingSimilar $block */
3+
$isEnabled = $block->isLookingSimilarEnabled();
4+
if ($isEnabled):
5+
$lsContainer = 'lsItems' . $block->generateUniqueToken(); ?>
6+
<div id="<?= $lsContainer ?>" class="lsItem recommend-component"></div>
7+
<script type="text/x-magento-init">
8+
{
9+
"*": {
10+
"Algolia_AlgoliaSearch/recommend" : {
11+
"recommendLSContainer" : "<?= $lsContainer ?>",
12+
"numOfLookingSimilarItem" : "<?= $block->getData('numOfLookingSimilarItem') ?>",
13+
}
14+
}
15+
}
16+
</script>
17+
<script type="text/x-magento-init">
18+
{
19+
"[data-role=tocart-form]": {
20+
"catalogAddToCart": {}
21+
}
22+
}
23+
</script>
24+
<?php endif; ?>

view/frontend/web/recommend.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,38 @@ define([
197197
);
198198
}
199199
});
200+
} else if (
201+
algoliaConfig.recommend.enabledLookingSimilar &&
202+
typeof config.recommendLSContainer !== 'undefined'
203+
){
204+
let containerValue = '#' + config.recommendLSContainer;
205+
recommendJs.lookingSimilar({
206+
container: containerValue,
207+
recommendClient,
208+
indexName,
209+
maxRecommendations: config.numOfLookingSimilarItem
210+
? parseInt(config.numOfLookingSimilarItem)
211+
: algoliaConfig.recommend.limitLookingSimilar,
212+
transformItems : function (items) {
213+
return items.map((item, index) => ({
214+
...item,
215+
position: index + 1,
216+
}));
217+
},
218+
headerComponent({html}) {
219+
return recommendProductsHtml.getHeaderHtml(
220+
html,
221+
algoliaConfig.recommend.lookingSimilarTitle
222+
);
223+
},
224+
itemComponent({item, html}) {
225+
return recommendProductsHtml.getItemHtml(
226+
item,
227+
html,
228+
algoliaConfig.recommend.isAddToCartEnabledInLookingSimilar
229+
);
230+
},
231+
});
200232
}
201233
});
202234
};

0 commit comments

Comments
 (0)