<?php
function product_item_data( $atts ) {
$cart = WC()->cart->get_cart();
?>
<script>
dataLayer.push({ ecommerce: null });
dataLayer.push({
event: "view_item_list",
ecommerce: {
items: [
<?php
foreach( $cart as $cart_item_key => $cart_item ){
$product = $cart_item['data'];
$euro = "€";
$attributes = $product->get_attributes();
?>
{
item_id: "<?php echo $product->get_sku(); ?>",
item_name: "<?php echo $product->get_name(); ?>",
currency: "€",
material: "<?php echo $attributes["pa_materiaal"]; ?>",
size: "<?php echo $attributes["pa_size"]; ?>",
price: <?php echo $product->get_price(); ?>,
},
<?php
}
?>
}
});
</script>
<?php
}
add_shortcode( 'cart_data', 'product_item_data');
?>