Discount is not counted
-
Hello,
I am using this plugin to do site wide sicounts: https://wordpress.org/plugins/woo-discount-rules/Discounts are not displyed in data layer and only regular price is shown.
What class you use to get discounted price from product on single product page and in cart?Thank you!
-
The plugin, I am using does not update price at back end. So, the discounted price will might not be reflected.
I found this documentation to get price:
https://docs.flycart.org/en/articles/7907342-developer-documentation
GTM Kit is only adding discount based on coupons.
You can use the filter
gtmkit_datalayer_item_data(https://github.com/tlamedia/gtm-kit/blob/main/src/Integration/WooCommerce.php#L685) to add the discount and discounted price.Create a filter for
gtmkit_datalayer_item_datathat applies theadvanced_woo_discount_filters.Have you tried my sugestion?
Closing this
Sorry for late replay. Could you please provide me an example of how does this work? How one would apply the filter and and add extra data to it?
Thank you!I have managed to create and use the filter to add discount information like sale price and discount amount, but at the same time, I am getting several errors. Most likely because of my crooked code 😀
Here is the code:add_filter( 'gtmkit_datalayer_item_data', 'gtm_kit_edit', $priority = 10, $accepted_args = 3 ); function gtm_kit_edit( $item_data, $product, $event_context ) { if ( is_product() ) { $discount_data = []; $sale_price = round(apply_filters('advanced_woo_discount_rules_get_product_discount_price', $product->get_price(), $product),2); if ($sale_price != $item_data['price']) { $discount = round($item_data['price'] - $sale_price,2); $discount_data = [ 'sale_price' => $sale_price, 'discount' => $discount, ]; $item_data = array_merge( $item_data, $discount_data ); } return $item_data; } return null; }1) I am getting this crazy pile of code on the cart page for eample, catalog pages are also broken: https://drive.google.com/file/d/1A0OqDaJO1ckIyGQgfiWMtW3tOFdE0ufY/view?usp=drivesdk
2) Some undefined events started to be generated: https://drive.google.com/file/d/1oBYOWi4pPQA-61qaWl3azvXkhNM9Kwuo/view?usp=drivesdk
3) this ajax error appears when cart is not empty: https://drive.google.com/file/d/1vd1pBSXwFZSPjV4qlyDAmVJDnf_xU1K0/view?usp=drivesdk
Try this:
function gtm_kit_edit( array $item_data, WC_Product $product, string $event_context ): array { $sale_price = round(apply_filters('advanced_woo_discount_rules_get_product_discount_price', $product->get_price(), $product),2); if ($sale_price != $item_data['price']) { $discount = round($item_data['price'] - $sale_price,2); $item_data['sale_price'] = $sale_price; $item_data['discount'] = $discount; } return $item_data; }You must always return the first parameter of a filter, which in this case is
$item_data.$productis always of the typeWC_Productso there is no need to test that.is_product()checks if you are on a product page so on all other types of pages you returnednullwhich is the root cause of you problems.That works perfectly now. Thank you.
Just to make sure, this code only gets run on woocommerce pages as it is a filter to your function that is run on woo pages only?Also how can I round up the value in cart/payment pages: https://drive.google.com/file/d/1jCUJDqmE0E5xbBTpKrEyUDrLZIFFk4VY/view?usp=drivesdk
And is it possible to add total discount value for whole cart event?
Have also noticed, that quantity is not being added from catalog side.
-
This reply was modified 2 years, 5 months ago by
raulis.
The code only runs on Woo pages when GTM Kit is active. So if you deactivate GTM Kit your site will function as normal.
You can filter the whole data layer using the filter
gtmkit_datalayer_contentchange anything you want.I can confirm that quantity is not being added from a catalog page and i will make a fix.
The quantity bug is now fixed and vill be included in the next release
I also noticed, that on every quantity increase there is an undefined event created. Not sure if it is intended so.
In GTM or a console error?
I see in console. Not sure about GTM.
Would it be possible to add select_item event when item in catalog is clicked?
Closing this as resolved
-
This reply was modified 2 years, 5 months ago by
The topic ‘Discount is not counted’ is closed to new replies.