如何跳过“你不能添加另一个你的购物车”woocommerce错误,并直接跳转到Checkout?

时间:2017-01-08 07:36:13

标签: wordpress woocommerce e-commerce storefront

Woocommerce的作用是......

当产品单独销售时,当产品已经存在于购物车中并且客户点击添加到购物车时,Woocommerce显示错误消息“您无法将另一个添加到购物车中......查看购物车”

而不是我想要的上述流程..

当客户点击添加到购物车并且产品已经存在于购物车中时,woocommerce应立即重定向到Checkout页面。

我认为这可以通过在Woocommerce Plugin的class-wc-cart.php中编辑几行代码来实现。

如下:

// Force quantity to 1 if sold individually and check for existing item in cart
                if ( $product_data->is_sold_individually() ) {
                    $quantity         = apply_filters( 'woocommerce_add_to_cart_sold_individually_quantity', 1, $quantity, $product_id, $variation_id, $cart_item_data );
                    $in_cart_quantity = $cart_item_key ? $this->cart_contents[ $cart_item_key ]['quantity'] : 0;

                    if ( $in_cart_quantity > 0 ) {
                        throw new Exception( sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', wc_get_cart_url(), __( 'View Cart', 'woocommerce' ), sprintf( __( 'You cannot add another &quot;%s&quot; to your cart.', 'woocommerce' ), $product_data->get_title() ) ) );
                    }
                }

2 个答案:

答案 0 :(得分:0)

尝试一下,它将起作用:

add_filter('woocommerce_add_to_cart_sold_individually_quantity', 'vipcomment_change_quantity_to_zero', 10, 5);


  function vipcomment_change_quantity_to_zero($one, $quantity, $product_id, $variation_id, $cart_item_data ){
    $your_product_id = 96;
    if($product_id == $your_product_id){
      $product_cart_id = WC()->cart->generate_cart_id( $product_id );
      $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
      if($in_cart){
        return 0;
      }else{
        return $quantity;
      }
    }else{
      return $quantity;
    }

  }

  add_filter('woocommerce_add_to_cart_sold_individually_found_in_cart', 'vipcomment_is_product_exist_in_cart', 10, 5);
  function vipcomment_is_product_exist_in_cart($exist, $product_id, $variation_id, $cart_item_data, $cart_id){

    $your_product_id = 96;
    if($product_id == $your_product_id){
      return false;
    }else{
      return $exist;
    }

  }

答案 1 :(得分:-1)

最简单的方法是:

throw new Exception( sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', 
  wc_get_cart_url(), __( 'View cart', 'woocommerce' ), 
  header( "Location: https://www.example.com/cart/" ) ) );

而不是那个错误。请注意:这会将用户重定向到购物车页面(产品应该已经到位)。