Checkout with a single product: verify if ANY product is in the cart, and give error

后端 未结 1 1019

I can\'t imagine how to verify if the cart has some products inside, or not. I just need to allow ONE product for the checkout.

Here is the code used in class-wc-cart.p

相关标签:
1条回答
  • 2021-01-22 20:54

    Don't make changes directly in woocommerce core file because when you update plugin, might your code lost.

    Add following code into functions.php and it will add only one product into cart:

    add_filter( 'woocommerce_add_to_cart_validation', 'woocommerce_add_cart_item_data_custom' );
    
    function woocommerce_add_cart_item_data_custom( $cart_item_data ) {
    
        global $woocommerce;
        if($woocommerce->cart->cart_contents_count > 0){
             wc_add_notice(
                         __( 'You cannot add another product to your cart.', 'woocommerce' ));
    
                    return false;
        }
    }
    
    0 讨论(0)
提交回复
热议问题