0

On my WooCommerce website, I am trying to make have a "Lunch Special" daily event. This means that from 11h00 to 15h00, specific products would be discounted due to this "Lunch Special" daily event.

I would like this "Lunch Special" event to reoccur every day of the week.

How could this be achieved?

I have searched this online and I only found plugins that could restrict items per day, not during a specific time period.

Any help is appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Shivang Patel
  • 157
  • 1
  • 3
  • 13
  • 1
    How do you find out what time of the day it is? How do you use that value in an if statement to know if it's between 11 and 15? There you go! – Andreas Oct 27 '18 at 17:56

1 Answers1

1

The code below will make a price discount (enabling a calculated sale price from the regular price) on selected product ids each day between 11h00 and 15h00.

You will have to set:

  • The correct time zone (Find yours in here)
  • The discount rate to be applied
  • The daily start and end time
  • The array of related product Ids to be daily discounted during the period

The code:

// Utility function that gives the discount daily period and the discount rate
function get_discount_period_rate(){
    // Set the correct time zone  (http://php.net/manual/en/timezones.php)
    date_default_timezone_set('Europe/Paris');

    // Set the discount rate
    $rate = 0.8; // <== 20 %

    // Set the start time and the end time
    $start_time = mktime( 11, 00, 00, date("m")  , date("d"), date("Y") );
    $end_time   = mktime( 15, 00, 00, date("m")  , date("d"), date("Y") );
    $time_now   = strtotime("now");

    // Return the rate during allowed discount the period or false outside the period
    return $start_time <= $time_now && $end_time > $time_now ? $rate : false;
}

// Enable calculated on sale price from the regular price and the rate
add_filter( 'woocommerce_product_variation_get_sale_price', 'periodic_discount_prices', 99, 3 );
add_filter( 'woocommerce_product_variation_get_price', 'periodic_discount_prices', 99, 3 );
add_filter( 'woocommerce_variation_prices_sale_price', 'periodic_discount_prices', 99, 3 );
add_filter( 'woocommerce_variation_prices_price', 'periodic_discount_prices', 99, 3 );
add_filter( 'woocommerce_product_get_sale_price', 'periodic_discount_prices', 99, 3 );
add_filter( 'woocommerce_product_get_price', 'periodic_discount_prices', 99, 3 );
function periodic_discount_prices( $price, $product, $parent = 0 ){
    // Set the product Ids that will be discounted
    $discounted_products = array( 37, 41, 53 );

    if( get_discount_period_rate() && in_array( $product->get_id(), $discounted_products ) ){
        $price = $product->get_regular_price() * get_discount_period_rate();
    }
    return $price;
}

// Handling variation prices caching
add_filter( 'woocommerce_get_variation_prices_hash', 'add_rate_to_variation_prices_hash', 99, 1 );
function add_rate_to_variation_prices_hash( $hash ) {
    if( get_discount_period_rate() )
        $hash[] = get_discount_period_rate();
    return $hash;
}

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • A couple of things: I got the following error: Warning: A non-numeric value encountered in /Applications/XAMPP/xamppfiles/htdocs/ecommerce/wp-content/themes/storefront-child/functions.php on line 44 For me, line 44 is: $price = $product->get_regular_price() * get_discount_period_rate(); Is it possible the time didn't account for DST? Also, if it's a variable product, would I include the ids of the variations too? How would I make it so that these products are selected by category not id? Also how would I make it so if the time is not 11 to 15, then the product is not available? – Shivang Patel Oct 28 '18 at 15:24
  • I just realized - the cause of the error might be because my products have variations so they have no "regular price". I'm just guessing, so correct me if I'm wrong. – Shivang Patel Oct 28 '18 at 15:31
  • @ShivangPatel Product variations have active price, regular price and sale price, just as simple products. I will test that with product variations. – LoicTheAztec Oct 28 '18 at 16:04