php,检查购物车是否包含要求 - 添加优惠券

问题描述:

我试图让php检查购物车在应用免费产品和优惠券之前是否有数组中指定的2个项目。我列出的这个具体例子是检查价格是否超过一定数量。我试过这个,因为我无法让这两个项目工作。php,检查购物车是否包含要求 - 添加优惠券

function bbloomer_add_gift_if_sku_added_cart($passed, $product_id, $quantity) { 
global $woocommerce; 


$skuswithgift = array('SMWB-M23','001-SLW'); 


$giftsku = 'comb'; 

$coupon_code = 'combfree'; 

$product = wc_get_product($product_id); 

if ($product->get_sku() && in_array($product->get_sku(), $skuswithgift) && $woocommerce->cart->total > 26.00) { 
    WC()->cart->add_to_cart(wc_get_product_id_by_sku($giftsku)); 
    wc_add_notice(__('Hey there! As promised, you recieved a free comb with the purchase of two towels and we added it to your cart for you!', 'woocommerce'), 'success'); 
    $woocommerce->cart->add_discount($coupon_code); 
} 

else { 
    WC()->cart->remove_cart_item(wc_get_product_id_by_sku($giftsku)); 
    $woocommerce->cart->remove_coupon($coupon_code); 
} 

return $passed; 
} 

add_filter('woocommerce_add_to_cart_validation', 'bbloomer_add_gift_if_sku_added_cart', 10, 3); 

尝试这样做,它不工作

function bbloomer_add_gift_if_sku_added_cart($passed, $product_id, $quantity) { 
global $woocommerce; 

$skuswithgift = array('SMWB-M23','001-SLW'); 

$giftsku = 'comb'; 

$coupon_code = 'combfree'; 

$product = wc_get_product($product_id); 

$total_towels = 0; 

// Determine how many towels there are 
// Loop through every cart item 
foreach ($woocommerce->cart->get_cart() as $cart_item) { 

    $product = $cart_item['data']; 
    //$product = $cart_item; 

    // Is it in Gift SKUs 
    $is_towel = in_array($product->get_sku(), $skuswithgift); 
    if($is_towel){ 
    // Add this quantity to the total towels 
    $total_towels += intval($cart_item['quantity']); 
    } 

} 


// Apply Discount 
if ($total_towels <= 2) { 
    WC()->cart->add_to_cart(wc_get_product_id_by_sku($giftsku)); 
    wc_add_notice(__('Hey there! As promised, you recieved a free comb with the purchase of two towels and we added it to your cart for you!', 'woocommerce'), 'success'); 
    $woocommerce->cart->add_discount($coupon_code); 
} 

else { 
    WC()->cart->remove_cart_item(wc_get_product_id_by_sku($giftsku)); 
    $woocommerce->cart->remove_coupon($coupon_code); 
} 

return $passed; 
} 

add_filter('woocommerce_add_to_cart_validation', 'bbloomer_add_gift_if_sku_added_cart', 10, 3); 
+0

你真的需要你能解释更多。 –

+0

我们基本上希望顾客购买任何SKU定义产品中的两种,然后他们可以免费获得免费送货和另一种产品。合理? –

+0

因此,基本上,您正在寻找如果两个sku已经在购物车中比免费送货和一个免费的产品必须自动添加到购物车中。我让你写吗?差不多就是 –

试试下面的代码

add_action('woocommerce_add_to_cart_redirect','customeApplyCode'); 
function customeApplyCode() 
{ 
    global $woocommerce;  
    $items = $woocommerce->cart->get_cart(); 
    $productSku = array(); 
    foreach($items as $item => $values) { 
     // Retrieve WC_Product object from the product-id: 
     $product = wc_get_product($values['product_id'] ); 
     // Get SKU from the WC_Product object: 
     $productSku[] = $product->get_sku(); 
    } 
    $skuswithgift = array('SMWB-M23','001-SLW'); 
    $result=array_intersect($skuswithgift,$productSku); 
    if(count($result)>0) 
    { 
     //product id of free product 
     $product_id = 1312; 
     $woocommerce->cart->add_to_cart($product_id); 
     add_filter('woocommerce_product_needs_shipping', 'hide_shipping_when_free_is_available', 10, 2);  
    } 

} 
function hide_shipping_when_free_is_available($rates, $package) { 
    return false; 
}