WooCommerce 根据自定义字段中的静态日期自动定期更改简单和可变产品的价格

WooCommerce automated regular price changes based on a static date within a custom field for simple and variable products

我的问题基于答案中提供的代码(LoicTheAztec and Xandl) to another question ''。虽然基础代码基于该问题的答案,但我对其进行了进一步开发以满足稍微复杂的要求。

问题:对于简单产品和可变产品,如何根据自定义字段中的静态日期自动进行定期价格更改?

再多解释一下;我正在尝试根据在自定义字段中输入的静态日期创建 WooCommerce 产品正常价格的自动降价 'wooct_time_start'。

下面的代码片段适用于简单的产品,目前设置为将价格更改为 'wooct_time_start' date/time 之前的“$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;
                
            }
    }   
}

我已尝试对代码进行各种迭代,但没有成功:

更新:

我已经能够使用文章“”中提供的答案创建以下更新代码。

这个新代码还有一个问题 - 它似乎没有成功更新任何 'variations' 的价格。但是,它适用于 'Simple' 和 'Variable' 产品!

如果任何人也能够让这个新代码与 'variations' 一起使用,那么这将回答这个问题。

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;
    }

使用 Diego 给出的建议,我能够添加以下内容以使代码正常工作:

function product_life_cycle_main_function($price, $product){
    
    $product_id = ($product->get_parent_id() > 0) ? $product->get_parent_id() : $product->get_id();
    
    $date = get_post_meta($product_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) {
        global $price;
        $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;
}