2017-03-24 2 views
2

WooCommerceでは、特定のカテゴリの商品がカートアイテム内に単独で存在する場合、エラーメッセージを表示してチェックアウトを防止する機能を使用しています。WooCommerceでカートが空の場合にエラーメッセージを表示しない

ただし、カートが空の場合でもエラーメッセージは表示され続けます

カートが空の場合、どうすればこの機能を無効にできますか?ここで

は私のコードは次のとおりです。カートは空であるとき、あなたはあなたの最初の関数の条件! WC()->cart->is_empty()にこの方法を追加する必要が示すエラーメッセージを回避するには

function sv_wc_prevent_checkout_for_category() { 

    // set the slug of the category for which we disallow checkout 
    $category = 'clothing'; 

    // get the product category 
    $product_cat = get_term_by('slug', $category, 'product_cat'); 

    // sanity check to prevent fatals if the term doesn't exist 
    if (is_wp_error($product_cat)) { 
     return; 
    } 

    $category_name = '<a href="' . get_term_link($category, 'product_cat') . '">' . $product_cat->name . '</a>'; 

    // check if this category is the only thing in the cart 
    if (sv_wc_is_category_alone_in_cart($category)) { 

     // render a notice to explain why checkout is blocked 
     wc_add_notice(sprintf('Hi there! Looks like your cart only contains products from the %1$s category &ndash; you must purchase a product from another category to check out.', $category_name), 'error'); 
    } 
} 
add_action('woocommerce_check_cart_items', 'sv_wc_prevent_checkout_for_category'); 


/** 
* Checks if a cart contains exclusively products in a given category 
* 
* @param string $category the slug of the product category 
* @return bool - true if the cart only contains the given category 
*/ 
function sv_wc_is_category_alone_in_cart($category) { 

    // check each cart item for our category 
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { 

     // if a product is not in our category, bail out since we know the category is not alone 
     if (! has_term($category, 'product_cat', $cart_item['data']->id)) { 
      return false; 
     } 
    } 

    // if we're here, all items in the cart are in our category 
    return true; 
} 

おかげ

答えて

1

// Check if this category is the only thing in the cart (when cart is not empty) 
if (sv_wc_is_category_alone_in_cart($category) && ! WC()->cart->is_empty()) { // <= <= 

最初の機能コードは次のようになります:

function sv_wc_prevent_checkout_for_category() { 

    // set the slug of the category for which we disallow checkout 
    $category = 'clothing'; 

    // get the product category 
    $product_cat = get_term_by('slug', $category, 'product_cat'); 

    // sanity check to prevent fatals if the term doesn't exist 
    if (is_wp_error($product_cat)) { 
     return; 
    } 

    $category_name = '<a href="' . get_term_link($category, 'product_cat') . '">' . $product_cat->name . '</a>'; 

    // Check if this category is the only thing in the cart (when cart is not empty) 
    if (sv_wc_is_category_alone_in_cart($category) && ! WC()->cart->is_empty()) { // <== 

     // render a notice to explain why checkout is blocked 
     wc_add_notice(sprintf('Hi there! Looks like your cart only contains products from the %1$s category &ndash; you must purchase a product from another category to check out.', $category_name), 'error'); 
    } 
} 
add_action('woocommerce_check_cart_items', 'sv_wc_prevent_checkout_for_category'); 
+0

クール、それは、ありがとう!私は 'WC() - > cart-> is_empty()'を試しましたが、間違った方法で試しました。なぜこのカテゴリがカート内の唯一のものであるかを確認することができますか? (sv_wc_is_category_alone_in_cart($ category)){'が十分でない場合は? – Gore

関連する問題