售后价格中显示FROM和TO日期的文本 [英] Text after sale price showing FROM and TO dates in Woocommerce
问题描述
我正在使用在Woocommerce 3+中,带有日期限制的自定义销售商品价格显示,以在具有限制期限的销售产品上显示自定义格式的价格。
I am using Customs product price on sale displayed with date limit in Woocommerce 3+ to make a custom formatted price display on products on sale that have a limitation period.
现在,我也尝试添加开始日期。如何在代码中获取和包含起始日期?
Now I am trying to include also the "from" date. How can I get and include the from date in this code?
任何曲目都非常有用并且值得赞赏。
Any track is really useful and appreciated.
推荐答案
以下代码将在您的价格自定义显示(需要日期和日期)中处理以下日期和日期的销售日期:
The following code will handle both on sale date from and on sale date to in your price custom display (require both from and to dates):
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文件)。经过测试并有效。
Code goes in function.php file of your active child theme (or active theme). Tested and works.
这篇关于售后价格中显示FROM和TO日期的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!