根据 Woocommerce 中的自定义字段有条件地设置产品销售价格
Set Product sale price conditionally based on custom fields in Woocommerce
在 Woocommerce 中,我在我的产品中设置了两个自定义字段:
iadi_price
定制促销价。
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');
但是它不起作用,什么也没有发生。
我做错了什么?如何根据所解释的价格和日期自定义字段以编程方式更改产品销售价格?
这样做不是一个好主意,因为:
- 您将如何管理已更新的产品以避免多次连续更新价格。
- 使用这种方式,它只会杀死您的网站(对许多进程而言)
- 您更新有效价格后,您的产品将停止销售
- 期间结束后,您将无法恢复到初始价格。
此外,您的代码充满了关于 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 文件。已测试并有效。
如果你来这里是为了让它与变体一起工作,你需要以下所有过滤器:
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 因为存储的瞬变。
您可能会发现我为客户创建的要点很有用
在 Woocommerce 中,我在我的产品中设置了两个自定义字段:
iadi_price
定制促销价。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');
但是它不起作用,什么也没有发生。
我做错了什么?如何根据所解释的价格和日期自定义字段以编程方式更改产品销售价格?
这样做不是一个好主意,因为:
- 您将如何管理已更新的产品以避免多次连续更新价格。
- 使用这种方式,它只会杀死您的网站(对许多进程而言)
- 您更新有效价格后,您的产品将停止销售
- 期间结束后,您将无法恢复到初始价格。
此外,您的代码充满了关于 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 文件。已测试并有效。
如果你来这里是为了让它与变体一起工作,你需要以下所有过滤器:
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 因为存储的瞬变。
您可能会发现我为客户创建的要点很有用