• Resolved jasonthegreek

    (@jasonthegreek)


    My goal is to limit a cart/checkout to one item per order.
    I am aware of this thread: https://www.ads-software.com/support/topic/one-product-and-one-piece-in-cart/

    That code does successfully limit the cart to one item, HOWEVER, it simply replaces the existing item in the cart with the newly selected item. There is no notification or error, that indicates you can only choose one item per order.

    Is there an additional code snippet to provide that error? I’d prefer not to have a static banner saying one item per order.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter jasonthegreek

    (@jasonthegreek)

    Also important to note – the code from the other thread conflicts with the FooEvents Plugin. It blocks the FooEvent plugin from capturing guest information as part of the checkout process.
    Therefore I’m still in search of a standard way to limit one item per order.

    The recommended code from the previous thread:
    // this code here sets the cart to a max of one product
    function woo_cart_ensure_only_one_item( $cart_contents ) {
    return array( end( $cart_contents ) );
    }
    add_filter( ‘woocommerce_cart_contents_changed’, ‘woo_cart_ensure_only_one_item’ );

    Hi @jasonthegreek

    Thanks for reaching out!

    Firstly, I apologize for the delayed response.

    I understand that you want to limit one (1) item per order upon checkout on your site, is that correct?

    This is not one of the core functionalities of WooCommerce, hence, this would require customization or installation of third-party plugins on your site to achieve this.

    I did some research and found these articles could be a good starting point:

    How to Set Minimum and Maximum Order Quantity in WooCommerce
    How to Limit the Cart Quantity to 1 in WooCommerce?
    Minimum and Maximum Quantity for WooCommerce

    Hope this helps!

    Thread Starter jasonthegreek

    (@jasonthegreek)

    Thank you @xue28 I appreciate the follow-up. The second link you shared with the plugin might do the trick.

    Last night I found and tested the code below which seems (so far) to be working.

    // 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( __(“Orders are limited to one item per checkout”, "woocommerce" ), 'error' );
            $passed = false;
        }
        return $passed;
    }
    Thread Starter jasonthegreek

    (@jasonthegreek)

    Thank you @xue28 – I suspect the plugin from the 2nd link would work. In the meantime, last night I found and successfully tested the code below. So far it seems to be working. It limits the items in the cart to 1, while also allowing me to chain (free) sub-products to the single item which creates multiple products at check-out.

    // 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( __(“Orders are limited to one item at a time”, "woocommerce" ), 'error' );
            $passed = false;
        }
        return $passed;
    }
    Saif

    (@babylon1999)

    Hello @jasonthegreek,

    Glad to hear you’ve figured it out and thank you for sharing the snippet, it will help whoever lands on the thread in the future. ??

    I will go ahead and mark the thread as solved. If you have a few minutes, we’d love it if you could leave us a review: https://www.ads-software.com/support/plugin/woocommerce/reviews/

    Cheers!

    Hi guys,

    i am also interested for this code but i am not a coder to what file to add this please.

    now the reason this code could be good, is for example if you rent properties and some times woo keeps cookies and same property come as two orders and i have had customers that have paid deposit for two orders and then after i cant remove an item from the order it mess up the rest of the payment. So this could be very good.
    regards B

    Hi @jasonthegreek

    Thanks for reaching out!

    I understand that you have a somewhat similar problem.

    However, per forum best practices shown here, it is advised that you create a new thread so that we can address your issue(s) separately.

    You can create a new thread here: https://www.ads-software.com/support/plugin/woocommerce/#new-topic-0

    Thanks for understanding!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Limit 1 item per cart checkout’ is closed to new replies.