0
我想讓客戶只添加來自每個類別的一個產品例如:牛仔褲,t恤等我有這個代碼,但我得到一個空白頁。此代碼檢查categry slug和產品數量。只允許從每個類別添加一個產品到購物車woocommerce
add_action('woocommerce_check_cart_items', 'check_total');
function check_total() {
// Only run in the Cart or Checkout pages
if(is_cart() || is_checkout()) {
global $woocommerce, $product;
$total_quantity = 0;
$display_notice = 1;
$i = 0;
//loop through all cart products
foreach ($woocommerce->cart->cart_contents as $product) {
// See if any product is from the cuvees category or not
if (has_term('category-1', 'product_cat', $product['product_id'])) {
$total_quantity += $product['quantity'];
}
}
// Set up the acceptable totals and loop through them so we don't have an ugly if statement below.
$acceptable_totals = array(1, 2, 3, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 72, 96, 120);
foreach($acceptable_totals as $total_check) {
if ($total_check == $total_quantity) { $display_notice = 0; }
}
foreach ($woocommerce->cart->cart_contents as $product) {
if (has_term('category-1', 'product_cat', $product['product_id'])) {
if($display_notice == 1 && $i == 0) {
// Display our error message
wc_add_notice(sprintf('This product can only be sold in following quantities 1 | 2 | 3 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60 | 72 | 96 | 120.</p>', $total_quantity),
'error');
}
$i++;
}
}
}
在你剛纔{'由'如果(has_term更換'如果($ product_cats, 'product_cat',$ cart_item [ '的product_id'])!has_term()這個相關答案( $ product_cats,'product_cat',$ cart_item ['product_id'])){'...它會做你期待的。 – LoicTheAztec
但那已經完成 – SandeepTete
我已經做了最後更新,只是爲你的情況... – LoicTheAztec