2
我必須檢查我的兩個強制性產品類別之一是否在購物車中。檢查多個強制性產品類別中的一個是否在購物車中
我從這個答案定製代碼:
Allow checkout only when a product of a mandatory category is in cart
但隨着我的自定義代碼,它總是會檢查只是其中之一,我得到了一些錯誤。我的代碼不起作用。
我如何使它適用於2個產品類別而不是一個?
下面是代碼:
// Function that define the mandatory product category
function your_mandatory_category_slug(){
// DEFINE HERE the SLUG of the needed product category
$category = 'cxsuite-download-option';
return $category;
}
function your_mandatory_category_slug_h(){
// DEFINE HERE the SLUG of the needed product category
$category_h = 'cxsuite-hosted-option';
return $category_h;
}
// Conditional function that returns true if the mandatory product category is in cart
function has_mandatory_category(){
$category_needed = your_mandatory_category_slug();
$category_needed_h = your_mandatory_category_slug_h();
$has_cat = false;
// Iterrating each item in cart and detecting…
foreach (WC()->cart->get_cart() as $item) {
// Detects if the needed product category is in cart items
if (has_term($category_needed, 'product_cat', $item['product_id'])) {
$has_cat = true;
break;
}
elseif (has_term($category_needed_h, 'product_cat', $item['product_id'])) {
$has_cat = true;
break;
}
}
return $has_cat;
}
// Function that display a message if there is not in cart a mandatory product category
function mandatory_category_display_message() {
$category_needed = your_mandatory_category_slug();
$category_needed_h = your_mandatory_category_slug_h();
// check that cart is not empty (for cart and product category archives)
if(!WC()->cart->is_empty() && (is_cart() || is_product_category($category_needed) || is_product_category($category_needed_h))){
$category_needed_single=null;
if($category_needed=='cxsuite-download-option'){
$category_needed_single='cxsuite-download-option';
}{
$category_needed_single=$category_needed_h;
}
$category_obj = get_term_by('slug', $category_needed_single, 'product_cat');
if (is_wp_error($category_obj)) return;
// Display message when product category is not in cart items
if (!has_mandatory_category()) {
$category_name = $category_obj->name;
$category_url = get_term_link($category_needed_single, 'product_cat');
// render a notice to explain why checkout is blocked
wc_add_notice(sprintf(__('<strong>Reminder:</strong> You have chosen Addon that can only be purchased together with a %1$s. Please add a %1$s (full or trial) before checking out. Please return <a href="%2$s"> here to "%1$s" product page</a>', 'your_theme_domain'), $category_name, $category_url), 'error');
}
}
}
add_action('woocommerce_before_main_content', 'mandatory_category_display_message', 30); // for product mandatory category archives pages
add_action('woocommerce_check_cart_items', 'mandatory_category_display_message'); // for cat page
// Function that redirect from checkout to mandatory product category archives pages
function mandatory_category_checkout_redirect() {
// If cart is not empty on checkout page
if(!WC()->cart->is_empty() && is_checkout()){
$category_needed = your_mandatory_category_slug();
$category_needed_h = your_mandatory_category_slug_h();
$category_needed_single=null;
if(is_product_category($category_needed)){
$category_needed_single=$category_needed;
}else{
$category_needed_single=$category_needed_h;
}
// If missing product category => redirect to the products category page
if (!has_mandatory_category())
wp_redirect(get_term_link($category_needed_single, 'product_cat'));
}
}
add_action('template_redirect', 'mandatory_category_checkout_redirect');
感謝喜又做很好的工作,但在最後的函數,我改變重定向回如果他不能有一個或另一個選擇,我會認爲更好的解決方案是去商店頁面。再次感謝。 –