WooCommerce产品捆绑ajax添加到购物车

时间:2019-06-06 10:42:30

标签: javascript php ajax wordpress woocommerce

我的代码没有添加到带有AJAX产品包的购物车中,但它适用于简单易变的产品。

如果我禁用了AJAX调用功能,则可以使用,但是会刷新页面。

$(document).on('click', '.single_add_to_cart_button', function (e) {
    e.preventDefault();
    var $thisbutton = $(this),
        $form = $thisbutton.closest('form.cart'),
        id = $thisbutton.val(),
        product_qty = $form.find('input[name=quantity]').val() || 1,
        product_id = $form.find('input[name=add-to-cart]').val() || id,
        variation_id = $form.find('input[name=variation_id]').val() || 0;

    var data = {
      action: 'woocommerce_ajax_add_to_cart',
      product_id: product_id,
      product_sku: '',
      quantity: product_qty,
      variation_id: variation_id,
    };

    $(document.body).trigger('adding_to_cart', [$thisbutton, data]);

    $.ajax({
      type: 'post',
      url: wc_add_to_cart_params.ajax_url,
      data: data,
      beforeSend: function (response) {
        $thisbutton.removeClass('added').addClass('loading');
      },
      complete: function (response) {
        $thisbutton.addClass('added').removeClass('loading');
      },
      success: function (response) {
        if (response.error & response.product_url) {
          window.location = response.product_url;
          return;
        } else {
          $(document.body).trigger('added_to_cart', [response.fragments, response.cart_hash, $thisbutton]);
          // if ($( "#mydiv" ).hasClass('disabled')) {
          //   $('#sidebar-right-hidden').addClass('is-active');
          // }
          $('#sidebar-right-hidden').addClass('is-active');
          $('#addToCartModal').modal('hide');
        }
      },
    });
    return false;
  });
add_action('wp_ajax_woocommerce_ajax_add_to_cart', 'woocommerce_ajax_add_to_cart');
add_action('wp_ajax_nopriv_woocommerce_ajax_add_to_cart', 'woocommerce_ajax_add_to_cart');

function woocommerce_ajax_add_to_cart() {
    $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_POST['product_id']));
    $quantity = empty($_POST['quantity']) ? 1 : wc_stock_amount($_POST['quantity']);
    $variation_id = absint($_POST['variation_id']);
    $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);
    $product_status = get_post_status($product_id);

    if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity, $variation_id) && 'publish' === $product_status) {
    do_action('woocommerce_ajax_added_to_cart', $product_id);

    if ('yes' === get_option('woocommerce_cart_redirect_after_add')) {
      wc_add_to_cart_message(array($product_id => $quantity), true);
    }

    WC_AJAX :: get_refreshed_fragments();
    } else {
    $data = array(
      'error' => true,
      'product_url' => apply_filters('woocommerce_cart_redirect_after_error', get_permalink($product_id), $product_id)
    );

    echo wp_send_json($data);
    }
    wp_die();
}

我得到错误:"test" cannot be added to your cart. Please choose "Black Belt" options...我猜想这与产品捆绑包不兼容。 我是从控制台中的admin-ajax.php获得的:

{error: true, product_url: "https://5050.local/produs/test/"}
error: true
product_url: "https://5050.local/produs/test/"

代码应将产品捆绑包添加到购物车。

我如何使它工作?

1 个答案:

答案 0 :(得分:0)

无法购买分组产品-它们是其他产品的包装。您必须将所有子产品一一添加到购物车。

检查WooCommerce分组的产品类别,您将了解一些想法

WooCommerce\Classes\Products\WC_Product_Grouped