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.

Related Blogs

Adding Sidebar In WordPress Theme

Adding a sidebar to a WordPress theme involves modifying the theme files and registering a new sidebar area using WordPress functions. Below are the general steps to add a sidebar in WordPress themes:

Ultimate Guide to Custom Meta Boxes in WordPress

In WordPress, `add_meta_boxes` is an action hook that allows developers to add custom meta boxes to different post types’ edit screens. Meta boxes are additional sections on the post editor screen where you can add and display custom fields or other types of data related to the post.

Shortcodes included with Dokan

Dokan is a popular multi-vendor marketplace plugin for WooCommerce that allows you to create and manage a marketplace where multiple vendors can sell their products. Dokan provides several shortcodes that you can use to display various elements and functionalities on your marketplace pages. Here are some of the essential shortcodes provided by Dokan:

Add New Tab In My Account Page.

We Are Web developer and Adding a Tab in WooCommerce My Account page with custom content is one of the most common customization requests which we receive From The clients.

Request A Quote

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.