Skip to content

Commit 601a486

Browse files
authored
Improved add to cart stability (#763)
* improved add to cart stability * use last added product id if swatch data is not available
1 parent edcbb60 commit 601a486

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

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

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
<?php
2+
3+
use Magento\Framework\Escaper;
4+
use Meta\Conversion\Block\Pixel\AddToCart;
5+
26
/**
3-
* @var \Meta\Conversion\Block\Pixel\AddToCart $block
4-
* @var $escaper \Magento\Framework\Escaper
7+
* @var AddToCart $block
8+
* @var Escaper $escaper
59
*/
10+
611
$trackerUrl = $block->getTrackerUrl();
712
?>
813
<?php if ($block->getFacebookPixelID()): ?>
914
<!-- Added the new component below to track server events -->
1015
<script>
1116
window.addToCartData =
1217
<?= /* @noEscape */ json_encode([
13-
"url" => $escaper->escapeUrl($trackerUrl),
14-
"eventName" => $block->getEventToObserveName(),
18+
'url' => $escaper->escapeUrl($trackerUrl),
19+
'eventName' => $block->getEventToObserveName(),
1520
'fbAgentVersion' => $block->getFacebookAgentVersion(),
1621
'fbPixelId' => $block->getFacebookPixelID(),
1722
'source' => $block->getSource(),
1823
'pluginVersion' => $block->getPluginVersion(),
1924
'track' => 'track',
2025
'event' => 'AddToCart'
21-
2226
]); ?>
2327
</script>
2428
<script>
@@ -27,33 +31,38 @@ $trackerUrl = $block->getTrackerUrl();
2731
'Meta_Conversion/js/metaPixelTracker'
2832
], function ($, metaPixelTracker) {
2933

30-
var product_info_url = '<?= /* @noEscape */ $block->getProductInfoUrl() ?>';
34+
const product_info_url = '<?= /* @noEscape */ $block->getProductInfoUrl() ?>';
3135

3236
$(document).on('ajax:addToCart', function (event, data) {
37+
let simpleProductId;
38+
// Get id of last added product
39+
const productId = data.productIds[data.productIds.length - 1];
3340
if (!data.response.backUrl) {
3441
// check product data from swatch widget
35-
var swatchData = $('[data-role=swatch-options]').data('mage-SwatchRenderer');
36-
if (swatchData) {
37-
var simpleProductId = swatchData.getProductId();
42+
const swatchData = $('[data-role=swatch-options]').data('mage-SwatchRenderer');
43+
if (swatchData && swatchData.getProductId()) {
44+
simpleProductId = swatchData.getProductId();
3845
} else {
3946
// 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 + ']')
47+
const categorySwatchData = $('[data-role=swatch-option-' + productId + ']')
4348
.data('mage-SwatchRenderer');
44-
if (categorySwatchData) {
45-
var simpleProductId = categorySwatchData.getProductId();
49+
if (categorySwatchData && categorySwatchData.getProductId()) {
50+
simpleProductId = categorySwatchData.getProductId();
4651
} else {
4752
// else check product data from configurable options
48-
var configurableProduct = data.form.data().mageConfigurable;
53+
const configurableProduct = data.form.data().mageConfigurable;
4954
if (configurableProduct) {
50-
var simpleProductId = configurableProduct.simpleProduct;
55+
simpleProductId = configurableProduct.simpleProduct;
5156
}
5257
}
5358
}
5459
// otherwise use the product sku
55-
var product_sku = data.sku;
56-
var form_key = jQuery("[name='form_key']").val();
60+
const product_sku = data.sku;
61+
// use last added product id if swatch data is not available
62+
if (!simpleProductId) {
63+
simpleProductId = productId;
64+
}
65+
const form_key = jQuery("[name='form_key']").val();
5766
$.ajax({
5867
url: product_info_url,
5968
data: {
@@ -64,7 +73,7 @@ $trackerUrl = $block->getTrackerUrl();
6473
type: 'get',
6574
dataType: 'json',
6675
success: function (res) {
67-
var addToCartConfigPixel = {
76+
const addToCartConfigPixel = {
6877
"url": window.addToCartData.url,
6978
"payload": {
7079
"eventName": window.addToCartData.eventName,

0 commit comments

Comments
 (0)