Code

Highlighted
New Member
11 0 0

Is this code right? Can I add it to additional scripts to Shopify, in order to send orders and items data?

<?php
/**
* Builds an Analytics Ecommerce addOrder action.
*
* @param array $order The completed order to report.
* @Return string The JavaScript representation of an Analytics Ecommerce addOrder action.
*/
function addOrderAction(&$order) {
$order_data = json_encode($order);

return "skroutz_analytics('ecommerce', 'addOrder', $order_data);";
}

/**
* Builds an Analytics Ecommerce addItem action.
*
* @param array $order The completed order to report.
* @param array $item The purchesed product to report, part of this order.
* @Return string The JavaScript representation of an Analytics Ecommerce addItem action.
*/
function addItemAction(&$order, &$item) {
$item_data = json_encode(array(
'order_id' => $order['order_id'],
'product_id' => $item['product_id'],
'name' => $item['name'],
'price' => $item['price'],
'quantity' => $item['quantity']
));

return "skroutz_analytics('ecommerce', 'addItem', $item_data);";
}
?>

<!--
Add this script at the final step of your checkout process, after a user has successfully
completed a purchase. Usually this is the "Thank You" or "Receipt" page.
-->
<script>
<?php
// Print the Analytics Ecommerce AddOrder action
echo addOrderAction($order);

// Print each Analytics Ecommerce AddItem action
foreach ($items as &$item) {
echo addItemAction($order, $item);
}
?>
</script>

0 Likes