下方二维码可享价格折扣(启用根据正常价格计算的销售价格)每天 11:00 到 15:00 之间针对选定的产品 ID。
您必须设置:
- 正确的时区(找到你的in here http://php.net/manual/en/timezones.php)
- 适用的折扣率
- 每天的开始和结束时间
- 期间每日打折的相关产品 ID 数组
代码:
// 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;
}
代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。