【问题标题】:Set WooCommerce Price based on Custom Field and Custom Date (Not Working)根据自定义字段和自定义日期设置 WooCommerce 价格(不工作)
【发布时间】:2021-10-14 18:29:34
【问题描述】:

我正在为 WooCommerce 中的单个产品制定反向“销售价格”,如“Future Price”。

总体思路是设置价格(未来价格)和开始+结束日期。 然后,使用woocommerce_product_get_price 过滤器,检查今天是否与“未来价格开始日期”的值相同,如果相同,则更改产品价格。

当未来价格结束日期过去后,将产品价格恢复为正常价格。

问题是:我没有收到任何错误或通知或类似的东西——这意味着我迷路了:)

为了完成这一切,我创建了一个自定义字段(未来价格)。我还创建了日期字段。它们都已保存和更新。

如果有人可以提供帮助并给我提示/指点,我将不胜感激。这是我一周内到期的第二个(3 个中的 2 个)学校项目。

这是我的完整代码:

add_filter( 'woocommerce_product_get_price', 'future_product_price', 20, 2 );
function future_product_price( $price, $product ) {

    if ( is_admin() ) return $price;

    $future_price       = get_post_meta( $product->get_id(), 'future_price', true ); // future product price
    $future_start_date  = get_post_meta( $product->get_id(), '_future_price_date_from', true ); // future product price start date
    $future_end_date    = get_post_meta( $product->get_id(), '_future_price_date_to', true ); // future product price end date

    if ( ! empty( $future_price ) && ! empty( $future_start_date ) && ! empty( $future_end_date ) ) {

        $future_date_time   = (int) strtotime( $future_start_date ); // convert
        $now_time           = (int) strtotime("now"); // now in seconds

        // check if right now is the same day as the future price start date
        if ( $now_time == $future_start_date )

            // if they are the same, set the product price to the future price
            $price = $future_price;
    }

    return $price;
}

【问题讨论】:

    标签: javascript jquery wordpress woocommerce


    【解决方案1】:

    将“add_filter woocommerce_product_get_price”更改为“woocommerce_get_price_html”以显示功能价格,并在您的 future_product_price 函数中进行一些小的更改:

    add_filter( 'woocommerce_get_price_html', 'future_product_price', 20, 2 );
    function future_product_price( $price, $product ) {
    
        if ( is_admin() ) return $price;
    
        $future_price       = get_post_meta( $product->get_id(), 'future_price', true ); // future product price
        $future_start_date  = get_post_meta( $product->get_id(), '_future_price_date_from', true ); // future product price start date
        $future_end_date    = get_post_meta( $product->get_id(), '_future_price_date_to', true ); // future product price end date
    
        if ( ! empty( $future_price ) && ! empty( $future_start_date ) && ! empty( $future_end_date ) ) {
    
            $future_start = (int) strtotime( $future_start_date ); // convert
            $future_end   = (int) strtotime( $future_end_date ); // convert
            $now_time     = (int) strtotime(date('Y-m-d')); // now in seconds
            // check if right now is the same day as the future price start date
            if($future_start <= $now_time && $now_time <= $future_end) {
                // if they are the same, set the product price to the future price
                $price = $future_price;
            }
        }
        return $price;  
    }
    

    添加其他功能以在购物车或结帐中设置未来价格:

    add_action( 'woocommerce_before_calculate_totals', 'set_alter_price_cart', 20, 2 );
    
    function set_alter_price_cart( $cart ) {
    
    if ( is_admin()) return;
    
        foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
    
            $product = $cart_item['data'];
            $price = $product->get_price();
    
            $future_price       = get_post_meta( $product->get_id(), 'future_price', true ); // future product price
            $future_start_date  = get_post_meta( $product->get_id(), '_future_price_date_from', true ); // future product price start date
            $future_end_date    = get_post_meta( $product->get_id(), '_future_price_date_to', true ); // future product price end date
    
            if ( ! empty( $future_price ) && ! empty( $future_start_date ) && ! empty( $future_end_date ) ) {
                
                $future_start = (int) strtotime( $future_start_date ); // convert
                $future_end   = (int) strtotime( $future_end_date ); // convert
                $now_time     = (int) strtotime(date('Y-m-d')); // now in seconds
                // check if right now is the same day as the future price start date
                if($future_start <= $now_time && $now_time <= $future_end) {
                    $price =  $future_price;
                }
                $cart_item['data']->set_price($price);
            }
        } 
    }
    

    【讨论】:

    • is_admin() 检查您是否在 WP 管理员中,而不是您是否已登录。我将查看您提到的其他更改。
    • 请多解释以便更好理解
    • 你需要我解释什么?
    • 效果很好。谢谢您的帮助。还需要我解释一下吗?
    • 好的,谢谢,如果一切正常,那么无需解释更多。
    猜你喜欢
    • 2019-04-24
    • 1970-01-01
    • 2023-04-10
    • 2018-11-01
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    相关资源
    最近更新 更多