Php 基于Woocommerce中每日时间范围的特定产品折扣

Php 基于Woocommerce中每日时间范围的特定产品折扣,php,wordpress,time,woocommerce,discount,Php,Wordpress,Time,Woocommerce,Discount,在我的WooCommerce网站上,我正试图让“午餐”成为一项特殊的日常活动。这意味着从11:00到15:00,特定产品将因这一每日午餐特别活动而打折 我希望这个午餐特别活动在一周的每一天都能重演 如何做到这一点 我在网上搜索过这个,我只找到了可以限制每天项目的插件,而不是在特定的时间段内 感谢您的帮助。以下代码将提供价格折扣,使您能够在每天11:00到15:00之间从所选产品ID的常规价格计算出销售价格 您必须设置: 找到你的正确时区 要应用的贴现率 每日开始和结束时间 在此期间每日折扣的相关

在我的WooCommerce网站上,我正试图让“午餐”成为一项特殊的日常活动。这意味着从11:00到15:00,特定产品将因这一每日午餐特别活动而打折

我希望这个午餐特别活动在一周的每一天都能重演

如何做到这一点

我在网上搜索过这个,我只找到了可以限制每天项目的插件,而不是在特定的时间段内


感谢您的帮助。

以下代码将提供价格折扣,使您能够在每天11:00到15:00之间从所选产品ID的常规价格计算出销售价格

您必须设置:

找到你的正确时区 要应用的贴现率 每日开始和结束时间 在此期间每日折扣的相关产品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文件。测试和工作。

您如何知道现在是一天中的什么时候?如何在if语句中使用该值来确定它是否在11和15之间?好了!有几件事:我得到了以下错误:警告:在第44行的/Applications/XAMPP/xamppfiles/htdocs/ecommerce/wp content/themes/storefront child/functions.php中遇到了一个非数字值。对我来说,第44行是:$price=$product->get\u regular\u price*get\u discounter\u period\u rate;有没有可能时间不包括DST?另外,如果它是一个可变产品,我是否也会包括可变产品的ID?如何使这些产品按类别而不是id进行选择?还有,如果时间不是11到15,那么产品就不可用了,我该怎么做呢?我刚刚意识到——错误的原因可能是因为我的产品有变化,所以没有固定的价格。我只是在猜测,所以如果我错了,请纠正我。@ShivangPatel的产品变体有现行价格、正常价格和销售价格,就像简单的产品一样。我将用产品变化来测试这一点。