php 显示含税和不含税的 Woocommerce 产品价格和税额

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37227730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:06:58  来源:igfitidea点击:

Display Woocommerce product price with and without tax and tax amount

phpwordpresswoocommercepricetax

提问by Nuri Akman

I am using WooCommerce for WordPress and I'm listing items excluding Tax.

我正在为 WordPress 使用 WooCommerce,我列出了不含税的项目。

I need to show separatelythe Price (without tax), the Tax and the PRICE + Tax on the product page (like in checkout page).

我需要在产品页面(如结帐页面)分别显示价格(不含税)、税费和价格 + 税费。

I have not been able to find a plugin that does this.

我一直没能找到一个插件来做到这一点。

How can I do this?

我怎样才能做到这一点?

回答by random_user_name

WooCommerce v3.0.0 and Later
As of WooCommerce version 3.0, the function woocommerce_price()is deprecated, as is the method get_price_including_tax(). Instead, you should use wc_get_price_including_tax:

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 ) ); ?>

Prior to WooCommerce v3.0.0
You need to modify a template. Do notmodify the core WooCommerce template, but rather make a copy of it to your theme, using the WooCommerce template override system. For help with that, refer to the WooCommerce docs on using the template override system.

在 WooCommerce v3.0.0 之前,
您需要修改一个模板。 不要修改核心 WooCommerce 模板,而是使用 WooCommerce 模板覆盖系统将其复制到您的主题。有关这方面的帮助,请参阅有关使用模板覆盖系统的 WooCommerce 文档。

In the price.phptemplate, you will add this bit of code where you want the price, including tax (VAT):

price.php模板中,您将在需要包含税费 (VAT) 的价格的位置添加这段代码:

<?php echo woocommerce_price( $product->get_price_including_tax() ); ?>

Note: the price.phptemplate that you modify should be located here in wp-content/themes/[your theme folder]/woocommerce/single-product/price.php

注意:price.php您修改的模板应位于此处wp-content/themes/[your theme folder]/woocommerce/single-product/price.php

回答by LoicTheAztec

Update 2018/2019(for Woocommerce 3+)

2018/2019 更新(适用于 Woocommerce 3+)

To display price without tax + tax amount + price including tax (on separated lines):

显示不含税价格 + 税额 + 含税价格(分行)

First read "How to override Woocommerce templates via your theme"

首先阅读“如何通过您的主题覆盖 Woocommerce 模板”

1) On single-product/price.phptemplate file(single product pages).

1) 在single-product/price.php模板文件(单个产品页面)上

Replace the code with:

将代码替换为:

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.phptemplate file(Shop and archive pages).

2) 在loop/price.php模板文件(商店和存档页面)上

Replace the code with:

将代码替换为:

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 ?>

Documentation:
? Template structure and how to override Woocommerce templates via your theme
? wc_get_price_including_tax()product price function
? wc_get_price_excluding_tax()product price function
? wc_price()formatting price function
? wc_get_price_to_display()product price function

文档:
? 模板结构以及如何通过您的主题覆盖 Woocommerce 模板
wc_get_price_including_tax()产品价格函数
? wc_get_price_excluding_tax()产品价格函数
? wc_price()格式化价格函数
wc_get_price_to_display()产品价格函数



Original answer (before woocommerce 3):

原始答案(在 woocommerce 3 之前)

Before check that your WooCommerce Tax general settingsmatch with your needs.

在检查您的 WooCommerce Tax 一般设置是否符合您的需求之前。

As cale_bsuggested, you need to copy from woocommerce the templatesfolder inside your active child theme or theme. Then rename it woocommerce. In this woocommercetemplates folder you will find inside single-productsubfolder the price.phptemplate to edit related to pricing display in single product pages.

正如cal_b建议的那样,您需要从 woocommerce 复制templates活动子主题或主题内的文件夹。然后重命名它woocommerce。在此woocommerce模板文件夹中,您将在single-product子文件夹中找到price.php模板,以便编辑与单个产品页面中的定价显示相关的内容。

In single-product/price.phptemplate file just after global $product;, replace the code with:

在 之后的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>

Because the additional prices are unformatted, you may need to mix some other elements with this additionals prices using some woocommerce php functions like:

由于附加价格未格式化,您可能需要使用一些 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.

Reference: WooCommerce WC_Product class

参考:WooCommerce WC_Product 类

回答by SPRBRN

At the moment you don't need to change a template anymore. You can set this in the Woocommerce settings:

目前您不再需要更改模板。您可以在 Woocommerce 设置中进行设置:

  • Woocommerce: Tax tab: Display Prices in the Shop / Display Prices During Cart and Checkout
  • Woocommerce:税收选项卡:在商店中显示价格/在购物车和结帐期间显示价格

回答by Mike Nash

Yes, you don't have to edit templates to show VAT. Go to Woocommerce settings > VAT > Price Display suffix. You can add: {price_excluding_tax} to show without VAT.

是的,您不必编辑模板来显示增值税。转到 Woocommerce 设置 > 增值税 > 价格显示后缀。您可以添加:{price_ exclude_tax} 以显示不含增值税。