Adding custom text labels to the prices when products are on sale(在产品打折时为价格添加自定义文本标签)
问题描述
在 WooCommerce 产品单页上,如果产品具有销售价格,则将正常价格划掉,并在其后面突出显示销售价格.
我的问题:
如何添加旧价格:XX美元"
和新价格:XX美元"
等标签而不是只有划掉的和新的价格(销售价格)?
更新 2(针对简单和可变的产品 + 解决了相同变体价格的错误)
当产品打折时,您可以使用挂接在
woocommerce_sale_price_html
和woocommerce_variation_sale_price_html
过滤器钩子(用于简单和可变的产品.对于变量产品的最低/最高价格,我们需要在
woocommerce_variation_sale_price_html
过滤器钩子中挂钩不同的函数.代码如下:
add_filter('woocommerce_variation_sale_price_html','sale_prices_custom_labels', 10, 2 );add_filter('woocommerce_sale_price_html','sale_prices_custom_labels', 10, 2);函数 sale_prices_custom_labels( $price, $product ){如果 (isset($product->sale_price)) {$price = '
'.__('旧价格:', 'woocommerce') .woocommerce_price( $product->regular_price ).'</del><ins class="highlight">'.__('新价格:', 'woocommerce') .woocommerce_price( $product->sale_price ) .'</ins>';}别的{$price = '<ins class="highlight">'.woocommerce_price( $product->regular_price ).'</ins>';}返回 $price;}add_filter('woocommerce_variable_sale_price_html', 'sale_prices_custom_labels_min_max', 20, 2);函数 sale_prices_custom_labels_min_max( $price, $product) {$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);如果 ( $variation_min_reg_price != $variation_min_sale_price || $variation_max_reg_price != $variation_max_sale_price ){if($variation_min_reg_price == $variation_max_reg_price && $variation_min_sale_price == $variation_max_sale_price ){$price = ''.__('旧价格:', 'woocommerce') .woocommerce_price($variation_max_reg_price) .'</del><ins class="highlight">'.__('新价格:', 'woocommerce') .woocommerce_price($variation_max_sale_price) .'</ins>';}elseif($variation_min_reg_price != $variation_max_reg_price && $variation_min_sale_price == $variation_max_sale_price ){$price = ''.__('旧价格:', 'woocommerce') .woocommerce_price($variation_min_reg_price) .'-' .woocommerce_price($variation_max_reg_price) .'</del><ins class="highlight">'.__('新价格:', 'woocommerce') .woocommerce_price($variation_max_sale_price) .'</ins>';}elseif($variation_min_reg_price == $variation_max_reg_price && $variation_min_sale_price != $variation_max_sale_price ){$price = ''.__('旧价格:', 'woocommerce') .woocommerce_price($variation_max_reg_price) .'</del><ins class="highlight">'.__('新价格:', 'woocommerce') .woocommerce_price($variation_min_sale_price) .'-' .woocommerce_price($variation_max_sale_price) .'</ins>';}别的{$price = ''.__('旧价格:', 'woocommerce') .woocommerce_price($variation_min_reg_price) .'-' .woocommerce_price($variation_max_reg_price) .'</del><ins class="highlight">'.__('新价格:', 'woocommerce') .woocommerce_price($variation_min_sale_price) .'-' .woocommerce_price($variation_max_sale_price) .'</ins>';}}返回 $price;}<块引用>
你也可以用一些东西替换普通的
和
html 标签else 并更改或添加一些类(如果对您更方便的话).此时一切皆有可能.
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.
此代码已经过测试且有效.
<小时>相关答案:条件定制围绕产品销售价格和正常价格的输出
On WooCommerce product single pages, if a product owns a sales price, the normal price is crossed out and behind it, the sale price is highlighted.
My question:
How can I add a label like "Old Price: XX Dollar"
and "New Price: XX Dollar"
instead of only the crossed out and the new price (sale price)?
Update 2 (for simple and variable products + solved the bug on same variations prices)
when products are on sale, you can add custom labels just as you want using a custom function hooked in woocommerce_sale_price_html
and woocommerce_variation_sale_price_html
filters hooks (for simple and variables products.
For the min / max prices in variables products, we need a different function hooked in woocommerce_variation_sale_price_html
filter hook.
Here is that code:
add_filter('woocommerce_variation_sale_price_html','sale_prices_custom_labels', 10, 2 );
add_filter('woocommerce_sale_price_html','sale_prices_custom_labels', 10, 2 );
function sale_prices_custom_labels( $price, $product ){
if (isset($product->sale_price)) {
$price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price( $product->regular_price ). '</del>
<ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price( $product->sale_price ) . '</ins>';
}
else
{
$price = '<ins class="highlight">'.woocommerce_price( $product->regular_price ).'</ins>';
}
return $price;
}
add_filter('woocommerce_variable_sale_price_html', 'sale_prices_custom_labels_min_max', 20, 2);
function sale_prices_custom_labels_min_max( $price, $product) {
$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);
if ( $variation_min_reg_price != $variation_min_sale_price || $variation_max_reg_price != $variation_max_sale_price )
{
if($variation_min_reg_price == $variation_max_reg_price && $variation_min_sale_price == $variation_max_sale_price ){
$price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price($variation_max_reg_price) . '</del>
<ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price($variation_max_sale_price) . '</ins>';
}
elseif($variation_min_reg_price != $variation_max_reg_price && $variation_min_sale_price == $variation_max_sale_price )
{
$price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price($variation_min_reg_price) . '-' . woocommerce_price($variation_max_reg_price) . '</del>
<ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price($variation_max_sale_price) . '</ins>';
}
elseif($variation_min_reg_price == $variation_max_reg_price && $variation_min_sale_price != $variation_max_sale_price )
{
$price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price($variation_max_reg_price) . '</del>
<ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price($variation_min_sale_price) . '-' . woocommerce_price($variation_max_sale_price) . '</ins>';
}
else
{
$price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price($variation_min_reg_price) . '-' . woocommerce_price($variation_max_reg_price) . '</del>
<ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price($variation_min_sale_price) . '-' . woocommerce_price($variation_max_sale_price) . '</ins>';
}
}
return $price;
}
You can also replace the normal
<ins>
and<del>
html tags by something else and change or add some classes too (if is more convenient for you). At this point everithing is possible.
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
Related answers: Conditional custom output around products sale price and regular price
这篇关于在产品打折时为价格添加自定义文本标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在产品打折时为价格添加自定义文本标签
基础教程推荐
- 超薄框架REST服务两次获得输出 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01