【问题标题】:Discount on specific products based on a daily time range in Woocommerce根据 Woocommerce 中的每日时间范围对特定产品进行折扣
【发布时间】:2018-10-27 17:35:05
【问题描述】:

在我的 WooCommerce 网站上,我正在尝试制作一个“午餐特价”每日活动。这意味着从 11:00 到 15:00,特定产品将因“午餐特价”每日活动而打折。

我希望这个“午餐特别”活动在一周中的每一天都再次发生。

如何做到这一点?

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

感谢任何帮助。

【问题讨论】:

  • 你如何知道现在是什么时间?您如何在 if 语句中使用该值来知道它是否在 11 到 15 之间?给你!

标签: php wordpress time woocommerce discount


【解决方案1】:

下面的代码将在每天 11:00 到 15:00 之间对选定的产品 ID 进行价格折扣(启用从正常价格计算的销售价格)

你必须设置:

  • 正确的时区(找到你的in here
  • 要应用的折扣率
  • 每天的开始和结束时间
  • 期间每日折扣的相关产品 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 文件中。经过测试并且可以工作。

【讨论】:

  • 有几件事:我收到以下错误:警告:在 /Applications/XAMPP/xamppfiles/htdocs/ecommerce/wp-content/themes/storefront-child/functions 中遇到的非数字值第 44 行的 .php 对我来说,第 44 行是: $price = $product->get_regular_price() * get_discount_period_rate();有没有可能时间没有考虑到夏令时?另外,如果它是可变产品,我是否也会包含变体的 ID?我将如何使这些产品按类别而不是 id 选择?另外,如果时间不是 11 到 15 点,那么我将如何做到这一点,那么产品不可用?
  • 我刚刚意识到 - 错误的原因可能是因为我的产品有变化,所以它们没有“正常价格”。我只是猜测,如果我错了,请纠正我。
  • @ShivangPatel 产品变体有活跃价格、正常价格和销售价格,就像简单的产品一样。我将通过产品变化进行测试。
猜你喜欢
  • 2020-04-26
  • 2021-03-14
  • 2021-01-29
  • 1970-01-01
  • 2021-05-18
  • 1970-01-01
  • 2021-05-16
  • 2019-02-06
  • 2021-06-10
相关资源
最近更新 更多