Brad Holmes
Brad Holmes

Reputation: 497

Allow specific products to be only purchased once by user in Woocommerce

The following code for Woocommerce, allows being purchased again once purchased

function sv_disable_repeat_purchase( $purchasable, $product ) {
    // Enter the ID of the product that shouldn't be purchased again
    $non_purchasable = 40021;

    // Get the ID for the current product (passed in)
    $product_id = $product->is_type( 'variation' ) ? $product->variation_id : $product->id;

    // Bail unless the ID is equal to our desired non-purchasable product
    if ( $non_purchasable != $product_id ) {
        return $purchasable;
    }

    // return false if the customer has bought the product
    if ( wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $product_id ) ) {
        $purchasable = false;
    }

    // Double-check for variations: if parent is not purchasable, then variation is not
    if ( $purchasable && $product->is_type( 'variation' ) ) {
        $purchasable = $product->parent->is_purchasable();
    }

    return $purchasable;
}
add_filter( 'woocommerce_variation_is_purchasable', 'sv_disable_repeat_purchase', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'sv_disable_repeat_purchase', 10, 2 );

please can somme one help me change $non_purchasable = 40021 to an array of products

Upvotes: 1

Views: 789

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 253784

Update (removed outdated unneeded $product->variation_id …)

You can simply use the PHP conditional function in_array()… Also, your code is outdated:

  • This is outdated $product->variation_id
  • replace $product->id by $product->get_id()

Here is a simplified revisited code version:

add_filter( 'woocommerce_variation_is_purchasable', 'products_purchasable_once', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'products_purchasable_once', 10, 2 );
function products_purchasable_once( $purchasable, $product ) {
    // Here set the product IDs in the array that can be purchased only once 
    $targeted_products = array(40021, 40038, 40171);

    // Only for logged in users and not for variable products
    if( ! is_user_logged_in() || $product->is_type('variable') )
        return $purchasable; // Exit

    $user = wp_get_current_user(); // The WP_User Object

    if ( in_array( $product->get_id(), $targeted_products ) &&
    wc_customer_bought_product( $user->user_email, $user->ID, $product->get_id() ) ) {
        $purchasable = false;
    }

    return $purchasable;
}

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

Upvotes: 1

Mr. Jo
Mr. Jo

Reputation: 5251

Here is your code with an array. Please check if it does what you want:

function sv_disable_repeat_purchase( $purchasable, $product ) {

    // Enter the ID of the product that shouldn't be purchased again
    $non_purchasable_arr = array(
        40021,
        40022
    );

    // Get the ID for the current product (passed in)
    $product_id = $product->is_type( 'variation' ) ? $product->variation_id : $product->id;

    // Bail unless the ID is equal to our desired non-purchasable product
    foreach ( $non_purchasable_arr as $non_purchasable ) {
        if ( $non_purchasable !== $product_id ) {
            return $purchasable;
        }
    }

    // return false if the customer has bought the product
    if ( wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $product_id ) ) {
        $purchasable = false;
    }

    // Double-check for variations: if parent is not purchasable, then variation is not
    if ( $purchasable && $product->is_type( 'variation' ) ) {
        $purchasable = $product->parent->is_purchasable();
    }

    return $purchasable;
}

add_filter( 'woocommerce_variation_is_purchasable', 'sv_disable_repeat_purchase', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'sv_disable_repeat_purchase', 10, 2 );

Upvotes: 1

Related Questions