Adding variations to dataLayer in WooCommerce (Add to cart event)

80 Views Asked by At

I'm using the code below which works great for simple products but doesn't log multiple variations when selected and added to cart. How can I modify the code so that the specific variations get pushed to the datalayer?

<?php
$product = wc_get_product( $post );
$terms = get_the_terms( $product->get_id(), 'product_cat' );
$term = $terms && $terms[0]->name != 'all' ? $terms[0]->name : $terms[1]->name;
?>
<script>
window.dataLayer.push({
  event: 'pin_cart',
  value: <?php echo $product->get_price(); ?>,
  currency: 'CAD',
  order_quantity: 1,
  line_items: [{
    product_name: '<?php echo $product->get_name(); ?>',
    product_id: <?php echo $product->get_id(); ?>,
    product_category: '<?php echo $term; ?>'
  }]
});
</script>
0

There are 0 best solutions below