简体   繁体   中英

Do not redirect to checkout if there is an "error" notice in the Woocommerce cart

I created a feature that adds an error message to the cart to prevent you from proceeding to checkout:

add_action( 'woocommerce_before_cart', 'wc_check_cart_qty' );
function wc_check_cart_qty() {

    // only for custom user role 'acquisto_a_bancali'
    $current_user = wp_get_current_user();
    if ( ! in_array( 'acquisto_a_bancali', $current_user->roles ) ) {
        return;
    }

    foreach ( WC()->cart->get_cart() as $cart_item ) {
        if ( has_product_category( $cart_item['product_id'], $category_ids = array( 1104 ) ) ) {
            $qty += $cart_item['quantity'];
        }
    }

    if ( $qty < 5 ) {
        wc_add_notice( __("You must buy at least 5 products", "woocommerce" ), 'error' );
    }

}

Before I entered the function Live update product category cart item count without reloading in Woocommerce :

function has_product_category( $product_id, $category_ids, $taxonomy = 'product_cat' ) {
    $term_ids = array(); // Initializing

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
        if( $term->parent > 0 ){
            $term_ids[] = $term->parent; // Set the parent product category
            $term_ids[] = $term->term_id;
        } else {
            $term_ids[] = $term->term_id;
        }
    }
    return array_intersect( $category_ids, array_unique($term_ids) );
}

If the error is displayed in the cart and I go to the Checkout, I should only see the message: " ", instead I see the form to complete the order.”,相反,我看到了完成订单的表格。

Why?

I just UNDO my last activity Error appear when I add new coupon I don't really know what's the connection why there is an error when I add new coupon.

I hope this can help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM