0

I'm building a ticketing website for events. I'm using WooCommerce and The Events Calendar plugin.

I've already made it so that users must be logged in to buy a ticket (product).

Then I found this code snippet that would limit it so that only 1 product can be purchased per order

add_filter( 'woocommerce_add_to_cart_validation', 'wc_limit_one_per_order', 10, 2 );
function wc_limit_one_per_order( $passed_validation, $product_id ) {
    if ( 31 !== $product_id ) {
        return $passed_validation;
    }

    if ( WC()->cart->get_cart_contents_count() >= 1 ) {
        wc_add_notice( __( 'This product cannot be purchased with other products. Please, empty your cart first and then add it again.', 'woocommerce' ), 'error' );
        return false;
    }

    return $passed_validation;
}

This would work, but in the first if statement you can see that the product ID has to be specified. I know I can also change this to an array like if ( ! in_array( $product_id, array( 31, 32, 33 ) ) ) { but the problem is that I would need to keep updating the IDs for every new event. Is there a way to do this so it applies to all products all the time? If not with code then maybe with settings or a plugin?

I also need to prevent customers (users) from returning to the site later and buying another ticket. So, I need to limit specific products so that only 1 of that SKU can be purchased per user account forever, meaning they can't just return to the site and start the buying process again. Is there a way to do this?

Many thanks in advance.

Æthelstan
  • 883
  • 1
  • 5
  • 12
  • 1
    "Is there a way to do this so it applies to all products all the time?". **If it applies to all products, then you can just omit the if condition that will check on the product id.** When it comes to certain products, a custom checkbox field in the product settings that you can switch on/off seems a better option. **Your current code can also be easily 'bypassed'**, as it is assumed that this product will only be added after other products have already been added, but not vice versa... – 7uc1f3r Sep 22 '21 at 07:37
  • In short: your question contains **an extensive answer** because there are several things that you have to take into account. Otherwise, it can easily be 'bypassed' anyway. Think carefully about what you do and don't want and adjust your question accordingly. However, make sure that it does not contain multiple questions in one because **as I see your question now, it lacks the necessary focus and I see it more as a code request. Especially since you didn't write the code you already use yourself** – 7uc1f3r Sep 22 '21 at 07:38

1 Answers1

1

Is there a way to do this so it applies to all products all the time?

Sure. Just force the Cart to empty before adding a new item:

add_filter( 'woocommerce_add_to_cart_validation', 'bbloomer_only_one_in_cart', 9999, 2 );
   
function bbloomer_only_one_in_cart( $passed, $added_product_id ) {
   wc_empty_cart();
   return $passed;
}

Source and screenshot: https://www.businessbloomer.com/woocommerce-allow-1-product-cart/

I need to limit specific products so that only 1 of that SKU can be purchased per user account forever, meaning they can't just return to the site and start the buying process again

As @7uc1f3r said, please share the code you tried with and then we'll take a look

businessbloomer
  • 1,115
  • 7
  • 11