Making Specific Products Unpurchasable Or Purchasable For A Specific Date In WooCommerce | Code For Developers

Making Specific Products Unpurchasable or purchasable for a specific date in WooCommerce

How to make specific products not purchasable in WooCommerce using code.

In this blog post, we will limit product purchasability based on various conditions.
To make a specific product not purchasable in WooCommerce, we can use a filter hook in your theme’s functions.php file or in a custom plugin. Here’s an example of how you can achieve this for a product with the ID 741:

functions.php
function disable_product_purchase($purchasable, $product) {
    // Check if the product ID is 741
    if ($product->get_id() == 741) {
        // Make the product not purchasable
        $purchasable = false;
    }
    return $purchasable;
}

add_filter('woocommerce_is_purchasable', 'disable_product_purchase', 10, 2);

If we need conditions based on the product category all products under that category will not be purchasable.

functions.php
function disable_products_in_category_purchase($purchasable, $product) {
    // Get the product categories IDs
    $product_categories = wp_get_post_terms($product->get_id(), 'product_cat', array('fields' => 'ids'));

    // Check if the product belongs to category with ID 57
    if (in_array(57, $product_categories)) {
        // Make the product not purchasable
        $purchasable = false;
    }
    return $purchasable;
}

add_filter('woocommerce_is_purchasable', 'disable_products_in_category_purchase', 10, 2);

We can also use a specific date when the product is purchasable or not. In this example we had some products for sale only on 11 Nov for 11-11 promotional sales.

function disable_products_in_category_purchase($purchasable, $product) {
    // Get the current date in 'm-d' format using current_time function
    $current_date = date('m-d', current_time('timestamp', 0)); // 0 for the local timezone

    // Get the product categories IDs
    $product_categories = wp_get_post_terms($product->get_id(), 'product_cat', array('fields' => 'ids'));

    if (in_array(57, $product_categories) && $current_date != '11-11') {
        // Make the product not purchasable
        $purchasable = false;
    }
    return $purchasable;
}

add_filter('woocommerce_is_purchasable', 'disable_products_in_category_purchase', 10, 2);
Previous Code

WooCommerce Custom Order Dropdown Based on Payment Method

WooCommerce admin order page custom drop-down base ...

Next Code

Enabling Guest Checkout in WooCommerce: Bypassing Email Verification for Order Payment

Enable guest payment, bypass email verification. E ...

Leave a Reply

Your email address will not be published. Required fields are marked *

If you find it useful

Buy me a coffee

ACF

Elementor

JavaScript

jQuery

Others

PHP

WooCommerce

WordPress

WP Plugin Dev

Implementing WooCommerce Shop Page Quantity Input and Plus (+) Minus (-) Button with simple Code

Enhance the user experience on your WooCommerce we ...

Customizing WooCommerce Order Numbers with Prefix and Year Suffix

Customize WooCommerce order numbers your way, whet ...

Get WooCommerce product info

Show product info in the place as you wish to. It ...

Update WooCommerce Delivery cost with condition

Change Delivery price base on what item in cart ...

WooCommerce Ajax Product Search and Category Filter Without Plugin

WooCommerce Ajax Product Search with Category Filt ...

top