Jordy
Jordy

Reputation: 11

Allow purchases for specific WooCommerce product only on a a specific day

How can I make Sell specific Woocommerce items on a specific day and others on a time range code answer to work with a product with an ID of 625 and change the day to monday?

I tried on my website, but the code applied to all products. Can somebody help to figure out how to adapt to specific ID and day?

Upvotes: 1

Views: 134

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

To enable purchases for specific product(s) only on Mondays use:

// Enable purchases for specific items on monday only
add_filter( 'woocommerce_variation_is_purchasable', 'restrict_specific_products_to_specific_days', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'restrict_specific_products_to_specific_days', 10, 2 );
function restrict_specific_products_to_specific_days( $purchasable, $product ) {
    // Set Your shop time zone (http://php.net/manual/en/timezones.php)
    date_default_timezone_set('Europe/Paris');

    $restricted_product_ids = array( 625 ); // Set in this array, your product Ids to be restricted

    // Target specific product ID(s)
    if( in_array( $product->get_id(), $restricted_product_ids ) ) {
        // Enable only on mondays
        $purchasable = date('w') == 1 ? true : false;
    }
    return $purchasable;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and work.

Upvotes: 1

Related Questions