【问题标题】:Set Product sale price conditionally based on custom fields in Woocommerce根据 Woocommerce 中的自定义字段有条件地设置产品销售价格
【发布时间】:2018-11-01 19:08:46
【问题描述】:

在 Woocommerce 中,我在我的产品中设置了两个自定义字段:

  1. iadi_price 定制销售价格。
  2. iadi_date 用于自定义日期。

如果指定日期和今天之间的时间少于 3 天,我想更新销售价格。

我写了以下代码:

function fiyatidegistir() {
global $product;
$bugun = time();
$yenifiyat = get_post_meta($product->ID, 'iadi_price', true); //new sale price
$kgn = get_post_meta($product->ID, 'iadi_date', true); // date
if(!empty($kgn)) {
    $kalan = $kgn - $bugun;
    $dakika = $kalan / 60;
    $saat = $dakika / 60;
    $gun = $saat / 24;
    $yil = floor($gun/365);
    $gun_farki = floor($gun - (floor($yil) * 365));
    if($gun_farki<4 && !empty($yenifiyat)) {
        update_post_meta($product->ID, '_price', $yenifiyat);
    }
}
}
add_action( 'woocommerce_before_main_content', 'fiyatidegistir'); 

但它不起作用,什么也没有发生。

我做错了什么?如何根据说明的价格和日期自定义字段以编程方式更改产品销售价格?

【问题讨论】:

    标签: php wordpress date woocommerce price


    【解决方案1】:

    这样做不是一个好主意,因为:

    • 您将如何管理已更新的产品以避免多次连续价格更新。
    • 使用这种方式,它只会杀死您的网站(对许多进程而言)
    • 在您更新有效价格后,您的产品将不再销售
    • 期限结束后,您将无法恢复到初始价格。

    您的代码也充满了关于 Woocommerce 中的 WC_Products 和日期计算的错误。最后一件事,当你写代码时,最好用英文命名变量和函数,用任何人都可以理解的英文注释你的代码。

    请尝试以下适用于简单产品的方法,当您的条件匹配时显示相关产品的销售价格(日期和自定义价格):

    add_filter( 'woocommerce_product_get_price', 'conditional_product_sale_price', 20, 2 );
    add_filter( 'woocommerce_product_get_sale_price', 'conditional_product_sale_price', 20, 2 );
    function conditional_product_sale_price( $price, $product ) {
        if( is_admin() ) return $price;
    
        $new_price = get_post_meta( $product->get_id(), 'iadi_price', true ); //new sale price
        $date      = get_post_meta( $product->get_id(), 'iadi_date', true ); // date
    
        if( ! empty($date) && ! empty($new_price) ) {
            $date_time      = (int) strtotime($date); // Convert date in time
            $now_time       = (int) strtotime("now"); // Now time in seconds
            $one_day        = 86400; // One day in seconds
    
            // Calculate the remaining days
            $remaining_days = floor( ( $date_time - $now_time ) / $one_day );
    
            if( $remaining_days >= 0 && $remaining_days < 4  )
                $price = $new_price;
        }
        return $price;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 谢谢。我们在卖教育。最后三天,五天的票价是不同的。我们正在手动更改它们,我想这样做/轻松学习。
    【解决方案2】:

    如果您来这里是为了让它与变体一起使用,您需要以下所有过滤器:

    add_filter( 'woocommerce_product_get_price', 'conditional_product_sale_price', 10, 2 );
    add_filter( 'woocommerce_product_variation_get_price', 'conditional_product_sale_price', 10, 2 );
    add_filter( 'woocommerce_product_get_sale_price', 'conditional_product_sale_price', 10, 2 );
    add_filter( 'woocommerce_product_variation_get_sale_price', 'conditional_product_sale_price', 10, 2 );
    add_filter( 'woocommerce_variation_prices_price', 'conditional_product_sale_price', 10, 2 );
    add_filter( 'woocommerce_variation_prices_sale_price', 'conditional_product_sale_price', 10, 2 );
    

    并且您需要确保已更改 woocommerce_get_variation_prices_hash 因为存储的瞬态。

    您可能会发现我为客户创建的要点很有用

    https://www.rent-a-ninja.org/programming/wordpress-plugin-entwickler-naehe-graz/woocommerce-custom-sale-price-for-custom-role/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 2019-04-23
      • 2018-07-23
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      相关资源
      最近更新 更多