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.
Advertisement
Answer
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.