Skip to content

Commit

Permalink
Merge pull request #719 from Nosto/hotfix/5.1.0
Browse files Browse the repository at this point in the history
Hotfix 5.1.0 - Fix adding multiple product to cart issue
  • Loading branch information
olsi-qose authored Dec 7, 2020
2 parents 7d763b3 + 164d9ba commit db7008d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: '7.2'
tools: composer, prestissimo, pecl
tools: composer:v1, prestissimo, pecl
coverage: none
extensions: ast

Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
All notable changes to this project will be documented in this file. This project adheres to Semantic Versioning.

# 5.1.0
* Fix addMultipleProductsToCart issue happening in M2 cloud

# 5.0.8
* Bump dependencies to be compatible with Nosto CMP module

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": "nosto/module-nostotagging",
"description": "Increase your conversion rate and average order value by delivering your customers personalized product recommendations throughout their shopping journey.",
"type": "magento2-module",
"version": "5.0.8",
"version": "5.1.0",
"require-dev": {
"php": ">=7.1.0",
"phpmd/phpmd": "^2.5",
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@
<!--suppress XmlUnboundNsPrefix -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Nosto_Tagging" setup_version="5.0.8"/>
<module name="Nosto_Tagging" setup_version="5.1.0"/>
</config>
4 changes: 1 addition & 3 deletions view/frontend/templates/addtocart.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ use Nosto\Tagging\Block\Addtocart;
?>
<form action="<?= $block->escapeUrl($block->getSubmitUrl()) ?>" method="post" id="nosto_addtocart_form">
<?= $block->getBlockHtml('formkey') ?>
<input type="hidden" name="qty" value="1">
<input type="hidden" name="product">
</form>
<script type="text/javascript">
require(['recobuy'], function (module) {
window.Nosto = module;
})
</script>
</script>
66 changes: 40 additions & 26 deletions view/frontend/web/js/recobuy.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,56 +35,70 @@

define([
'nostojs',
'jquery',
'catalogAddToCart'
'jquery'
], function (nostojs, $) {
'use strict';

//noinspection SpellCheckingInspection
var form = $('#nosto_addtocart_form').catalogAddToCart({});
var Recobuy = {};
Recobuy.addProductToCart = function (productId, element, quantity) {
quantity = quantity || 1;

Recobuy.addProductToCart = function (productId, element, quantity = 1) {
var productData = {
"productId" : productId,
'skuId' : productId
productId : productId,
skuId : productId,
quantity: quantity
};
Recobuy.addSkuToCart(productData, element, quantity);
return Recobuy.addSkuToCart(productData, element);
};

// Products must be and array of objects [{'productId': '123', 'skuId': '321'}, {...}]
// skuId is optional for simple products.
Recobuy.addMultipleProductsToCart = function (products, element) {
if (Array.isArray(products)) {
products.forEach(function (productObj) {
Recobuy.addSkuToCart(productObj, element, 1);
});
return products.reduce(function(acc, product) {
return acc.then(function() {
return Recobuy.addSkuToCart(product, element)
})
} , Promise.resolve())
} else {
Promise.reject(new Error("Products is not type array"))
}
};

// Product object must have fields productId and skuId {'productId': '123', 'skuId': '321'}
Recobuy.addSkuToCart = function (product, element, quantity) {
quantity = quantity || 1;
Recobuy.addSkuToCart = function (product, element) {

var quantity = product.quantity || 1;
var url = document.querySelector("#nosto_addtocart_form").getAttribute("action")
var formKey = document.querySelector("#nosto_addtocart_form > input[name='form_key']").getAttribute("value")


return new Promise(function (resolve, reject) {
$.post(url, {
form_key: formKey,
qty: quantity,
product: product.productId,
sku: product.skuId
}).done(function() {
Recobuy.sendCartEvent(element, product.productId)
return resolve()
}).fail(function () {
return reject()
})
})

};

Recobuy.sendCartEvent = function (element, productId) {
if (typeof element === 'object' && element) {
var slotId = this.resolveContextSlotId(element);
if (slotId) {
nostojs(function (api) {
api.recommendedProductAddedToCart(product.productId, slotId);
api.recommendedProductAddedToCart(productId, slotId);
});
}
}
}

form.find('input[name="product"]').val(product.productId);
var productSku = document.createElement("input");
productSku.setAttribute("type", "hidden");
productSku.setAttribute("name", 'sku');
productSku.setAttribute("value", product.skuId);
form.append(productSku);

form.find('input[name="qty"]').val(quantity);
form.catalogAddToCart('ajaxSubmit', form);
};

Recobuy.resolveContextSlotId = function (element) {
var m = 20;
var n = 0;
Expand Down

0 comments on commit db7008d

Please sign in to comment.