gtm4wp
gtm4wp copied to clipboard
Woocommerce add-to-cart shortcode issue
Moved from WP.org support forum: https://wordpress.org/support/topic/woocommerce-add-to-cart-shortcode-ecommerce-add-products-must-have-a-name/
When using the woocommerce add-to-cart shortcode – Register by clicking add to cart [add_to_cart sku=”xxx-xxx-xxx” – the following error is received:
Google – dataLayer.push(): event: gtm4wp.addProductToCartEEC a.js:6 ~ ERR: ecommerce.add.products must have a name or ID a.js:6 ~ Object:
{
event: “gtm4wp.addProductToCartEEC”,
ecommerce: {
currencyCode: “CAD”,
add: {
products: [{
“quantity: 1
}]
}
}
If the shortcode is changed to use the product_page shortcode, all is good.
Unfortunately the add-to-cart shortcode does not include any hook that could be used to directly alter the behavior of the button:
https://github.com/woocommerce/woocommerce/blob/857c5cbc5edc0451cf965b19788e3993804d4131/includes/class-wc-shortcodes.php
It calls woocommerce_template_loop_add_to_cart()
which is a standard method to output the add to cart button. Here we have a filter called woocommerce_loop_add_to_cart_args
but using this filter also changes HTML output for normal add to cart buttons.
What could be done is to move product attributes from the <span class="gtm4wp_productdata"... >
element into the add to cart button and rewrite all JS event handlers to use this data storage instead of the hidden <span>
element.
This needs extensive testing thus v1.11 is not the right time but perhaps v1.12 or a alter version could implement this change.