Discount on specific products based on a daily time range in Woocommerce

时光总嘲笑我的痴心妄想 提交于 2019-12-12 01:15:51

问题


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.


回答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.



来源:https://stackoverflow.com/questions/53024563/discount-on-specific-products-based-on-a-daily-time-range-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!