php - WooCommerce 根据自定义字段中的静态日期自动定期更改简单和可变产品的价格
问题描述
我的问题基于(由LoicTheAztec和Xandl)回答另一个问题“根据 Woocommerce 中的自定义字段有条件地设置产品销售价格”中提供的代码。虽然基础代码是基于该问题的答案,但我已经对其进行了进一步开发以满足稍微复杂的要求。
问题:对于简单和可变产品,如何根据自定义字段中的静态日期自动执行定期价格更改?
给出更多的解释;我正在尝试根据输入自定义字段“wooct_time_start”的静态日期创建自动降价到 WooCommerce 产品的正常价格。
下面的代码片段适用于简单的产品,目前设置为在“wooct_time_start”日期/时间之前将价格更改为“$remaining_days”的数量,但我似乎无法让后半部分代码与可变产品一起使用这就是我需要帮助的地方?
add_filter('woocommerce_product_get_price', 'conditional_product_sale_price', 20, 6);
add_filter('woocommerce_product_get_sale_price', 'conditional_product_sale_price', 20, 6);
add_filter('woocommerce_product_variation_get_price', 'conditional_product_sale_price', 20, 6);
add_filter('woocommerce_product_variation_get_sale_price', 'conditional_product_sale_price', 20, 6);
add_filter('woocommerce_variation_prices_price', 'conditional_product_sale_price', 20, 6);
add_filter('woocommerce_variation_prices_sale_price', 'conditional_product_sale_price', 20, 6);
function conditional_product_sale_price($price, $product){
if($product->is_type('simple')){
$date = get_post_meta($product->get_id(), 'wooct_time_start', true);
if(!empty($date)){
$date_time = (int) strtotime($date);
$now_time = (int) strtotime("now");
$remaining_days = floor(($date_time - $now_time) / 86400);
if($remaining_days <= 30){
$price = $remaining_days;
}elseif($remaining_days >= 31 && $remaining_days <= 60){
$price = $remaining_days;
}elseif($remaining_days >= 61 && $remaining_days <= 90){
$price = $remaining_days;
}elseif($remaining_days >= 91 && $remaining_days <= 120){
$price = $remaining_days;
}elseif($remaining_days >= 121 && $remaining_days <= 150){
$price = $remaining_days;
}elseif($remaining_days > 150){
$price = $remaining_days;
}
}
return $price;
}if($product->is_type('variable')){
$children_ids = $product->get_children();
$date = get_post_meta($product->get_id(), 'wooct_time_start', true);
foreach( $children_ids as $child_id ){
$variation = wc_get_product($child_id);
$var_price = $variation->get_price();
if(!empty($date)){
$date_time = (int) strtotime($date);
$now_time = (int) strtotime("now");
$remaining_days = floor(($date_time - $now_time) / 86400);
if($remaining_days <= 30){
$var_price = $remaining_days;
}elseif($remaining_days >= 31 && $remaining_days <= 60){
$var_price = $remaining_days;
}elseif($remaining_days >= 61 && $remaining_days <= 90){
$var_price = $remaining_days;
}elseif($remaining_days >= 91 && $remaining_days <= 120){
$var_price = $remaining_days;
}elseif($remaining_days >= 121 && $remaining_days <= 150){
$var_price = $remaining_days;
}elseif($remaining_days > 150){
$var_price = $remaining_days;
}
}
return $price;
}
}
}
我尝试了对代码的各种迭代,但没有成功:
- 改变线的位置
$date = get_post_meta($product->get_id(), 'wooct_time_start', true);
- 将线路更改
$var_price = $variation->get_price();
为$var_price = $product->get_price();
- 由于存储的瞬态,我已经使用了与woocommerce_get_variation_prices_hash相关的代码,但是尽管在线研究了几个小时,但我仍然不知道它与我的代码有什么直接关系。
更新:
我已经能够使用文章“通过 WooCommerce 3+ 中的挂钩更改产品价格”中提供的答案创建以下更新的代码。
这个新代码还有一个问题 - 它似乎没有成功更新任何“变体”的价格。但是,它适用于“简单”和“可变”产品!
如果有人能够让这个新代码也与“变体”一起工作,那么这将回答这个问题。
function product_life_cycle_main_function($price, $product){
$date = get_post_meta($product->get_id(), 'wooct_time_start', true);
if(!empty($date)){
$date_time = (int) strtotime($date);
$now_time = (int) strtotime("now");
$remaining_days = floor(($date_time - $now_time) / 86400);
if($remaining_days <= 30){
$price = $remaining_days;
}elseif($remaining_days >= 31 && $remaining_days <= 60){
$price = $remaining_days;
}elseif($remaining_days >= 61 && $remaining_days <= 90){
$price = $remaining_days;
}elseif($remaining_days >= 91 && $remaining_days <= 120){
$price = $remaining_days;
}elseif($remaining_days >= 121 && $remaining_days <= 150){
$price = $remaining_days;
}elseif($remaining_days > 150){
$price = $remaining_days;
}
}
return $price;
}
add_filter('woocommerce_product_get_price', 'product_life_cycle_custom_price', 99, 2);
add_filter('woocommerce_product_get_regular_price', 'product_life_cycle_custom_price', 99, 2);
add_filter('woocommerce_product_variation_get_regular_price', 'product_life_cycle_custom_price', 99, 2);
add_filter('woocommerce_product_variation_get_price', 'product_life_cycle_custom_price', 99, 2);
function product_life_cycle_custom_price($price, $product) {
return product_life_cycle_main_function($price, $product);
}
add_filter('woocommerce_variation_prices_price', 'product_life_cycle_custom_variable_price', 99, 3);
add_filter('woocommerce_variation_prices_regular_price', 'product_life_cycle_custom_variable_price', 99, 3);
function product_life_cycle_custom_variable_price($price, $variation, $product) {
wc_delete_product_transients($variation->get_id());
return product_life_cycle_main_function($price, $product);
}
add_filter('woocommerce_get_variation_prices_hash', 'product_life_cycle_variation_prices_hash', 99, 3);
function product_life_cycle_variation_prices_hash($price_hash, $product, $for_display) {
$price_hash[] = product_life_cycle_main_function($price, $product);
return $price_hash;
}
解决方案
我建议重写获取数据的逻辑以使其更健壮。如果产品不是简单的、可变的或变化的,则接受的答案中描述的检查将失败,但可以使其更通用。这是一个如何做到这一点的例子:
function product_life_cycle_main_function($price, $product){
// Determine the product ID to use
// - Parent product ID for variations
// - Product ID for all other products
$meta_product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
// The above can also be simplified as follows:
// - If the product has a parent, take the meta from the parent
// - If the product doesn't have a parent, take the meta from the product
//
// This check would also work with other product types, such as bundled products, subscriptions, etc
//$meta_product_id = ($product->get_parent_id() > 0) ? $product->get_parent_id() : $product->get_id();
$date = get_post_meta($meta_product_id, 'wooct_time_start', true);
// Rest of the code...
return $price;
}
推荐阅读
- javascript - 获取第一个数组中具有 id 的对象与 java 脚本中其他数组的对象中的 id 相同
- ios - 上下文闭包类型 '(Any?) -> Void' 需要 1 个参数,但在闭包体中使用了 2 个
- javascript - 使用 Crossfilter 如何返回特定类型的所有 id 值的数组
- ios - 从 swift cocoa touch 框架导入 jwt
- python-3.x - 为什么我无法连接到数据库
- react-native-navigation - react-native-navigation v2中左侧菜单右侧的阴影去掉?
- c# - 如何使用 C# Fluent 在 SQL 中插入汉字
- ruby-on-rails - Rspec ruby on rails:Haversine gem 在编写单元测试时返回 nil 值
- julia - 错误:LoadError:UndefVarError:TCPServer 未定义 Julia
- google-cloud-platform - Dataflow 停止流式传输到 BigQuery 且没有错误