0
我試圖讓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);
你真的需要你能解釋更多。 –
我們基本上希望顧客購買任何SKU定義產品中的兩種,然後他們可以免費獲得免費送貨和另一種產品。合理? –
因此,基本上,您正在尋找如果兩個sku已經在購物車中比免費送貨和一個免費的產品必須自動添加到購物車中。我讓你寫嗎?差不多就是 –