Restrict product sale by one per customer for always

431 Views Asked by At

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 );   
    }
}   
0

There are 0 best solutions below