2017-06-19 154 views
0

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

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

这段代码添加到您的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; 
} 
+0

感谢您的重播。但我需要像这样的百分比折扣 - > http://nimb.ws/meoza2 – Ketan

+0

链接不工作 –

+0

在这里工作 - >看到这个屏幕截图http://nimb.ws/meoza2 – Ketan