問題描述
我正在嘗試在使用 WooCommerce 的網站中添加折扣百分比價格.
I’m trying to add a discount percentage aside price in a site that uses WooCommerce.
我已將此腳本應用于標準價格和銷售價格:
I’ve applied this script for the standard price and the sale price:
// Add save percentage next to sale item prices.
add_filter( 'woocommerce_get_price_html', 'adventure_tours_sales_price', 10, 2 );
function adventure_tours_sales_price( $price, $product ){
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
}
上面的腳本有效.
在前端,我有價格百分比.
In front-end, I have the price percentage.
現在我想對產品變化價格應用相同的腳本.
Now I want apply the same script to the product variation price.
我檢查了產品變體選項并嘗試了以下操作:
I’ve checked the product variation option and tried something like this:
// Add save percentage next to sale item prices.
add_filter( 'woocommerce_get_price_html', 'adventure_tours_sales_price', 10, 2 );
function adventure_tours_sales_price( $price, $product ){
if( $product->is_type( 'variable' ) ) {
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
}else{
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
}
}
但它不起作用,百分比不適用于價格.
But it does not work, the percentage is not applied to the price.
前端也不行.
推薦答案
針對 WooCommerce 版本 3+ 進行了更新 |已棄用的替代品
- 將woocommerce_variable_sale_price_html"替換為woocommerce_variable_get_price_html"
- 將woocommerce_sale_price_html"替換為woocommerce_get_price_html"
- 將woocommerce_price()"替換為wc_price()"
- 將
WC_Product
價格屬性替換為WC_Product
價格方法
- Replaced 'woocommerce_variable_sale_price_html' by 'woocommerce_variable_get_price_html'
- Replaced 'woocommerce_sale_price_html' by 'woocommerce_get_price_html'
- Replaced 'woocommerce_price()' by 'wc_price()'
- Replaced
WC_Product
price properties byWC_Product
price methods
可變產品更復雜,因為您有 2 個不同的價格位置,第一個顯示最小和最大價格(當您有多個變體時),第二個顯示所選變體的價格.我對你的原始代碼做了很多更改.
For variable products is more complicated as you have 2 different locations with prices, the first one displays the minimal and maximal price (when you have multiple variations) and the second one displays the price from the selected variations. I have changed a lot your original code.
這是顯示折扣百分比周圍的自定義動態標簽的正確代碼:
Here the correct code to display that custom dynamic labels arround discounted percentages:
add_filter('woocommerce_variable_get_price_html','adventure_tours_sales_price', 10, 2 );
add_filter('woocommerce_get_price_html','adventure_tours_sales_price', 10, 2 );
function adventure_tours_sales_price( $price, $product ){
// Variables initialisation
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
$save_text = __('Save', 'woocommerce') . ' ';
if(!empty($sale_price)) {
// Percentage calculation
$percentage = '<span class="save-percent"> ' .$save_text . round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 ) . '%</span>';
$price = '<del class="strike">' . wc_price( $regular_price ) . '</del>
<ins class="highlight">' . wc_price( $sale_price ) . $percentage . '</ins>';
} else {
$price = '<ins class="highlight">'.wc_price( $regular_price ).'</ins>';
}
return $price;
}
add_filter('woocommerce_variable_get_price_html', 'adventure_tours_sales_min_max_prices', 20, 2);
function adventure_tours_sales_min_max_prices( $price, $product) {
// Variables initialisation
$variation_min_reg_price = $product->get_variation_regular_price('min', true);
$variation_max_reg_price = $product->get_variation_regular_price('max', true);
$variation_min_sale_price = $product->get_variation_sale_price('min', true);
$variation_max_sale_price = $product->get_variation_sale_price('max', true);
$percentage_min = '';
$percentage_max = '';
$save_text = __('Save', 'woocommerce') . ' ';
// Percentage calculations
if($variation_min_reg_price != $variation_min_sale_price)
$percentage_min = '<span class="save-percent-min"> (' .$save_text . round( ( ( $variation_min_reg_price - $variation_min_sale_price ) / $variation_min_reg_price ) * 100 ) . '%)</span>';
if($variation_max_reg_price != $variation_max_sale_price)
$percentage_max = '<span class="save-percent-max"> (' .$save_text . round( ( ( $variation_max_reg_price - $variation_max_sale_price ) / $variation_max_reg_price ) * 100 ) . '%)</span>';
if (($variation_min_reg_price != $variation_min_sale_price) || ($variation_max_reg_price != $variation_max_sale_price)) {
$price = '<del class="strike">' . wc_price($variation_min_reg_price) . '-' . wc_price($variation_max_reg_price) . '</del>
<ins class="highlight">' . wc_price($variation_min_sale_price) . $percentage_min . ' - ' . wc_price($variation_max_sale_price) . $percentage_max . '</ins>';
}
return $price;
}
代碼位于活動子主題(或主題)的functions.php 文件或任何插件文件中.
在 Woocommerce 版本 3+ 上測試并運行
Tested and works on Woocommerce version 3+
相關答案:
- 添加產品打折時的價格自定義文本標簽
- 圍繞產品的條件自定義輸出銷售價格和正常價格
這篇關于將折扣百分比添加到銷售的可變產品中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!