Running a restaurant delivery website, I would like to create sale products based on “days of the week”.
Tuesday 5 products Wednesday 10 products Saturday 6 products
For example, if Tuesday and product id is on sale, then sales price should be shown. I tried re-using this but can’t find a topic about adding the sales price.
// Utility conditional function that check if day is tuesday (returns boolean) function is_tuesday() { // Set Your shop time zone (http://php.net/manual/en/timezones.php) date_default_timezone_set('Europe/London'); // If the current day is "tuesday" return true (else retun false) return ( date('w') == 2 ) ? true : false; } // check if day is wednesday (returns boolean) function is_wednesday() { date_default_timezone_set('Europe/London'); return ( date('w') == 3 ) ? true : false; } // check if day is saturday (returns boolean) function is_saturday() { date_default_timezone_set('Europe/London'); return ( date('w') == 6 ) ? true : false; } // Utility function (setting your product IDS in the array) function tuesday_products() { // HERE your product IDs in the array (need to be coma separated) return array( 37 ); } function wednesday_products() { return array( 38,66,42 ); } function saturday_products() { return array( 321,87 ); } // Enable sales price for specific items on conditional days only add_filter( 'woocommerce_product_get_price', 'enable_specific_products_on_tuesday', 10, 2 ); add_filter( 'woocommerce_product_variation_get_price', 'enable_specific_products_on_tuesday', 10, 2 ); function enable_specific_products_on_tuesday( $purchasable, $product ) { // Enable sales price for specific defined items only on tuesday if( ! is_tuesday() && in_array( $product->get_id(), tuesday_products() ) ) $sale_price = $product->get_sale_price(); }
Advertisement
Answer
I havent been able to test this, but as an alternative to editing the price, which will hide the On Sale messages, as the product wouldnt really be a sale item according to WooCommerce.
This method, uses the existing sales functionality of WooCommerce by utilising the sale schedule from and to. Each day a cronjob runs and determines the current day, and either sets the sale from / to really far in the future, or to today. Allow you to keep storing the sales price in the normal field without the product being stuck on sale all the time, and WooCommerce visually will show to the customer the sale discount.
You will have to make sure you set a sale from / to, when updating products to stop the product going on sale straight away.
// Set Your shop time zone (http://php.net/manual/en/timezones.php) date_default_timezone_set('Europe/London'); // Setup cron schedule add_action('init', 'setup_schedule', 1); function setup_schedule() { if (!wp_next_scheduled('my_cron_hook')) { wp_schedule_event(time(), 'twicedaily', 'my_cron_hook'); } } // Configure product sales on schedule add_action('my_cron_hook', 'config_product_sales'); function config_product_sales() { if (is_tuesday()) { addProductSale(tuesday_products()); } else { removeProductSale(tuesday_products()); } if (is_wednesday()) { addProductSale(wednesday_products()); } else { removeProductSale(wednesday_products()); } if (is_saturday()) { addProductSale(saturday_products()); } else { removeProductSale(saturday_products()); } } function addProductSale($products) { foreach ($products as $id) { $product = new WC_Product($id); if (!$product->is_on_sale()) { // Set scheduled date to now $product->set_date_on_sale_from(time()); $product->set_date_on_sale_to(1847300208); // Needed to stop default woo logic unsetting the sale $product->save(); } } } function removeProductSale($products) { foreach ($products as $id) { $product = new WC_Product($id); if ($product->is_on_sale()) { // Set scheduled date to far in the future, so the sale never comes on normally $product->set_date_on_sale_from(1847300208); $product->set_date_on_sale_to(1847300208); $product->save(); } } } function is_tuesday() { return date('w') == 2 ? true : false; } function is_wednesday() { return date('w') == 3 ? true : false; } function is_saturday() { return date('w') == 6 ? true : false; } function tuesday_products() { // HERE your product IDs in the array (need to be coma separated) return array( 37 ); } function wednesday_products() { // HERE your product IDs in the array (need to be coma separated) return array( 38,66,42 ); } function saturday_products() { // HERE your product IDs in the array (need to be coma separated) return array( 321,87 ); }