1
我想設置一個最低金額的購物車,讓客戶結帳。但我希望在一個產品類別中作出例外,即使它不符合最低要求200,我會允許客戶繼續。最小車量,除了和專供產品類別
我的代碼工作除了幾乎罰款:在通知(錯誤)顯示
- 車計算量。
- 這個特殊類別的獨有條件
501
(如果其他類別的產品項目也在購物車中,除了此特殊類別以外不會按預期工作)。
這裏是我的代碼:
add_action('woocommerce_check_cart_items', 'oxynergy_set_min_total');
function oxynergy_set_min_total() {
// Only run in the Cart or Checkout pages
if(is_cart() || is_checkout()) {
global $woocommerce, $product;
$i=0;
// Minimum order checking
$minimumCheck = false;
// Set minimum cart total
$minimum_cart_total = 200;
//loop through all cart products
foreach ($woocommerce->cart->cart_contents as $product) {
// Total we are going to be using for the Math
// This is before taxes and shipping charges
$total = WC()->cart->total;
// See if any product is from the STOCK category or not
if (has_term('481', 'product_cat', $product['product_id']) || has_term('482', 'product_cat', $product['product_id']) || has_term('495', 'product_cat', $product['product_id'])) {
$minimumCheck = true;
//Get price of that product
$regular_price = get_post_meta($product['product_id'], '_regular_price', true); //change to _sale_price if it is in sale
//echo $regular_price."<br>";
$total = $regular_price * $product['quantity'];
//echo $total."<br>";
$subtotal_cat += $total; //get total of
//echo $subtotal_cat;
//$category_price += ($product['line_subtotal'] + $product['line_subtotal_tax']);
}
if (has_term('501', 'product_cat', $product['product_id'])) {
//Get price of that product
$regular_price = get_post_meta($product['product_id'], '_regular_price', true); //change to _sale_price if it is in sale
//echo $regular_price."<br>";
$total = $regular_price * $product['quantity'];
//echo $total."<br>";
$subtotal_cat += $total; //get total of
//echo $subtotal_cat;
//$category_price += ($product['line_subtotal'] + $product['line_subtotal_tax']);
}
}
if ($minimumCheck && $subtotal_cat <= $minimum_cart_total) {
// Compare values and add an error is Cart's total
// happens to be less than the minimum required before checking out.
// Will display a message along the lines of
// A Minimum of 200 USD is required before checking out. (Cont. below)
// Current cart total: 6 USD
wc_add_notice(sprintf('<strong>A Minimum of %s %s excl. TAX is required category before checking out.</strong>'
.'<br />Current cart\'s total: %s %s excl. TAX',
$minimum_cart_total,
get_option('woocommerce_currency'),
$subtotal_cat,
get_option('woocommerce_currency')),
'error');
}
}
}
我做錯了嗎?
我該如何讓這段代碼能夠專門爲該特殊產品類別設置例外?
謝謝。
嘿!在1小時內與Skype進行聊天可以嗎?現在我不在辦公室 –
非常感謝你的朋友! –