wordpress - 在 Woocommerce 产品页面上显示产品销售结束日期
问题描述
我有这个代码,但它似乎过时了
add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
global $product;
function custom_price_html( $price, $product ) {
if ( is_single() && $product->is_on_sale() && $sales_price_to != "" ) {
$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) ){
$sales_price_date_to = $sales_price_from->date( "j.m.Y");
$sales_price_date_from = $sales_price_to->date( "j.m.Y");
$sales_date = '<p class="offer_date">Angebot vom '.$sales_price_date_from.' bis '.$sales_price_date_to.'</p>';
} else {
$sales_date = $sales_price_from = $sales_price_to = '';
}
$price = str_replace( '</ins>', ' </ins> <b>(Offer from ' . $sales_price_date_from . ' till ' . $sales_price_date_to . ')</b>', $price );
}
return $price;
}
我想显示简单和可变产品的销售结束日期。
解决方案
您正在使用未定义的 $sales_price_to 变量。请使用下面的代码,经过测试并且可以完美运行。
add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
function custom_price_html( $price, $product ) {
if ( is_single() && $product->is_on_sale() ) {
$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) ){
$sales_price_date_from = $sales_price_from->date( "j.m.Y");
$sales_price_date_to = $sales_price_to->date( "j.m.Y");
$sales_date = '<p class="offer_date">Angebot vom '.$sales_price_date_from.' bis '.$sales_price_date_to.'</p>';
} else {
$sales_date = $sales_price_from = $sales_price_to = '';
}
$price = str_replace( '</ins>', ' </ins> <b>(Offer from ' . $sales_price_date_from . ' till ' . $sales_price_date_to . ')</b>', $price );
}
return $price;
}
推荐阅读
- permissions - 如何使用 devicepolicymanager 为我的设备所有者应用程序在 android 10 中获取 android.permission.ACCESS_BACKGROUND_LOCATION?
- flutter - 如何使用 FutureBuilder 等待从屏幕或对话框返回数据?
- vba - VBA 命名范围
- javascript - 当消息 ID 末尾有重复数字时,设置消息不显示
- python - 条件语句的优先顺序
- node.js - 加密/解密sails.js 特定模型值如何工作?
- python - 如何将现有的 django 项目移动到 pycharm
- google-sheets - 如何使用导入范围将内容导入单个单元格而不是整行?
- java - 如何停止多个线程更新全局变量?
- r - R 对象未在运行时环境中正确存储