最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论