• Resolved Paul

    (@paulschiretz)


    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)
  • Hey @paulschiretz,

    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.

    Many thanks for reporting this issue and for sharing all that detail!

    We have now merged a fix which will be included in the next release of the extension.

    Plugin Support Kaushik S. a11n

    (@kaushiksomaiya)

    Hello @paulschiretz,

    We are pleased to inform you that the issue you reported has been addressed, and we have released version 2.0.2, which includes the fix. I will be marking this thread as resolved. Should you encounter any further concerns, please don’t hesitate to reach out.

    Also, if you enjoy using Google Analytics for WooCommerce, we would greatly appreciate it if you could take a moment to leave us a review. Your feedback is valuable to us.

    Wishing you a wonderful day ahead! ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘JS errors on product pages’ is closed to new replies.