WooCommerce AJAX添加到购物车以获取可变产品,仍重定向到产品页面

时间:2019-02-22 14:18:07

标签: php wordpress woocommerce

以前,将产品从存档页面添加到购物车后,页面停留在同一页面上(不重定向),但这仅适用于简单的产品。我想做的是也为可变产品启用此功能(无需先进入产品页面即可在存档页面显示产品选项)。

基于此链接:

[WooCommerce 3.0+] Ajax add to cart on variable products archive page

使用此代码:

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {

   if ( ! function_exists( 'woocommerce_template_loop_add_to_cart' ) ) {

        function woocommerce_template_loop_add_to_cart() {
             global $product;

             if ($product->get_type() == "variable" ) {
                 woocommerce_variable_add_to_cart();
             }
             else {
                 wc_get_template( 'loop/add-to-cart.php' );
             }
         }
    }

    function ajax_add_to_cart_script() {

      wp_enqueue_script( 'add-to-cart-variation', plugins_url() . '/woocommerce-ajax-add-to-cart-variable-products/js/add-to-cart-variation.js', array('jquery'), '', true );
      wp_localize_script( 'add-to-cart-variation', 'AddToCartAjax', array(
        'ajaxurl' => admin_url( 'admin-ajax.php' ),

      ));
    }
    add_action( 'wp_enqueue_scripts', 'ajax_add_to_cart_script' );

    add_action( 'wp_ajax_woocommerce_add_to_cart_variable_rc', 'woocommerce_add_to_cart_variable_rc_callback' );

    function woocommerce_add_to_cart_variable_rc_callback() {
        $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) );
        $quantity = empty( $_POST['quantity'] ) ? 1 : apply_filters( 'woocommerce_stock_amount', $_POST['quantity'] );
        $variation_id = $_POST['variation_id'];
        $variation  = $_POST['variation'];
        $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );

        if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation  ) ) {
            do_action( 'woocommerce_ajax_added_to_cart', $product_id );
            if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
            wc_add_to_cart_message( $product_id );
        }

        $this->get_refreshed_fragments();
        } else {
            $this->json_headers();

        $data = array(
            'error' => true,
            'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id )
            );
        echo json_encode( $data );
        }
        die();
    }  
}

我已经中途了,但是现在最大的问题是在将可变产品和/或简单产品添加到购物车后,我被重定向到产品页面(将产品添加到购物车就好了)

我不确定这是否是问题,但是我发现在添加上述代码之前和之后HTML的区别是:

之前:

<a href="/product-category/meats/?instock_products=in&#038;v=b718adec73e0&#038;add-to-cart=861" data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_id="861" data-product_sku="" aria-label="Add &ldquo;Whole Pork Fillet (400G Up) Chilled&rdquo; to your cart" rel="nofollow">

之后:

<a href="/product-category/meats/?instock_products=in&#038;v=b718adec73e0&#038;add-to-cart=861" data-quantity="1" class="button" >

不确定这是否是主要原因。任何帮助表示赞赏。

谢谢。

1 个答案:

答案 0 :(得分:-1)

如果单击“添加成功后重定向到购物车页面”复选框是否有帮助?

It is found in Woocommerce > Settings > Products

可以在Woocommerce>设置>产品中找到