2017-06-19 153 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