JS errors on product pages
-
Hi there,
Adding a product to the cart on single product pages causes a js error.The cause is that the classic single product pages have the product id as value of the add_to_cart button rather than a separate data-product_id attribute. But parsing in classic.js is always done on the product_id attribute as can be seen here:
// Handle runtime cart events. /** * Track the custom add to cart event dispatched by WooCommerce Core * * @param {Event} e - The event object * @param {Object} fragments - An object containing fragments of the updated cart. * @param {string} cartHash - A string representing the hash of the cart after the update. * @param {HTMLElement[]} button - An array of HTML elements representing the add to cart button. */ document.body.onadded_to_cart = ( e, fragments, cartHash, button ) => { tracker.eventHandler( 'add_to_cart' )( { product: getProductFromID( parseInt( button[ 0 ].dataset.product_id ), products, cart ), } ); };
A workaround is to add the product_id attribute in the single-product/add-to-cart/simple.php template.
<button type="submit" name="add-to-cart" data-product_id="<?php echo esc_attr( $product->get_id() ); ?>" value="<?php echo esc_attr( $product->get_id() ); ?>" class="single_add_to_cart_button button alt<?php echo esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '' ); ?>"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
But i would appreciate if thats fixed in the js of the plugin!
Thanks,
Paul
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘JS errors on product pages’ is closed to new replies.