php - 在 Woocommerce 中显示 FROM 和 TO 日期的售后文本
问题描述
我正在使用Woocommerce 3+中显示的带有日期限制的海关产品销售价格来对具有限制期限的销售产品进行自定义格式的价格显示。
现在我试图包括“从”日期。如何在此代码中获取并包含起始日期?
任何曲目都非常有用和赞赏。
解决方案
以下代码将在您的价格自定义显示中处理销售日期从和销售日期到(需要从和到日期):
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 文件中。测试和工作。
推荐阅读
- javascript - href 点击触发选择选项的下拉菜单
- docker - 没有基本的身份验证凭据 - `docker push image_name`
- c# - 当 TextBox 失去焦点时在 ViewModel 中运行函数?
- java - Spring @Component 未正确读取 .properties 字段
- android - 从私人游戏商店 iFrame 中删除应用程序
- c# - 为什么这条 MVC 路线不起作用(ASP.Net Core 2.2)
- azure-api-management - 如何在 Azure API 管理的 set-body 中引发异常?
- powershell - 检查开关参数的正确方法是什么
- python - 使用 z3Py 证明两个表达式范围的等价/不同
- operating-system - VirtualBox上PCI Express(PCIe)配置空间访问问题