Skip to content

Commit a1ac1e7

Browse files
authored
issue-resolution/fixed issue with add to cart tracking on (#182)
1 parent 3747622 commit a1ac1e7

File tree

1 file changed

+40
-37
lines changed

1 file changed

+40
-37
lines changed

app/code/Meta/Conversion/view/frontend/templates/pixel/add_to_cart.phtml

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,47 +31,50 @@
3131
var product_info_url = '<?= /* @noEscape */ $block->getProductInfoUrl() ?>';
3232

3333
$(document).on('ajax:addToCart', function (event, data) {
34-
// check product data from swatch widget
35-
var swatchData = $('[data-role=swatch-options]').data('mage-SwatchRenderer');
36-
if (swatchData) {
37-
var simpleProductId = swatchData.getProductId();
38-
} else {
39-
// In case product is added from category page get options using added product's id
40-
// Get id of last added product
41-
var productId = data.productIds[data.productIds.length - 1];
42-
var categorySwatchData = $('[data-role=swatch-option-'+productId+']').data('mage-SwatchRenderer');
43-
if (categorySwatchData) {
44-
var simpleProductId = categorySwatchData.getProductId();
34+
if (!data.response.backUrl) {
35+
// check product data from swatch widget
36+
var swatchData = $('[data-role=swatch-options]').data('mage-SwatchRenderer');
37+
if (swatchData) {
38+
var simpleProductId = swatchData.getProductId();
4539
} else {
46-
// else check product data from configurable options
47-
var configurableProduct = data.form.data().mageConfigurable;
48-
if(configurableProduct) {
49-
var simpleProductId = configurableProduct.simpleProduct;
40+
// In case product is added from category page get options using added product's id
41+
// Get id of last added product
42+
var productId = data.productIds[data.productIds.length - 1];
43+
var categorySwatchData = $('[data-role=swatch-option-'+productId+']')
44+
.data('mage-SwatchRenderer');
45+
if (categorySwatchData) {
46+
var simpleProductId = categorySwatchData.getProductId();
47+
} else {
48+
// else check product data from configurable options
49+
var configurableProduct = data.form.data().mageConfigurable;
50+
if(configurableProduct) {
51+
var simpleProductId = configurableProduct.simpleProduct;
52+
}
5053
}
5154
}
55+
// otherwise use the product sku
56+
var product_sku = data.sku;
57+
var form_key = jQuery("[name='form_key']").val();
58+
$.ajax({
59+
url: product_info_url,
60+
data: {
61+
product_sku: product_sku,
62+
product_id: simpleProductId,
63+
form_key: form_key
64+
},
65+
type: 'post',
66+
dataType: 'json',
67+
success: function (res) {
68+
trackAddToCart(
69+
[res.id],
70+
res.name,
71+
res.content_category,
72+
res.value,
73+
res.event_id
74+
);
75+
}
76+
});
5277
}
53-
// otherwise use the product sku
54-
var product_sku = data.sku;
55-
var form_key = jQuery("[name='form_key']").val();
56-
$.ajax({
57-
url: product_info_url,
58-
data: {
59-
product_sku: product_sku,
60-
product_id: simpleProductId,
61-
form_key: form_key
62-
},
63-
type: 'post',
64-
dataType: 'json',
65-
success: function (res) {
66-
trackAddToCart(
67-
[res.id],
68-
res.name,
69-
res.content_category,
70-
res.value,
71-
res.event_id
72-
);
73-
}
74-
});
7578
});
7679
});
7780
</script>

0 commit comments

Comments
 (0)