javascript - Avoid "wc_add_to_cart_params is not defined" error in WooCommerce - Stack Overflow

I'm trying to add products to the cart in woomerce, but am getting a persistent error Uncaught Ref

I'm trying to add products to the cart in woomerce, but am getting a persistent error Uncaught ReferenceError: wc_add_to_cart_params is not defined.

Have used this approach many times before without issue, has wc_add_to_cart_params been removed from woomerce? If so what is the replacement?

My code is as follows:

  var productId = form.find('input[name=product_id]').val();
  var variationId = form.find('input[name=variation_id]').val();
  var qty = form.find('input[name=qty]').val();

  // Data array
  var data = {
    action: 'woomerce_ajax_add_to_cart',
    product_id: productId,
    variation_id: variationId,
    quantity: qty,
  };

  // Ajax function
  $.ajax({
    type: 'post',
    url: wc_add_to_cart_params.ajax_url,
    data: data,
  });

Thanks

I'm trying to add products to the cart in woomerce, but am getting a persistent error Uncaught ReferenceError: wc_add_to_cart_params is not defined.

Have used this approach many times before without issue, has wc_add_to_cart_params been removed from woomerce? If so what is the replacement?

My code is as follows:

  var productId = form.find('input[name=product_id]').val();
  var variationId = form.find('input[name=variation_id]').val();
  var qty = form.find('input[name=qty]').val();

  // Data array
  var data = {
    action: 'woomerce_ajax_add_to_cart',
    product_id: productId,
    variation_id: variationId,
    quantity: qty,
  };

  // Ajax function
  $.ajax({
    type: 'post',
    url: wc_add_to_cart_params.ajax_url,
    data: data,
  });

Thanks

Share edited Oct 3, 2020 at 2:13 LoicTheAztec 255k24 gold badges397 silver badges443 bronze badges asked Oct 1, 2020 at 22:57 dungey_140dungey_140 2,8029 gold badges41 silver badges75 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

In case this es up for anyone else, it appears the add-to-cart.min.js only loads if 'Enable AJAX add to cart buttons on archives' is enabled in the WC settings. This resolves the issue for me.

Your syntax is correct but the point is from where it is called. If it is a external js file then enqueue it in footer.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742291889a4416348.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信