在 Woocommerce 中显示 FROM 和 TO 日期后的文本

Text after sale price showing FROM and TO dates in Woocommerce

我正在使用 在有限制期限的促销产品上显示自定义格式的价格。

现在我正试图包括 "from" 日期。如何在此代码中获取并包含开始日期?

任何曲目都非常有用和赞赏。

以下代码将在您的价格自定义显示中处理开始销售日期和结束销售日期(需要开始日期和结束日期):

add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
function custom_price_html( $price, $product ){
    if ( is_product() ) {
        // Simple products and variations
        if( $product->is_type( 'simple' ) || $product->is_type( 'variation' )  )
        {
            $sales_price_from = $product->get_date_on_sale_from();
            $sales_price_to = $product->get_date_on_sale_to();
            if( ! empty($sales_price_from) && ! empty($sales_price_to) ){
                $replacement = ' </ins> <span class="notice-price">(on offer from ' . date( 'j.M.Y', $sales_price_from->getTimestamp() ) . ' until ';
                return str_replace( '</ins>', $replacement . date( 'j.M.Y', $sales_price_to->getTimestamp() ) . ')</span>', $price );
            }
        }
        // Variable products
        else if ( $product->is_type( 'variable' ) )
        {
            $from = $to = '';
            // Loop through variations
            foreach ( $product->get_children() as $key => $variation_id ) {
                $variation        = wc_get_product($variation_id);
                $sales_price_from = $variation->get_date_on_sale_from();
                $sales_price_to   = $variation->get_date_on_sale_to();

                if( ! empty($sales_price_from) && ! empty($sales_price_to) ){
                    $date_from = date( 'j.M.Y', $sales_price_from->getTimestamp() );
                    $date_to   = date( 'j.M.Y', $sales_price_to->getTimestamp() );
                    $class     = $key == 0 ? 'class="active"' : '';
                    $from     .= '<i data-id="'.$variation_id.'" data-order="'.($key + 1).'" '.$class.'>'. $date_from .'</i>';
                    $to       .= '<i data-id="'.$variation_id.'" data-order="'.($key + 1).'" '.$class.'>'. $date_to .'</i>';
                }
            }
            if( ! empty($content) ){
                return $price . ' <span class="notice-price">(on offer from ' . $from . ' until ' . $to .')</span>';
            }
        }
    }
    return $price;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。