Skip to content

Commit 2f11068

Browse files
authored
Merge pull request #1530 from algolia/epic/MAGE-893
Epic/mage 893
2 parents 3e24c73 + 89425b6 commit 2f11068

File tree

14 files changed

+319
-28
lines changed

14 files changed

+319
-28
lines changed

Block/Configuration.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,11 @@ 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(),
229+
'limitLookingSimilar' => $config->getNumberOfLookingSimilar(),
228230
'enabledTrendItems' => $config->isRecommendTrendingItemsEnabled(),
229231
'trendItemFacetName' => $config->getTrendingItemsFacetName(),
230232
'trendItemFacetValue' => $config->getTrendingItemsFacetValue(),
@@ -233,10 +235,14 @@ public function getConfiguration()
233235
'isAddToCartEnabledInFBT' => $config->isAddToCartEnabledInFrequentlyBoughtTogether(),
234236
'isAddToCartEnabledInRelatedProduct' => $config->isAddToCartEnabledInRelatedProducts(),
235237
'isAddToCartEnabledInTrendsItem' => $config->isAddToCartEnabledInTrendsItem(),
238+
'isAddToCartEnabledInLookingSimilar' => $config->isAddToCartEnabledInLookingSimilar(),
236239
'FBTTitle' => __($config->getFBTTitle()),
237240
'relatedProductsTitle' => __($config->getRelatedProductsTitle()),
238241
'trendingItemsTitle' => __($config->getTrendingItemsTitle()),
239242
'addToCartParams' => $addToCartParams,
243+
'isLookingSimilarEnabledInPDP' => $config->isLookingSimilarEnabledInPDP(),
244+
'isLookingSimilarEnabledInCartPage' => $config->isLookingSimilarEnabledInShoppingCart(),
245+
'lookingSimilarTitle' => __($config->getLookingSimilarTitle())
240246
],
241247
'extensionVersion' => $config->getExtensionVersion(),
242248
'applicationId' => $config->getApplicationID(),

Block/Widget/LookingSimilar.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 string
51+
*/
52+
public function getProductIds()
53+
{
54+
return json_encode(explode(",", $this->getData('productIds')));
55+
}
56+
57+
/**
58+
* @return int
59+
*/
60+
public function isLookingSimilarEnabled()
61+
{
62+
return $this->configHelper->isRecommendLookingSimilarEnabled();
63+
}
64+
}

Helper/ConfigHelper.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ 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';
131+
protected const NUM_OF_LOOKING_SIMILAR = 'algoliasearch_recommend/recommend/looking_similar/num_of_products';
130132
protected const NUM_OF_TRENDING_ITEMS = 'algoliasearch_recommend/recommend/trends_item/num_of_trending_items';
131133
protected const TREND_ITEMS_FACET_NAME = 'algoliasearch_recommend/recommend/trends_item/facet_name';
132134
protected const TREND_ITEMS_FACET_VALUE = 'algoliasearch_recommend/recommend/trends_item/facet_value';
@@ -135,6 +137,10 @@ class ConfigHelper
135137
protected const IS_ADDTOCART_ENABLED_IN_FREQUENTLY_BOUGHT_TOGETHER = 'algoliasearch_recommend/recommend/frequently_bought_together/is_addtocart_enabled';
136138
protected const IS_ADDTOCART_ENABLED_IN_RELATED_PRODUCTS = 'algoliasearch_recommend/recommend/related_product/is_addtocart_enabled';
137139
protected const IS_ADDTOCART_ENABLED_IN_TRENDS_ITEM = 'algoliasearch_recommend/recommend/trends_item/is_addtocart_enabled';
140+
protected const IS_ADDTOCART_ENABLED_IN_LOOKING_SIMILAR = 'algoliasearch_recommend/recommend/looking_similar/is_addtocart_enabled';
141+
protected const IS_LOOKING_SIMILAR_ENABLED_IN_PDP = 'algoliasearch_recommend/recommend/looking_similar/is_looking_similar_enabled_on_pdp';
142+
protected const IS_LOOKING_SIMILAR_ENABLED_IN_SHOPPING_CART = 'algoliasearch_recommend/recommend/looking_similar/is_looking_similar_enabled_on_cart_page';
143+
protected const LOOKING_SIMILAR_TITLE = 'algoliasearch_recommend/recommend/looking_similar/title';
138144
protected const USE_VIRTUAL_REPLICA_ENABLED = 'algoliasearch_instant/instant/use_virtual_replica';
139145
protected const AUTOCOMPLETE_KEYBORAD_NAVIAGATION = 'algoliasearch_autocomplete/autocomplete/navigator';
140146
protected const FREQUENTLY_BOUGHT_TOGETHER_TITLE = 'algoliasearch_recommend/recommend/frequently_bought_together/title';
@@ -805,6 +811,21 @@ public function getNumberOfTrendingItems($storeId = null)
805811
);
806812
}
807813

814+
/**
815+
* Returns number of looking similar products to display
816+
*
817+
* @param $storeId
818+
* @return int
819+
*/
820+
public function getNumberOfLookingSimilar($storeId = null)
821+
{
822+
return (int)$this->configInterface->getValue(
823+
self::NUM_OF_LOOKING_SIMILAR,
824+
ScopeInterface::SCOPE_STORE,
825+
$storeId
826+
);
827+
}
828+
808829
/**
809830
* @param $storeId
810831
* @return mixed
@@ -831,6 +852,57 @@ public function getTrendingItemsFacetValue($storeId = null)
831852
);
832853
}
833854

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+
870+
/**
871+
* Determines whether Looking Similar enabled on PDP
872+
*
873+
* @param $storeId
874+
* @return int
875+
*/
876+
public function isLookingSimilarEnabledInPDP($storeId = null)
877+
{
878+
return (int)$this->configInterface->getValue(
879+
self::IS_LOOKING_SIMILAR_ENABLED_IN_PDP,
880+
ScopeInterface::SCOPE_STORE,
881+
$storeId
882+
);
883+
}
884+
885+
public function getLookingSimilarTitle($storeId = null)
886+
{
887+
return $this->configInterface->getValue(
888+
self::LOOKING_SIMILAR_TITLE,
889+
ScopeInterface::SCOPE_STORE, $storeId
890+
);
891+
}
892+
893+
/**
894+
* @param $storeId
895+
* @return int
896+
*/
897+
public function isLookingSimilarEnabledInShoppingCart($storeId = null)
898+
{
899+
return (int)$this->configInterface->getValue(
900+
self::IS_LOOKING_SIMILAR_ENABLED_IN_SHOPPING_CART,
901+
ScopeInterface::SCOPE_STORE,
902+
$storeId
903+
);
904+
}
905+
834906
/**
835907
* @param $storeId
836908
* @return int
@@ -884,6 +956,21 @@ public function isAddToCartEnabledInTrendsItem($storeId = null)
884956
return $this->configInterface->isSetFlag(self::IS_ADDTOCART_ENABLED_IN_TRENDS_ITEM, ScopeInterface::SCOPE_STORE, $storeId);
885957
}
886958

959+
/**
960+
* Determines whether add to cart is enabled in Looking Similar
961+
*
962+
* @param $storeId
963+
* @return bool
964+
*/
965+
public function isAddToCartEnabledInLookingSimilar($storeId = null)
966+
{
967+
return $this->configInterface->isSetFlag(
968+
self::IS_ADDTOCART_ENABLED_IN_LOOKING_SIMILAR,
969+
ScopeInterface::SCOPE_STORE,
970+
$storeId
971+
);
972+
}
973+
887974
/**
888975
* @param $storeId
889976
* @return string

ViewModel/Recommend/Cart.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Cart implements ArgumentInterface
1616
* @var StoreManagerInterface
1717
*/
1818
protected $storeManager;
19-
19+
2020
/**
2121
* @var Session
2222
*/
@@ -81,7 +81,8 @@ public function getAlgoliaRecommendConfiguration()
8181
return [
8282
'enabledFBTInCart' => $this->configHelper->isRecommendFrequentlyBroughtTogetherEnabledOnCartPage(),
8383
'enabledRelatedInCart' => $this->configHelper->isRecommendRelatedProductsEnabledOnCartPage(),
84-
'isTrendItemsEnabledInCartPage' => $this->configHelper->isTrendItemsEnabledInShoppingCart()
84+
'isTrendItemsEnabledInCartPage' => $this->configHelper->isTrendItemsEnabledInShoppingCart(),
85+
'isLookingSimilarEnabledInCartPage' => $this->configHelper->isLookingSimilarEnabledInShoppingCart()
8586
];
8687
}
8788
}

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->isLookingSimilarEnabledInPDP()
7172
];
7273
}
7374
}

etc/widget.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,19 @@
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+
<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>
35+
</parameters>
36+
</widget>
2237
</widgets>

view/frontend/layout/catalog_product_view.xml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
* See COPYING.txt for license details.
66
*/
77
-->
8-
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
8+
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
910
<body>
1011
<referenceContainer name="content.aside">
11-
<block name="algolia.product.view" template="Algolia_AlgoliaSearch::recommend/products.phtml" >
12-
<arguments>
13-
<argument name="view_model" xsi:type="object">Algolia\AlgoliaSearch\ViewModel\Recommend\ProductView</argument>
14-
</arguments>
12+
<block name="algolia.product.view" template="Algolia_AlgoliaSearch::recommend/products.phtml">
13+
<arguments>
14+
<argument name="view_model" xsi:type="object">
15+
Algolia\AlgoliaSearch\ViewModel\Recommend\ProductView
16+
</argument>
17+
</arguments>
1518
</block>
1619
</referenceContainer>
1720
</body>
18-
</page>
21+
</page>

view/frontend/templates/recommend/cart/recommend_items.phtml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ if (!empty($recommendConfig['enabledRelatedInCart']) || !empty($recommendConfig[
88
<div id="frequentlyBoughtTogether" class="recommend-component"></div>
99
<div id="relatedProducts" class="recommend-component"></div>
1010
<div id="trendItems" class="trendsItem recommend-component"></div>
11+
<div id="lookingSimilar" class="lookingSimilar recommend-component"></div>
1112
<script type="text/x-magento-init">
1213
{
1314
"*": {
1415
"Algolia_AlgoliaSearch/recommend" : {
15-
"algoliObjectId" : <?= json_encode($cartItems) ?>
16+
"objectIDs" : <?= json_encode($cartItems) ?>
1617
}
1718
}
1819
}

view/frontend/templates/recommend/products.phtml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
<?php
2-
/** @var \Algolia\AlgoliaSearch\ViewModel\Recommend\ProductView $block */
2+
use Algolia\AlgoliaSearch\ViewModel\Recommend\ProductView;
3+
use Magento\Framework\View\Element\Template;
4+
5+
/**
6+
* @var ProductView $viewModel
7+
* @var Template $block
8+
*/
39
$viewModel = $block->getViewModel();
4-
$recommendConfig = $viewModel->getAlgoliaRecommendConfiguration();
10+
$recommendConfig = $viewModel->getAlgoliaRecommendConfiguration();
511

6-
if (!empty($recommendConfig['enabledFBT']) || !empty($recommendConfig['enabledRelated']) || !empty($recommendConfig['isTrendItemsEnabledInPDP'])):
12+
if (!empty($recommendConfig['enabledFBT'])
13+
|| !empty($recommendConfig['enabledRelated'])
14+
|| !empty($recommendConfig['isTrendItemsEnabledInPDP'])
15+
|| !empty($recommendConfig['isLookingSimilarEnabledInPDP'])):
716
$product = $viewModel->getProduct(); ?>
817
<div id="frequentlyBoughtTogether" class="recommend-component"></div>
918
<div id="relatedProducts" class="recommend-component"></div>
1019
<div id="trendItems" class="trendsItem recommend-component"></div>
20+
<div id="lookingSimilar" class="lookingSimilar recommend-component"></div>
1121
<script type="text/x-magento-init">
1222
{
1323
"*": {
1424
"Algolia_AlgoliaSearch/recommend" : {
15-
"algoliObjectId" : ["<?= $product->getId() ?>"]
25+
"objectIDs" : ["<?= $product->getId() ?>"]
1626
}
1727
}
1828
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
"objectIDs" : <?= $block->getProductIds() ?>
14+
}
15+
}
16+
}
17+
</script>
18+
<script type="text/x-magento-init">
19+
{
20+
"[data-role=tocart-form]": {
21+
"catalogAddToCart": {}
22+
}
23+
}
24+
</script>
25+
<?php endif; ?>

0 commit comments

Comments
 (0)