Pawan Kumawat

Disable Repeat Purchase Of Products

To disable repeat purchase of products using the `woocommerce_is_purchasable` and `woocommerce_variation_is_purchasable` hooks in WooCommerce, you can implement a custom solution that checks if the product or its variation is already in the customer’s order history. If so, the product will not be purchasable again. Here’s how you can achieve this:

Step 1: Check if Product is Already Purchased
Add the following code to your theme’s `functions.php` file or a custom plugin. This code checks if the product or variation is already purchased by the customer. If it is, the product will be marked as not purchasable.

 

function disable_repeat_purchase( $is_purchasable, $product ) {
    if ( $product instanceof WC_Product_Variation ) {
        $product_id = $product->get_parent_id();
    } else {
        $product_id = $product->get_id();
    }

    if ( wc_customer_bought_product( get_current_user_id(), get_current_user_email(), $product_id ) ) {
        $is_purchasable = false;
    }

    return $is_purchasable;
}

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

 

Step 2: Test the Functionality
With the above code in place, products that the customer has already purchased will be marked as not purchasable. Customers will not be able to add the same product or variation to the cart again.

Please note that this code checks if the current customer (logged-in user) has purchased the product. If the customer is not logged in or does not have an order history, they will be able to purchase the product as usual.

Keep in mind that this approach might not be suitable for all scenarios, especially if you have guest users or customers who might want to repurchase products. Before implementing this functionality, consider how it aligns with your store’s specific requirements.

As always, thoroughly test the functionality to ensure it works as expected and does not interfere with other aspects of your WooCommerce store.

Categories

Related Blogs

How to hide particular category product on shop

To hide a particular category on the shop page using the `woocommerce_product_query` hook, you can modify the query parameters to exclude the category you want to hide. This approach allows you to customize the product query directly without modifying the main query.

How To Add Menu In WordPress Admin Panel

How to add menu in WordPress admin panel – In this post we see how we can add custom menu to admin sidebar. Sometimes when we on WordPress and we need to show some features or any information in admin page then we can use this code snippet to create the same.

How to log information, warnings, errors In wordpress

To create a custom write_log() function for WordPress, you can use the error_log() function to write messages to the error log. This function will allow you to log information, warnings, errors, or any other messages during development or debugging.