【问题标题】:Hide error message if cart is empty in WooCommerce如果 WooCommerce 中的购物车为空,则隐藏错误消息
【发布时间】:2017-08-17 21:27:34
【问题描述】:

在 WooCommerce 中,我正在使用一个功能来显示错误消息并阻止结帐,如果来自一个特定类别的产品单独在购物车项目中。

但错误消息即使购物车是空的也会显示

如果购物车为空,如何禁用此功能?

这是我的代码:

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;
}

谢谢

【问题讨论】:

    标签: php wordpress woocommerce cart categories


    【解决方案1】:

    为避免在购物车为空时显示错误消息,您需要以这种方式添加第一个函数条件 ! WC()-&gt;cart-&gt;is_empty()

    // 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' );
    

    【讨论】:

    • 酷,它有效,谢谢!我试过WC()-&gt;cart-&gt;is_empty(),但方法不对。我可以知道为什么// check if this category is the only thing in the cart if ( sv_wc_is_category_alone_in_cart( $category ) ) { 不够吗?
    猜你喜欢
    • 2022-06-10
    • 2021-06-14
    • 2020-06-06
    • 2018-10-16
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    相关资源
    最近更新 更多