2

With Woocommerce I would like to sell certain defined items on Sunday only. If anyone tries to purchase outside of Sunday, it should give an error. The remaining items in the shop can be purchase anytime.

Is this possible? Any track is really appreciated.

Example - I have a product called “Sunday Tapas” which is product code “2795”. I want this product available on Sunday’s only. Everything else in the store should be available everyday as normal.

2 Answers 2

4

Based on "Enable Woocommerce shop purchases only during a daily time range" answer code, changed to handle "days of the week" (So "Sunday" for you).

The following will enable purchases for a specific item (the product Id 2795) on sundays only.

If the item is in cart but not purchasable anymore, It's auto removed (on cart or checkout) displaying an error message.

Outside sundays, an error message is displayed on the product page, indicating that the product is only purchasable on sundays.

You will have to set your time zone in the first function and your specific products in the second function.

The code:

// Utility conditional function that check if day is sunday (returns boolean)
function is_sunday() {
    // Set Your shop time zone (http://php.net/manual/en/timezones.php)
    date_default_timezone_set('Europe/London');

    // If the current day is "sunday" return true (else retun false)
    return ( date('w') == 0 ) ? true : false;
}

// Utility function (setting your product IDS in the array)
function sunday_products() {
    // HERE your product IDs in the array (need to be coma separated)
    return array( 37 );
}

// Enable purchases for specific items on sundays only
add_filter( 'woocommerce_variation_is_purchasable', 'enable_specific_products_on_sundays', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'enable_specific_products_on_sundays', 10, 2 );
function enable_specific_products_on_sundays( $purchasable, $product ) {
    // Enable purchases for specific defined item only on sunday
    if( ! is_sunday() && in_array( $product->get_id(), sunday_products() ) )
        $purchasable = false;

    return $purchasable;
}

// Add a notice in specific products outside sundays
add_action( 'woocommerce_before_single_product', 'filter_before_single_product' );
function filter_before_single_product() {
    global $product;

    if( ! is_sunday() && in_array( $product->get_id(), sunday_products() ) ) {
        wc_print_notice( __( 'This product is only purchasable on sundays', 'woocommerce' ), 'error' );
    }
}

// IN CASE OF (but not needed): Cart and checkout validation + error message
add_action( 'woocommerce_check_cart_items', 'conditionally_allowing_checkout' );
add_action( 'woocommerce_checkout_process', 'conditionally_allowing_checkout' );
function conditionally_allowing_checkout() {
    if ( ! is_sunday() ) {
        // Loop through cart items
        foreach( WC()->cart->get_cart() as $cart_item ){
            // Check cart items
            if( in_array( $cart_item['data']->get_id(), sunday_products() ) ){
                wc_add_notice( sprintf(__("%s can be only purchase on sundays.", "woocommerce"), $cart_item['data']->get_name() ), 'error' );
                break;
            }
        }
    }
}

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

enter image description here

6
  • Thank you. I have modified question again to be more clear. It’s onky certain items I would like to restrict by the day of the week. The rest of the shop items can be purchased on any day.
    – Zed0121
    Feb 25, 2019 at 15:17
  • I have a product called “Sunday tapas” which has product ID “2795”. I want this product to be available on Sundays only. But everything else in the store should be available everyday as normal.
    – Zed0121
    Feb 25, 2019 at 16:04
  • @Zed0121 I have changed the code for your product ID “2795”, that will be purchasable only on sundays… Feb 26, 2019 at 5:10
  • this worked (it essentially removed the add to cart button on the product page). However it doesn’t tell the user why the the button is not there - I.e. It does not “give an error” when someone tries to purchase outside of the required day.
    – Zed0121
    Feb 26, 2019 at 12:40
  • @Zed0121 Outside sundays the specified product Id is not purchasable, and if it is in cart, it's removed. So what it can be done is display message on the specific product pages outside sundays (when add to cart button is hidden). Feb 26, 2019 at 12:49
0

This code snippet will prevent the Sunday product from adding into cart.

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

function restrict_products_on_sunday(  $is_purchasable, $product ){
    $sunday_products = array( 'sunday_product', 'product2' ); //Give here SKUs of the product waht to sell on sundays only

    date_default_timezone_set("Asia/Bangkok"); // give here the time zone of your country
    $sku = $product->get_sku();

    $today = date('w');
    if( (int)$today==0 && in_array( $sku, $sunday_products ) ){
        return true;
    }
    return false;
}

As your requirement is that you want proceeding from checkout, You can use the below code snippet. It will show some message in checkout, if the cart items contains invalid product.

add_action( 'woocommerce_check_cart_items', 'prevent_chckout_for_sunday_products' );
function prevent_chckout_for_sunday_products(){
    $sunday_products = array( 'sunday_product', 'product2' ); //Give here SKUs of the product which will sell on Sundays only
    global $woocmmerce;

    date_default_timezone_set("Asia/Bangkok");
    $today = date('w');

    $items = WC()->cart->get_cart();
    foreach( $items as $key => $item ){
        $sku = $item['data']->get_sku();
        if( (int)$today==0 && in_array( $sku, $sunday_products ) ){
            wc_add_notice( sprintf( 'Look like your cart contains product which is not eligible to checkout today, Consider removing those.' ), 'error' );
            break;
        }
    }
}
3
  • This worked in hiding the add to cart functionality. But it doesn’t solve the “it should give an error”. So when a user tries to purchase it outside of the required day, it should return a checkout errror.
    – Zed0121
    Feb 26, 2019 at 12:44
  • I'll modify the snippet such way that it will prevent proceeding from checkout. Give me some time.
    – Nishad Up
    Feb 27, 2019 at 5:19
  • @Zed0121 , Please see the modified answer, You can proceed with the second snippet.
    – Nishad Up
    Feb 27, 2019 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.