Nur*_*man 9 php wordpress woocommerce price tax
我正在使用WooCommerce for WordPress,我列出的项目不含税.
我需要在产品页面上单独显示价格(不含税),税和PRICE +税(如结帐页面).
我找不到这样做的插件.
我怎样才能做到这一点?
ran*_*ame 15
WooCommerce v3.0.0及更高
版本从WooCommerce 3.0版开始, 不推荐使用函数woocommerce_price(),方法是get_price_including_tax().相反,您应该使用wc_get_price_including_tax:
<?php echo wc_price( wc_get_price_including_tax( $product ) ); ?>
Run Code Online (Sandbox Code Playgroud)
在WooCommerce v3.0.0之前
您需要修改模板. 不要修改核心WooCommerce模板,而是使用WooCommerce模板覆盖系统将其复制到您的主题.有关这方面的帮助,请参阅有关使用模板覆盖系统的WooCommerce文档.
在price.php模板中,您将在需要价格的位置添加此位代码,包括税(VAT):
<?php echo woocommerce_price( $product->get_price_including_tax() ); ?>
Run Code Online (Sandbox Code Playgroud)
注意:price.php您修改的模板应位于此处wp-content/themes/[your theme folder]/woocommerce/single-product/price.php
更新2018/2019 (适用于Woocommerce 3+)
显示不含税的价格+税额+含税价格(在单独的行上):
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>
Run Code Online (Sandbox Code Playgroud)
2)在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 ?>
Run Code Online (Sandbox Code Playgroud)
文档:
• 模板结构以及如何通过主题覆盖Woocommerce模板
• wc_get_price_including_tax()产品价格功能
• wc_get_price_excluding_tax()产品价格功能
• wc_price()格式化价格功能
• wc_get_price_to_display()产品价格功能
原始答案(在woocommerce 3之前):
在检查您的WooCommerce Tax常规设置是否符合您的需求之前.
正如cale_b建议的那样,您需要从woocommerce中复制templates活动子主题或主题中的文件夹.然后重命名它woocommerce.在此woocommerce模板文件夹中,您将在single-product子文件夹中找到price.php模板,以编辑与单个产品页面中的定价显示相关的模板.
在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>
Run Code Online (Sandbox Code Playgroud)
由于额外的价格未格式化,您可能需要使用一些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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32006 次 |
| 最近记录: |