[英]How to add a CSS class for the displayed discounted percentage near sale price on single product pages in WooCommerce
[英]Add custom text with sale dates after sale price in Woocommerce Single product
我有這段代碼,它在銷售價格后添加了一個文本,即使函數文件中沒有 PHP 錯誤,它也會在日志中生成一個錯誤,說“做錯了”。
知道為什么要“抱怨”嗎? 這是代碼:
add_filter( 'woocommerce_get_price_html', 'sale_dates_after_price', 100, 2 );
function sale_dates_after_price( $price, $product ) {
$sales_price_from = get_post_meta( $product->id, '_sale_price_dates_from', true );
$sales_price_to = get_post_meta( $product->id, '_sale_price_dates_to', true );
if ( is_single() && $product->is_on_sale() && $sales_price_to != "" ) {
$sales_price_date_from = date( "j M y", $sales_price_from );
$sales_price_date_to = date( "j M y", $sales_price_to );
$price = str_replace( '</ins>', ' </ins> <span class="sale-dates"><b>offer valid between ' . $sales_price_date_from . ' and ' . $sales_price_date_to . '</b></span>', $price );
}
return apply_filters( 'woocommerce_get_price', $price );
}
你的代碼已經過時,有一些錯誤和錯誤……當你使用格式化的銷售價格時,有一個更好的鈎子。 請嘗試以下操作:
add_filter( 'woocommerce_format_sale_price', 'sale_dates_after_price', 10, 3 );
function sale_dates_after_price ( $price, $regular_price, $sale_price ) {
global $product;
if( ! is_a($product, 'WC_Product') )
return $price;
$date_from = $product->get_date_on_sale_from();
$date_to = $product->get_date_on_sale_to();
if( is_product() && ! ( empty($date_from) && empty($date_to) ) ) {
$date_from = date( "j M y", strtotime($date_from) );
$date_to = date( "j M y", strtotime($date_to) );
$price .= ' <span class="sale-dates"><strong>offer valid between '.$date_from.' and '.$date_to.'</strong></span>';
}
return $price;
}
代碼位於活動子主題(或活動主題)的 function.php 文件中。 測試和工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.