根据Woocommerce的每日时间范围对特定产品进行折扣 [英] Discount on specific products based on a daily time range in Woocommerce

查看:185
本文介绍了根据Woocommerce的每日时间范围对特定产品进行折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WooCommerce网站上,我试图举办特别午餐"的日常活动.这意味着从11:00到15:00,由于每日特别午餐"活动,特定产品将打折.

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.

这怎么实现?

我在网上搜索了此内容,但只发现了每天都会限制项目的插件,而不是在特定时间段内.

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

感谢您的帮助.

推荐答案

下面的代码将在每天之间的选定产品编号之间每天折扣价(从正常价格中计算得出的销售价格) 11h00和15h00.

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.

您将必须设置:

  • 正确的时区(在此处找到您的 )
  • 要应用的折扣率
  • 每日开始和结束时间
  • 此期间每天要打折的相关产品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文件中.经过测试,可以正常工作.

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

这篇关于根据Woocommerce的每日时间范围对特定产品进行折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆