未显示总百分比折扣

时间:2017-06-19 06:10:02

标签: wordpress woocommerce

我们使用以下代码显示类别页面产品和单个产品页面上的总折扣百分比。但该代码不显示任何输出。所以,如果有人知道解决方案,请查看此代码并帮助我。

add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 );
function woocommerce_custom_sales_price( $price, $product ) {
  $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
  return $price . sprintf( __('<span class="percentage-dscnt"> %s Off</span>', 'woocommerce' ), $percentage . '%' );
}

1 个答案:

答案 0 :(得分:1)

将此代码添加到您的functions.php中,其当前用于商店页面的屏幕截图http://prntscr.com/flgkcx您可以根据需要进行设计。

function woocommerce_saved_sales_price( $price, $product ) {
$sale = isset($product->sale_price) ? $product->sale_price : '';
if(!empty($sale))
{
    $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
    return $price . sprintf( __('%s', 'woocommerce' ), "<div class='sale-perc'>-" . $percentage ."%</div>" );
}else
{
    return $price;
}
}
add_filter( 'woocommerce_get_price_html', 'woocommerce_saved_sales_price', 10, 2 );

CSS

.sale-perc {
background-color: #D9534F;
display: inline;
padding: .2em .6em .3em;
font-size: 75%;
font-weight: bold;
color: #fff;
text-align: center;
border-radius: .25em;
}
相关问题