Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leftis/ba6cffb1d488094d0c775212760b373c to your computer and use it in GitHub Desktop.
Save leftis/ba6cffb1d488094d0c775212760b373c to your computer and use it in GitHub Desktop.
<?php
add_action('woocommerce_thankyou', 'order_received', 10, 1);
function order_received($order_id)
{
$order = wc_get_order($order_id);
$order_data = $order->get_data();
$quantity = $order->get_item_count();
// Needed variables
$line_items = $order_data['line_items'];
$order_total = $order_data['total'];
$product_ids = array();
$product_quantities = array();
foreach ($line_items as $prod) {
array_push($product_ids, $prod->get_product_id('view'));
array_push($product_quantities, $prod->get_quantity('view'));
}
?>
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
cart: {
productIds: <?php echo json_encode($product_ids) ?>,
productQuantities: <?php echo json_encode($product_quantities) ?>,
productsLength: <?php echo $quantity ?>,
value: <?php echo $order_total ?>,
}
});
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment