[英]Display Woocommerce product price with and without tax and tax amount
我正在为 WordPress 使用 WooCommerce,我列出了不含税的项目。
我需要在产品页面(如结帐页面)上分别显示价格(不含税) 、税费和价格 + 税费。
我一直没能找到一个插件来做到这一点。
我怎样才能做到这一点?
WooCommerce v3.0.0 及更高版本
从 WooCommerce 3.0 版开始,函数woocommerce_price()已弃用,方法get_price_include_tax() 也是如此。 相反,您应该使用wc_get_price_including_tax :
<?php echo wc_price( wc_get_price_including_tax( $product ) ); ?>
在 WooCommerce v3.0.0 之前
您需要修改模板。 不要修改核心 WooCommerce 模板,而是使用 WooCommerce 模板覆盖系统将其复制到您的主题。 有关这方面的帮助,请参阅有关使用模板覆盖系统的 WooCommerce 文档。
在price.php
模板中,您将在需要包含税费 (VAT) 的价格的位置添加这段代码:
<?php echo woocommerce_price( $product->get_price_including_tax() ); ?>
注意:您修改的price.php
模板应该位于wp-content/themes/[your theme folder]/woocommerce/single-product/price.php
2018/2019 更新(适用于 Woocommerce 3+)
显示不含税价格 + 税额 + 含税价格(分行) :
首先阅读“如何通过您的主题覆盖 Woocommerce 模板”
1) 在single-product/price.php
模板文件(单个产品页面)上。
将代码替换为:
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $product;
// Get the prices
$price_excl_tax = wc_get_price_excluding_tax( $product ); // price without VAT
$price_incl_tax = wc_get_price_including_tax( $product ); // price with VAT
$tax_amount = $price_incl_tax - $price_excl_tax; // VAT amount
// Display the prices
?>
<p class="price-excl"><?php echo wc_price( $price_excl_tax ); ?></p>
<p class="tax-price"><?php echo wc_price( $tax_amount ); ?></p>
<p class="price-incl"><?php echo wc_price( $price_incl_tax ); ?></p>
2) On loop/price.php
模板文件(商店和存档页面) 。
将代码替换为:
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $product;
if ( $product->get_price_html() ) :
// Get the prices
$price_excl_tax = wc_get_price_excluding_tax( $product ); // price without VAT
$price_incl_tax = wc_get_price_including_tax( $product ); // price with VAT
$tax_amount = $price_incl_tax - $price_excl_tax; // VAT amount
// Display the prices
?>
<span class="price price-excl"><?php echo wc_price( $price_excl_tax ); ?></span><br>
<span class="price tax-price"><?php echo wc_price( $tax_amount ); ?></span><br>
<span class="price price-incl"><?php echo wc_price( $price_incl_tax ); ?></span>
<?php endif ?>
文档:
•模板结构以及如何通过您的主题覆盖 Woocommerce 模板
• wc_get_price_including_tax()
产品价格函数
• wc_get_price_excluding_tax()
产品价格函数
• wc_price()
格式化价格函数
• wc_get_price_to_display()
产品价格函数
原始答案(在 woocommerce 3 之前) :
在检查您的 WooCommerce Tax 一般设置是否符合您的需求之前。
正如cal_b建议的那样,您需要从 woocommerce 复制活动子主题或主题中的templates
文件夹。 然后将其重命名为woocommerce
。 在此woocommerce
模板文件夹中,您将在single-product
子文件夹中找到price.php模板,以编辑与单个产品页面中的定价显示相关的内容。
在global $product;
之后的single-product/price.php
模板文件中global $product;
,将代码替换为:
?>
<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<?php
$price_excl = $product->get_price_excluding_tax(); // price without VAT
$price_incl = $product->get_price_including_tax(); // price included VAT
$tax_amount = $price_incl - $price_excl; // VAT price amount
?>
<p class="price"><?php echo woocommerce_price( $price_excl ); /* without VAT */ ?></p> (formatted)
<p class="price-vat"><?php echo woocommerce_price( $tax_amount); /* VAT */ ?></p>
<p class="price-and-vat"><?php echo woocommerce_price( $price_incl); /* With VAT */ ?></p>
<meta itemprop="price" content="<?php echo esc_attr( $product->get_price() ); ?>" />
<meta itemprop="priceCurrency" content="<?php echo esc_attr( get_woocommerce_currency() ); ?>" />
<link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />
</div>
由于附加价格未格式化,您可能需要使用一些 woocommerce php 函数将一些其他元素与此附加价格混合,例如:
get_price_suffix( ) // Get the suffix to display after prices > 0.
$currency = esc_attr( get_woocommerce_currency( ) ) // Get the currency code.
get_woocommerce_currency_symbol( $currency ) // Get the currency symbol.
get_tax_class( ) // Returns the tax class.
get_tax_status( ) // Returns the tax status.
目前您不再需要更改模板。 您可以在 Woocommerce 设置中进行设置:
是的,您不必编辑模板来显示增值税。 转到 Woocommerce 设置 > 增值税 > 价格显示后缀。 您可以添加:{price_ exclude_tax} 以显示不含增值税。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.