Only allow to purchase one Item In Woocommerce 3

2020-04-21 04:37发布

问题:

Is there anyway to prevent more than one item for purchase in WooCommerce, or prevent more than one item added to the cart?

I have different products but I want to allow only one item per checkout.

I tried to search for solution but those existing solutions are not working properly, let's say when user is not signed in and adds an item into cart and then goes to checkout and logins there an item that has been added previously when customer was logged in also adds up to the one customer just added, so now there is 2 products inside the cart, and this is an issue here is the code that I'm using that's not working properly.

function woo_custom_add_to_cart( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );

回答1:

Updated (with a 2nd alternative as asked in your comment).

The code below will limit add to cart to a unique item displaying an error message when adding more than one. A second function will check cart items avoiding checkout and adding an error message when there is more than one item:

// Allowing adding only one unique item to cart and displaying an error message
add_filter( 'woocommerce_add_to_cart_validation', 'add_to_cart_validation', 10, 1 );
function add_to_cart_validation( $passed ) {
    if( ! WC()->cart->is_empty() ){
        wc_add_notice( __("You can add only one item to cart", "woocommerce" ), 'error' );
        $passed = false;
    }
    return $passed;
}

// Avoiding checkout when there is more than one item and displaying an error message
add_action( 'woocommerce_check_cart_items', 'check_cart_items' ); // Cart and Checkout
function check_cart_items() {
    if( sizeof( WC()->cart->get_cart() ) > 1 ){
        // Display an error message
        wc_add_notice( __("More than one items in cart is not allowed to checkout", "woocommece"), 'error' );
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.


1) When trying add to cart a 2nd item:

2) If a more than one item is in cart:

3) And in checkout you will get an empty page with the same error notice:


To allow only one cart item removing any additional items that will work in any cases:

// Removing on add to cart if an item is already in cart
add_filter( 'woocommerce_add_cart_item_data', 'remove_before_add_to_cart' );
function remove_before_add_to_cart( $cart_item_data ) {
    WC()->cart->empty_cart();
    return $cart_item_data;
}

// Removing one item on cart item check if there is more than 1 item in cart
add_action( 'template_redirect', 'checking_cart_items' ); // Cart and Checkout
function checking_cart_items() {
    if( sizeof( WC()->cart->get_cart() ) > 1 ){
        $cart_items_keys = array_keys(WC()->cart->get_cart());
        WC()->cart->remove_cart_item($cart_items_keys[0]);
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.