'Tracking in Woocommerce
Hello brilliant people.
i have a problem here with tracking on the cart page. I need to send data to my third party crm.
it is an addtobasket event, but it needs to run on the basket page and send all the basket items.
i have tried with this, but it sends the same 1 productid everytime instead of looping through it:
add_action('wp_footer', 'zz_add_tracking_codes3');
function zz_add_tracking_codes3(){
// adding on product page only
if(Is_cart){
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values){
$produc_id = get_the_id();
$categories = get_the_terms( $produc_id, 'product_cat');
?>
<script type="text/javascript" language="javascript">
const PRODUCTID = '<?php echo $produc_id; ?>';
const CATEGORYID = '<?php echo $categories[0]->term_id; ?>';
const CATEGORYNAME = '<?php echo $categories[0]->name; ?>';
var hlt;
var BiTrackingLoaded = function (BiTracking) {
hlt=BiTracking;
BiTracking.initialize('c5gizkvDg3PurFHH', 1, '7468');
hlt.addToBasket(PRODUCTID, PRODUCT COUNT, 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
}}}
and in the console it says product id has already been declared when adding more items.
You can check by going to frkfaerch.dk
EDIT: it actually also doesnt correctly get the id of the product, it seems to be getting the pageid.
/*Produkt side tracking*/
add_action('wp_footer', 'zz_add_tracking_codes');
function zz_add_tracking_codes(){
// adding on product page only
if(is_singular( 'product' )){
$produc_id = get_the_id();
$categories = get_the_terms( $produc_id, 'product_cat');
?>
<script type="text/javascript" language="javascript">
const PRODUCTID = '<?php echo $produc_id; ?>';
const CATEGORYID = '<?php echo $categories[0]->term_id; ?>';
const CATEGORYNAME = '<?php echo $categories[0]->name; ?>';
var hlt;
var BiTrackingLoaded = function (BiTracking) {
hlt=BiTracking;
BiTracking.initialize('c5gizkvDg3PurFHH', 1, '7468');
hlt.visit(PRODUCTID,CATEGORYID,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
}
}
This code is running in product pages and works, sending productid, categoryname and category id in the payload.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
