Skip to content

Commit 1d35626

Browse files
authored
Merge pull request #844 from magento-commerce/merge-pr-95
PR #58 authored by @Jmencab: Handling Response.backUrl Undefined in AddToCart #95
2 parents 4e98a94 + 2b7221d commit 1d35626

File tree

1 file changed

+62
-61
lines changed

1 file changed

+62
-61
lines changed

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

Lines changed: 62 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -37,71 +37,72 @@ $trackerUrl = $block->getTrackerUrl();
3737
let simpleProductId;
3838
// Get id of last added product
3939
const productId = data.productIds[data.productIds.length - 1];
40-
// check product data from swatch widget
41-
const swatchData = $('[data-role=swatch-options]').data('mage-SwatchRenderer');
42-
if (swatchData && swatchData.getProductId()) {
43-
simpleProductId = swatchData.getProductId();
40+
// check product data from swatch widget
41+
const swatchData = $('[data-role=swatch-options]').data('mage-SwatchRenderer');
42+
if (swatchData && swatchData.getProductId()) {
43+
simpleProductId = swatchData.getProductId();
44+
} else {
45+
// In case product is added from category page get options using added product's id
46+
const categorySwatchData = $('[data-role=swatch-option-' + productId + ']')
47+
.data('mage-SwatchRenderer');
48+
if (categorySwatchData && categorySwatchData.getProductId()) {
49+
simpleProductId = categorySwatchData.getProductId();
4450
} else {
45-
// In case product is added from category page get options using added product's id
46-
const categorySwatchData = $('[data-role=swatch-option-' + productId + ']')
47-
.data('mage-SwatchRenderer');
48-
if (categorySwatchData && categorySwatchData.getProductId()) {
49-
simpleProductId = categorySwatchData.getProductId();
50-
} else {
51-
// else check product data from configurable options
52-
const configurableProduct = data.form.data().mageConfigurable;
53-
if (configurableProduct) {
54-
simpleProductId = configurableProduct.simpleProduct;
55-
}
51+
// else check product data from configurable options
52+
const configurableProduct = data.form.data().mageConfigurable;
53+
if (configurableProduct) {
54+
simpleProductId = configurableProduct.simpleProduct;
5655
}
5756
}
58-
// otherwise use the product sku
59-
const product_sku = data.sku;
60-
// use last added product id if swatch data is not available
61-
if (!simpleProductId) {
62-
simpleProductId = productId;
63-
}
64-
const form_key = jQuery("[name='form_key']").val();
65-
$.ajax({
66-
url: product_info_url,
67-
data: {
68-
product_sku: product_sku,
69-
product_id: simpleProductId,
70-
form_key: form_key
71-
},
72-
type: 'get',
73-
dataType: 'json',
74-
async: !data.response.backUrl,
75-
success: function (res) {
76-
const addToCartConfigPixel = {
77-
"url": window.addToCartData.url,
78-
"payload": {
79-
"eventName": window.addToCartData.eventName,
80-
"productId": res.productId
81-
},
82-
"browserEventData": {
83-
'fbAgentVersion': window.addToCartData.fbAgentVersion,
84-
'fbPixelId': window.addToCartData.fbPixelId,
85-
'source': window.addToCartData.source,
86-
'pluginVersion': window.addToCartData.pluginVersion,
87-
'track': window.addToCartData.track,
88-
'event': window.addToCartData.event,
89-
'payload': {
90-
"content_name": res.name,
91-
"content_ids": [res.id],
92-
"value": res.value,
93-
"currency": res.currency,
94-
"content_type": res.content_type,
95-
"contents": [{
96-
"id": res.id,
97-
"quantity": 1
98-
}]
99-
}
57+
}
58+
// otherwise use the product sku
59+
const product_sku = data.sku;
60+
// use last added product id if swatch data is not available
61+
if (!simpleProductId) {
62+
simpleProductId = productId;
63+
}
64+
const form_key = jQuery("[name='form_key']").val();
65+
const runAsync = !data?.response?.backUrl;
66+
$.ajax({
67+
url: product_info_url,
68+
data: {
69+
product_sku: product_sku,
70+
product_id: simpleProductId,
71+
form_key: form_key
72+
},
73+
type: 'get',
74+
dataType: 'json',
75+
async: runAsync,
76+
success: function (res) {
77+
const addToCartConfigPixel = {
78+
"url": window.addToCartData.url,
79+
"payload": {
80+
"eventName": window.addToCartData.eventName,
81+
"productId": res.productId
82+
},
83+
"browserEventData": {
84+
'fbAgentVersion': window.addToCartData.fbAgentVersion,
85+
'fbPixelId': window.addToCartData.fbPixelId,
86+
'source': window.addToCartData.source,
87+
'pluginVersion': window.addToCartData.pluginVersion,
88+
'track': window.addToCartData.track,
89+
'event': window.addToCartData.event,
90+
'payload': {
91+
"content_name": res.name,
92+
"content_ids": [res.id],
93+
"value": res.value,
94+
"currency": res.currency,
95+
"content_type": res.content_type,
96+
"contents": [{
97+
"id": res.id,
98+
"quantity": 1
99+
}]
100100
}
101-
};
102-
metaPixelTracker(addToCartConfigPixel);
103-
}
104-
});
101+
}
102+
};
103+
metaPixelTracker(addToCartConfigPixel);
104+
}
105+
});
105106
});
106107
});
107108
</script>

0 commit comments

Comments
 (0)