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 "%s" to your cart.', 'woocommerce' ), $product_data->get_title() ) ) );
}
}
答案 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/" ) ) );
而不是那个错误。请注意:这会将用户重定向到购物车页面(产品应该已经到位)。