Skip to content

Commit

Permalink
Merge pull request #77 from YotpoLtd/v1.4.12
Browse files Browse the repository at this point in the history
v1.4.12: Refactored snippet loader to work better with vanilla JS when using Yotpo JS SDK.
  • Loading branch information
pniel-cohen authored Oct 23, 2024
2 parents 3c409b3 + 0e45be3 commit 4b61e48
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
32 changes: 13 additions & 19 deletions Model/Api/Swell/Session/SnippetManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,25 +142,17 @@ public function getSnippet()
$response = [
"error" => 0,
"snippet" => "",
"invalidate_customer_cart" => false,
"scripts" => [],
];
try {
if ($this->isEnabled()) {

if ($this->isForceCartReload()) {
$this->setForceCartReload(0);
$response["snippet"] .= '
<!-- Yotpo Loyalty - Reload customerData cart -->
<script>
(function () {
require([
"Magento_Customer/js/customer-data"
],function(customerData) {
customerData.invalidate(["cart"]);
});
})();
</script>
<!--/ Yotpo Loyalty - Reload customerData cart -->
';
$response["invalidate_customer_cart"] = true;
}

if (($swellGuid = $this->getSwellGuid()) && ($swellApiKey = $this->getSwellApiKey())) {
$response["snippet"] .= '
<!-- Yotpo Loyalty - Swell Snippet -->
Expand All @@ -182,15 +174,17 @@ public function getSnippet()
></div>
';

if ($this->getUseYotpoJsSdk()) {
$response["snippet"] .= '
<script type="text/javascript" async src="https://cdn-loyalty.yotpo.com/loader/' . $swellGuid . '.js"></script>
';
}

$response["snippet"] .= '
<!--/ Yotpo Loyalty - Swell Snippet -->
';

// Additional scripts to load
if ($this->getUseYotpoJsSdk()) {
$response["scripts"][] = [
'src' => 'https://cdn-loyalty.yotpo.com/loader/' . $swellGuid . '.js',
'async' => true,
];
}
}
}
} catch (\Exception $e) {
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.11",
"version": "1.4.12",
"repositories": [{
"type": "git",
"url": "https://github.com/YotpoLtd/magento2-module-yotpo-loyalty"
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.11">
<module name="Yotpo_Loyalty" setup_version="1.4.12">
<sequence>
<module name="Magento_Catalog" />
</sequence>
Expand Down
31 changes: 27 additions & 4 deletions view/frontend/templates/snippet.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,33 @@ $scriptContent = "
throw new Error('Failed to parse JSON response: ' + e.message);
}
if (!response.error && response.snippet) {
var snippet = document.createElement('div');
snippet.innerHTML = response.snippet;
document.body.appendChild(snippet);
if (!response.error) {
if (
typeof require !== 'undefined' && typeof requirejs !== 'undefined' &&
response.invalidate_customer_cart
) {
require(['Magento_Customer/js/customer-data'], function(customerData) {
customerData.invalidate(['cart']);
});
}
if (response.snippet) {
var snippet = document.createElement('div');
snippet.innerHTML = response.snippet;
document.body.appendChild(snippet);
}
if (response.scripts && Array.isArray(response.scripts)) {
response.scripts.forEach(function(scriptObj) {
if (scriptObj.src) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = !!scriptObj.async;
script.src = scriptObj.src;
document.head.appendChild(script);
}
});
}
} else {
throw response || 'Unknown error occurred';
}
Expand Down

0 comments on commit 4b61e48

Please sign in to comment.