我想在产品标题下显示销售百分比。这是我在functions.php页面中的函数,但是我得到了NAN% Off
// Function to calculate and display sale percentage.
function display_sale_percentage() {
global $product;
if ( $product->is_on_sale() ) {
// Get regular and sale prices
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
// Calculate discount percentage
$discount_percentage = round( ( ($regular_price - $sale_price) / $regular_price ) * 100 );
// Display the percentage
echo '<span class="sale-percentage">' . $discount_percentage . '% off</span>';
}
}
// Hook the function to a suitable location in your product archive template.
// For example, you can hook it into the product loop.
add_action( 'woocommerce_before_shop_loop_item_title', 'display_sale_percentage', 25 );
您的代码需要处理可变产品及其变体价格。
对于可变产品,将显示以下代码
对于打折的简单商品,会显示折扣百分比。
// Utility function: Get an array of discount percentages from the variations
function get_variations_discount_percentage( $product ) {
$prices = $product->get_variation_prices( true );
$percentages = array();
foreach( $prices['regular_price'] as $key_id => $price ) {
if( $price != $prices['price'][$key_id] ) {
$reg_price = floatval($price);
$sale_price = floatval($prices['price'][$key_id]);
$percentages[] = round( ($reg_price - $sale_price) / $reg_price * 100);
}
}
return array_unique($percentages);
}
// Displays On Sale product discount percentage
add_action( 'woocommerce_before_shop_loop_item_title', 'display_products_on_sale_percentage', 25 );
function display_products_on_sale_percentage() {
global $product;
if ( ! $product->is_on_sale() ) return; // Only "On sale" products
if ( $product->is_type('variable') ) {
$percentages = get_variations_discount_percentage( $product );
if ( count($percentages) > 1 ) {
$percentage_min = min($percentages);
$percentage_max = max($percentages);
// Display the discount percentages range
printf( '<span class="sale-percentage">From %d%% to %d%% off</span>', min($percentages), max($percentages) );
} elseif ( count($percentages) === 1 ) {
// Display the discount percentage
printf( '<span class="sale-percentage">%d%% off</span>', current($percentages) );
}
} else {
$reg_price = (float) $product->get_regular_price();
$sale_price = (float) $product->get_sale_price();
// Display the discount percentage
printf( '<span class="sale-percentage">%d%% off</span>',
round( ($reg_price - $sale_price) / $reg_price * 100) );
}
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。