0

I have a product in my WooCommerce shop where I have limited that it can only be sold once. That is a standard setting in WooCommerce. But customers now simply order the product a few times in succession in ever-new orders.

So I would like that customers can buy the product just one (for always).

I've searched the internet for some time but can't find a suitable solution. I find many plugins that just don't do what I'm looking for or too much.

Hopefully someone can help me with a minimal code by excluding the user id and/or email for purchasing the product. I already use this code from BBloomer for free shipping for the product. Perhaps some code can be added to this?

I'm still learning, thanks in advance!

add_action( 'woocommerce_before_cart', 'bbloomer_apply_matched_coupons' );
  
function bbloomer_apply_matched_coupons() {
  
    $coupon_code = 'GRATISverzendingProefbox'; 
  
    if ( WC()->cart->has_discount( $coupon_code ) ) return;
  
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
  
    // this is your product ID
    $autocoupon = array( 7946 );
  
    if ( in_array( $cart_item['product_id'], $autocoupon ) ) {   
        WC()->cart->apply_coupon( $coupon_code );
        wc_print_notices();
    }
  
    }
  
}

I tried this code which prevent logged-in users to add it to cart again but order as a guest of adding to cart and then loggin in to order the product is still possible. Any thoughts maybe? I really appreciate any help.

function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
    // Retrieve the current user object
    $current_user = wp_get_current_user();
    
    // Checks if a user (by email or ID or both) has bought an item
    if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id ) ) {
        // Display an error message
        wc_add_notice( __( 'Je hebt onze proefbox al eens gekocht. Door de grote vraag is het helaas niet mogelijk om het nog een keer te kopen.', 'woocommerce' ), 'error' );
        
        $passed = false;
    }

    return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );

function action_woocommerce_check_cart_items() {
    // Retrieve the current user object
    $current_user = wp_get_current_user();
    
    // Initialize
    $flag = false;
    
    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ) {
        // Check for variantions
        $product_id = $cart_item['7946'];
        
        // Checks if a user (by email or ID or both) has bought an item
        if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id ) ) {
            // Flag becomes true
            $flag = true;
            
            // Break loop
            break;
        }
    }
    
    // True
    if ( $flag ) {
        // Clear all other notices          
        wc_clear_notices();

        // Avoid checkout display an error notice
        wc_add_notice( __( 'Je hebt onze proefbox al eens gekocht. Door de grote vraag is het helaas niet mogelijk om het nog een keer te kopen.', 'woocommerce' ), 'error' );
        
        // Optional: remove proceed to checkout button
        remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );   
    }
}   
  • I assume that only registered customers can buy something? in other words, guest orders are not possible? If this is the case, see [this answer](https://stackoverflow.com/a/68007576/11987538) – 7uc1f3r Aug 24 '21 at 14:21
  • Sorry for my unclear post. Guest orders are possible and should stay possible. That's why I thought of using user ID and email address. With the email address it should be possible I guess to exclude guest shoppers. – MediaCreandum Aug 24 '21 at 15:01
  • Then you can use the following custom function: https://stackoverflow.com/a/46217461/11987538 in conjunction with the hook I referenced earlier in my previous comment – 7uc1f3r Aug 24 '21 at 15:17
  • I used the code in this post: https://stackoverflow.com/a/68007576/14066761 and it works when your logged-in. In the post the submitter said that to avoid adding to cart as guest and then login there's a extra code, but even with that code I can add it to cart and login and order. Any thoughts maybe? I added the exactly used code in my question above. – MediaCreandum Aug 25 '21 at 07:50

0 Answers0