WooCommerce单品页同时显示含VAT与不含VAT价格方案咨询
WooCommerce单品页同时显示含税/不含税价格解决方案(适配3.3.5版本)
嘿,我来帮你搞定这个价格显示的问题!针对你用的WooCommerce 3.3.5版本,我整理了两个可行的方案,你可以按顺序尝试:
方案一:通过钩子函数修改价格输出
这个方法不需要修改主题模板,直接把代码加到你当前主题的functions.php文件末尾(或者用自定义功能插件更安全,避免主题更新丢失修改):
add_filter('woocommerce_get_price_html', 'display_both_prices_with_tax', 100, 2); function display_both_prices_with_tax($price_html, $product) { // 仅在单品页生效,不影响其他页面价格显示 if ( ! is_product() ) { return $price_html; } // 获取不含VAT和含VAT的价格(适配3.3.5版本的方法) $price_excl_tax = wc_get_price_excluding_tax($product); $price_incl_tax = wc_get_price_including_tax($product); // 自定义价格显示格式,你可以根据需求调整文字和排版 $price_html = '<span class="price-excl-tax">不含VAT:' . wc_price($price_excl_tax) . '</span><br>'; $price_html .= '<span class="price-incl-tax">含VAT:' . wc_price($price_incl_tax) . '</span>'; return $price_html; }
添加后刷新单品页,就能看到两个价格同时展示了。如果样式不符合你的需求,还可以通过CSS调整price-excl-tax和price-incl-tax这两个类的样式。
方案二:修改主题的价格模板文件
如果方案一没生效,大概率是你的主题自定义了单品页的价格模板。你可以按以下步骤操作:
- 检查主题目录下是否有
woocommerce/single-product/price.php文件; - 如果没有,从WooCommerce插件的
templates/single-product/price.php复制一份到主题对应的目录里; - 替换文件内容为以下代码:
<?php /** * Single Product Price * * This template can be overridden by copying it to yourtheme/woocommerce/single-product/price.php. * * @see https://docs.woocommerce.com/document/template-structure/ * @author WooCommerce * @package WooCommerce/Templates * @version 3.0.0 */ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } global $product; // 获取不含税和含税价格 $price_excl_tax = wc_get_price_excluding_tax($product); $price_incl_tax = wc_get_price_including_tax($product); ?> <p class="price"> <span class="price-excl-tax">不含VAT:<?php echo wc_price($price_excl_tax); ?></span><br> <span class="price-incl-tax">含VAT:<?php echo wc_price($price_incl_tax); ?></span> </p>
保存后刷新页面就能看到效果了,记得修改前备份原文件,避免出错后无法恢复。
额外提醒
- 如果你用的是子主题,一定要把代码或模板文件放到子主题目录下,这样主题更新时不会丢失你的修改;
- 确保你已经在WooCommerce后台正确配置了VAT税率,否则价格计算会出现错误。
内容的提问来源于stack exchange,提问作者DrMTR
相关产品推荐
相关产品推荐

