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