Skip to content

Commit

Permalink
Merge pull request #70 from YotpoLtd/v1.4.7
Browse files Browse the repository at this point in the history
v1.4.7: Added advanced configuration options to control where to load the snippet ('Load Yotpo JS Snippet' and related options).
  • Loading branch information
pniel-cohen authored Feb 22, 2024
2 parents ab14427 + 3659bcf commit 8833659
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 5 deletions.
92 changes: 92 additions & 0 deletions Block/Snippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,96 @@

class Snippet extends AbstractBlock
{
public function getFullActionName()
{
return $this->getRequest()->getFullActionName();
}

public function getUseYotpoJsSdk()
{
return $this->_yotpoHelper->getUseYotpoJsSdk();
}

public function getLoadYotpoSnippet()
{
return $this->_yotpoHelper->getLoadYotpoSnippet();
}

/**
* Return true/false if the current page in the cart page
* @method shouldLoadSnippet
* @return bool
*/
public function isCartPage()
{
return $this->getFullActionName() === $this->_yotpoHelper->getCartPageFullActionName();
}

/**
* Return true/false if the current page in the checkout page
* @method shouldLoadSnippet
* @return bool
*/
public function isCheckoutPage()
{
return $this->getFullActionName() === $this->_yotpoHelper->getCheckoutPageFullActionName();
}

public function isPathMatchingSnippetPatterns()
{
$matching = false;

try {
$uri = $this->getRequest()->getRequestUri();
foreach ($this->_yotpoHelper->getLoadYotpoSnippetPathPatternsArray() as $pattern) {
if (\preg_match($pattern, $uri)) {
$matching = true;
break;
}
}
} catch (\Exception $e) {
$this->_yotpoHelper->log("[Yotpo - isPathMatchingSnippetPatterns - ERROR] " . $e->getMessage() . "\n" . $e->getTraceAsString(), "error");
}

return $matching;
}

/**
* Return true/false if the snippet should be loaded on the current page.
* @method shouldLoadSnippet
* @return bool (Default: true)
*/
public function shouldLoadSnippet()
{
if (!$this->isEnabled()) {
return false;
}

// If using Yotpo JS SDK - always load the snippet.
if ($this->getUseYotpoJsSdk()) {
return true;
}

// If not using Yotpo JS SDK - load the snippet by configuration.
switch ($this->getLoadYotpoSnippet()) {
case 'checkout':
return $this->isCheckoutPage();
break;

case 'checkout_cart':
return $this->isCartPage() || $this->isCheckoutPage();
break;

case 'url_path_patterns':
return $this->isPathMatchingSnippetPatterns();
break;

case 'all':
default:
return true;
break;
}

return true;
}
}
31 changes: 31 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class Data extends AbstractHelper
public const XML_PATH_SWELL_INSTANCE_ID = "yotpo_loyalty/advanced/swell_instance_id";
public const XML_PATH_DELETE_USED_COUPONS = "yotpo_loyalty/advanced/delete_used_coupons";
public const XML_PATH_USE_YOTPO_JS_SDK = "yotpo_loyalty/advanced/use_yotpo_js_sdk";
public const XML_PATH_LOAD_YOTPO_SNIPPET = "yotpo_loyalty/advanced/load_yotpo_snippet";
public const XML_PATH_CART_PAGE_FULL_ACTION_NAME = "yotpo_loyalty/advanced/cart_page_full_action_name";
public const XML_PATH_CHECKOUT_PAGE_FULL_ACTION_NAME = "yotpo_loyalty/advanced/checkout_page_full_action_name";
public const XML_PATH_LOAD_YOTPO_SNIPPET_PATH_PATTERNS = "yotpo_loyalty/advanced/load_yotpo_snippet_path_patterns";
//= Others
public const XML_PATH_CURRENCY_OPTIONS_DEFAULT = "currency/options/default";
public const XML_PATH_SECURE_BASE_URL = "web/secure/base_url";
Expand Down Expand Up @@ -273,6 +277,33 @@ public function getUseYotpoJsSdk($scope = null, $scopeId = null, $skipCahce = fa
return (bool) $this->getConfig(self::XML_PATH_USE_YOTPO_JS_SDK, $scope, $scopeId, $skipCahce);
}

public function getLoadYotpoSnippet($scope = null, $scopeId = null, $skipCahce = false)
{
return (string) $this->getConfig(self::XML_PATH_LOAD_YOTPO_SNIPPET, $scope, $scopeId, $skipCahce);
}

public function getCartPageFullActionName($scope = null, $scopeId = null, $skipCahce = false)
{
return (string) $this->getConfig(self::XML_PATH_CART_PAGE_FULL_ACTION_NAME, $scope, $scopeId, $skipCahce) ?: 'checkout_cart_index';
}

public function getCheckoutPageFullActionName($scope = null, $scopeId = null, $skipCahce = false)
{
return (string) $this->getConfig(self::XML_PATH_CHECKOUT_PAGE_FULL_ACTION_NAME, $scope, $scopeId, $skipCahce) ?: 'checkout_index_index';
}

public function getLoadYotpoSnippetPathPatterns($scope = null, $scopeId = null, $skipCahce = false)
{
return (string) $this->getConfig(self::XML_PATH_LOAD_YOTPO_SNIPPET_PATH_PATTERNS, $scope, $scopeId, $skipCahce);
}

public function getLoadYotpoSnippetPathPatternsArray($scope = null, $scopeId = null, $skipCahce = false)
{
$patterns = (array) explode(PHP_EOL, $this->getLoadYotpoSnippetPathPatterns($scope, $scopeId, $skipCahce));
$patterns = array_map('trim', $patterns);
return array_filter($patterns);
}

public function getDefaultCurrency($scope = null, $scopeId = null, $skipCahce = false)
{
return (string) $this->getConfig(self::XML_PATH_CURRENCY_OPTIONS_DEFAULT, $scope, $scopeId, $skipCahce);
Expand Down
34 changes: 34 additions & 0 deletions Model/Adminhtml/Source/LoadJsSnippet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Yotpo\Loyalty\Model\Adminhtml\Source;

use Magento\Framework\Option\ArrayInterface;

/**
* Yotpo Loyalty LoadJsSnippet source model.
*
* @category Loyalty
* @package Loyalty_Loyalty
*/
class LoadJsSnippet implements ArrayInterface
{
public function toOptionArray()
{
return [
['value' => 'all', 'label' => __('On All Pages')],
['value' => 'checkout', 'label' => __('Only on Checkout')],
['value' => 'checkout_cart', 'label' => __('Only on Checkout & Cart')],
['value' => 'url_path_patterns', 'label' => __('Only on Specified Paths (Using Regex)')],
];
}

public function toArray()
{
return [
'all' => __('On All Pages'),
'checkout' => __('Only on Checkout'),
'checkout_cart' => __('Only on Checkout & Cart'),
'url_path_patterns' => __('Only on Specified Paths (Using Regex)'),
];
}
}
1 change: 0 additions & 1 deletion Observer/Config/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class Save implements ObserverInterface
{

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "yotpo/magento2-module-yotpo-loyalty",
"description": "Magento 2 module for integration with Yotpo",
"type": "magento2-module",
"version": "1.4.6",
"version": "1.4.7",
"repositories": [{
"type": "git",
"url": "https://github.com/YotpoLtd/magento2-module-yotpo-loyalty"
Expand Down
29 changes: 29 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,35 @@
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[When set to 'Yes', <b>https://cdn-loyalty.yotpo.com/loader/{GUID}.js</b> would be loaded on all pages.]]></comment>
</field>
<field id="load_yotpo_snippet" translate="label comment" type="select" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Load Yotpo Snippet</label>
<source_model>Yotpo\Loyalty\Model\Adminhtml\Source\LoadJsSnippet</source_model>
<comment>Select where to load Yotpo's snippet and the "customer-identification" div (using an AJAX call to the module's session/snippet API endpoint).</comment>
<depends>
<field id="use_yotpo_js_sdk">0</field>
</depends>
</field>
<field id="cart_page_full_action_name" translate="label comment" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Cart Page - Full Action Name</label>
<comment>If your site is using a custom cart page that has a different full-action-name than "checkout_cart_index", please define it here.</comment>
<depends>
<field id="load_yotpo_snippet">checkout_cart</field>
</depends>
</field>
<field id="checkout_page_full_action_name" translate="label comment" type="text" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Checkout Page - Full Action Name</label>
<comment>If your site is using a custom checkout page that has a different full-action-name than "checkout_index_index", please define it here.</comment>
<depends>
<field id="load_yotpo_snippet" separator="|">checkout_cart|checkout</field>
</depends>
</field>
<field id="load_yotpo_snippet_path_patterns" translate="label comment" type="textarea" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Yotpo Snippet - Allowed URL Path patterns</label>
<comment><![CDATA[Add one or more Regex patterns (separated by new lines), that will be used for determining on which URL paths (Request URIs) the snippet will be loaded (include delimiters of your choice).<br><b>This is an advanced option. Be sure you know what you're doing.</b><br>*With the predefined example the snippet will be loaded on the default checkout page.]]></comment>
<depends>
<field id="load_yotpo_snippet">url_path_patterns</field>
</depends>
</field>
<field id="swell_merchant_info_confirm" translate="label" type="button" sortOrder="1000" showInDefault="1" showInWebsite="1" showInStore="1">
<frontend_model>Yotpo\Loyalty\Block\Adminhtml\System\Config\MerchantInfoConfirm</frontend_model>
</field>
Expand Down
4 changes: 4 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
</sync_settings>
<advanced>
<use_yotpo_js_sdk>0</use_yotpo_js_sdk>
<load_yotpo_snippet>all</load_yotpo_snippet>
<cart_page_full_action_name>checkout_cart_index</cart_page_full_action_name>
<checkout_page_full_action_name>checkout_index_index</checkout_page_full_action_name>
<load_yotpo_snippet_path_patterns>#/checkout/?(index/?|\?|$)#</load_yotpo_snippet_path_patterns>
</advanced>
</yotpo_loyalty>
<csp>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Yotpo_Loyalty" setup_version="1.4.6">
<module name="Yotpo_Loyalty" setup_version="1.4.7">
<sequence>
<module name="Magento_Catalog" />
</sequence>
Expand Down
4 changes: 2 additions & 2 deletions view/frontend/templates/snippet.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
* @var $block \Yotpo\Loyalty\Block\Snippet
*/
?>
<?php if (!$block->isEnabled()) {
<?php if (!$block->shouldLoadSnippet()) {
return;
} ?>
<!-- Yotpo Loyalty - Snippet Loader -->
<script>
(function () {
require([
"jquery",
'mage/storage'
'mage/storage'
],function($, storage) {
$(document).ready(function() {
try {
Expand Down

0 comments on commit 8833659

Please sign in to comment.