我正在try 在购物车中添加不同的产品.有两种类型的产品,一种是other
,另一种是simple
.
我需要抓取如果简单的产品在购物车里,那么只有简单的可以添加,而其他的在购物车里,那么只有其他.
我不想同时添加简单和其他类型的产品.
add_action('woocommerce_add_to_cart', 'custome_add_to_cart', 10, 6);
function custome_add_to_cart($cart_id, $product_id, $request_quantity, $variation_id, $variation, $cart_item_data) {
global $woocommerce;
//print_r($cart_item_data); //Current
// die();
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_type = $cart_item['type'];
if ($product_type == 'other'){
if($cart_item_data['type'] == 'other'){
WC()->cart->add_to_cart( $product_id );
// print_r($cart_item); //Old Cart
// die();
}else{
wc_print_notice( 'This type of product not added 1', 'notice' );
}
}else{
//print_r($cart_item); //Old Cart
if($cart_item_data['type'] != 'other'){
WC()->cart->add_to_cart( $product_id );
}else{
wc_print_notice( 'This type of product not added 2', 'notice' );
exit;
}
}
}
}
}
我正在使用这个钩子,但它对我不起作用.它显示503
个错误.