您可以使用is_product() 条件函数,但这还不够,因为单个产品页面上还有其他产品循环,例如追加销售产品、相关产品……
要使您的功能仅对单个产品页面(而不是其他产品循环)有效,请使用以下内容:
add_filter( 'woocommerce_format_sale_price', 'display_percentage_discount_on_sale_price_format', 99, 3 );
function display_percentage_discount_on_sale_price_format( $price, $regular_price, $sale_price ){
global $woocommerce_loop;
// Only on single product pages (and not on other product loops)
if( is_product() && isset($woocommerce_loop['name']) && empty($woocommerce_loop['name']) ) {
$percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
$price = '<ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins> <del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del><br><span class="plan-atc">*Payment Plan Available at Checkout</span><br> <span class="below-atc">Add to cart today, get ' . $percentage . __( ' off </span>', 'text-domain' );
}
return $price;
}
现在缺少关闭 </span>。请尝试以下重新访问的代码:
add_filter( 'woocommerce_format_sale_price', 'display_percentage_discount_on_sale_price_format', 99, 3 );
function display_percentage_discount_on_sale_price_format( $price, $regular_price, $sale_price ){
global $woocommerce_loop;
// Only on single product pages (and not on other product loops)
if( is_product() && isset($woocommerce_loop['name']) && empty($woocommerce_loop['name']) ) {
$percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
$text_end = sprintf( __("Add to cart today, get %s off", "woocommerce"), $percentage );
$text_prefix = sprintf( ' <br><span class="plan-atc">%s</span> <br><span class="below-atc">%s</span>',
__("Payment Plan Available at Checkout", "woocommerce"), $text_end );
$price_html = '<ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins> <del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del>' . $text_prefix;
}
return $price_html;
}
代码进入活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。