Tracking multiple products on cart page

Viewed 34

i am trying to add some tracking on the cart page of woocommerce, to track all products and send them to a crm system so we can use abandoned cart mails.

i have managed to get the tracking information to send correctly, but i need it to loop over the products in the cart and send the data for each of the products, this only sends the first product in the cart

this is the code i have added:

/*Kurv tracking*/
add_action('wp_footer', 'zz_add_tracking_codes3');
function zz_add_tracking_codes3(){
   
    if(Is_cart){
        global $woocommerce;
   foreach( WC()->cart->get_cart() as $cart_item ){
    $product_id = $cart_item['product_id'];
    $productcount = $cart_item['quantity'];


        $categories = get_the_terms( $product_id, 'product_cat');
    ?>
        <script type="text/javascript" language="javascript">
        const PRODUCTID = '<?php echo $product_id; ?>';
        const CATEGORYID = '<?php echo $categories[0]->term_id; ?>';
        const CATEGORYNAME = '<?php echo $categories[0]->name; ?>';
        const PRODUCTCOUNT = '<?php echo $productcount?>';
        var hlt;
        var BiTrackingLoaded = function (BiTracking) {
        hlt=BiTracking;
        BiTracking.initialize('trackingid', 1, '7468');
        hlt.addToBasket(PRODUCTID,PRODUCTCOUNT,CATEGORYID,'BASKETID',CATEGORYNAME);  }
        </script>
        <script>
        (function (d, t) {
        var g = d.createElement(t),
        s = d.getElementsByTagName(t)[0];
        h = "https:" == document.location.protocol ? "https://" : "http://";
        g.src = h + 'tracking.heycommerce.dk/hlbi.js';
        s.parentNode.insertBefore(g, s);
        }
        (document, 'script'));
        </script>
    <?php
    }
}}
0 Answers
Related