简体   繁体   中英

Woocommerce custom function - Add to cart

We had been using a custom function in our theme.

    add_shortcode('add_to_cart_button', 'add_to_cart_func');

    function add_to_cart_func($atts) {

      $str = '<span class="new_product_header"><span class="button single_add_to_cart_button" onclick="jQuery(\'.single_add_to_cart_button \').click()">ADD TO CART</span></span>';

      return $str;

    }

Worked fine until last WP update, now when you click the button, it does an infinite loop of adding the product to the cart over and over.

Any ideas on why the infinite loop or a way to stop it?

There is a unique thing in your code on line

$str = '<span class="new_product_header"><span class="button single_add_to_cart_button" onclick="jQuery(\'.single_add_to_cart_button \').click()">ADD TO CART</span></span>';

You have attached click event to span having class .single_add_to_cart_button and on click event your trying to run following code same element.

jQuery('.single_add_to_cart_button').click();

So basically when your event fire it keep firing event.

Just remove

onclick="jQuery(\'.single_add_to_cart_button \').click()"

And all will be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM