Skip to main content

Analytics tag begin checkout

More
5 years 8 months ago #328373 by marchela-apostolova
-- HikaShop version -- : 4.4.0

Hello,

I try to create marker in Analytics in the following file show_block_cart.php

Here is the code:
Code:
gtag('event', 'begin_checkout', { "items": [ <?php $count = count($cart->products); ?> <?php foreach($cart->products as $i => $product){ ?> { "id": "<?php echo $product->product_id; ?>", "name": "<?php echo strip_tags($product->product_name); ?>", "quantity": <?php echo $product->cart_product_quantity; ?>, "price": '<?php echo $product->prices[0]->price_value; ?>' }<?php if ($i > $count - 1) echo ","; ?> <?php $k = 1-$k; } ?> ], "coupon": "<?php echo $cart->coupon->discount_value;?>" });

For products without Characteristics, everything works fine.

For products with Characteristics: in the script, there is the selected variant from the shopping cart + the main product with 0 quantity (which is not in the shopping cart).
Code:
gtag('event', 'begin_checkout', { "items": [ { "id": "81", "name": "Dress color: black: S", "quantity": 1, "price": '29' }, { "id": "13", "name": "Bag", "quantity": 1, "price": '28' }, { "id": "80", "name": "Dress color: black", "quantity": 0, "price": '29.00000' } ], "coupon": "5.7" });

Can you help me with this issue?


Best regards!

Please Log in or Create an account to join the conversation.

More
5 years 8 months ago #328386 by nicolas
Hi,

You just need to skip the products with a quantity of 0 with such line of code at the beginning of your foreach:
if(empty($product->cart_product_quantity)) continue;

Please Log in or Create an account to join the conversation.

More
5 years 8 months ago #328506 by marchela-apostolova
Thank you so much, Nicolas!

Probably in the shoppinng cart there is information for the main product, even if it's not visualized there.

Now it worked that way, but I seem to have problem with the product counting: at the end of the product list after the last product again there is , (comma).

Probably it still counts the main products as well with 0 quantity.
Code:
gtag('event', 'begin_checkout', { "items": [ { "id": "81", "name": "Dress color: black: S", "quantity": 1, "price": '29' }, { "id": "13", "name": "Bag", "quantity": 1, "price": '28' }, ], "coupon": "5.7" });

Do you have any idea how can I fix this? 

Please Log in or Create an account to join the conversation.

More
5 years 8 months ago - 5 years 8 months ago #328513 by nicolas
Hi,

Well, you can't rely on $count. But the solution to your issue is PHP coding.
There are two ways to do it:
- instead of directly outputting the text, you could have an array where you would add the text for each product to be added.
And then, you would do a "echo implode(',', $array);" to output the text with a comma between each.
- instead of adding the comma at the end of the loop, you could add it at the beginning and just have a flag variable $first = true; before the loop. and in the loop you would have at the beginning : if($first) { $first = false; } else { echo ','; }
Last edit: 5 years 8 months ago by nicolas.

Please Log in or Create an account to join the conversation.

Time to create page: 0.217 seconds
Powered by Kunena Forum