Display Woocommerce product price with and without tax and tax amount

本秂侑毒 提交于 2019-12-04 19:08:20

问题


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

I need to show separately the 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?


回答1:


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:

<?php echo wc_price( wc_get_price_including_tax( $product ) ); ?>

Prior to WooCommerce v3.0.0
You need to modify a template. Do not modify 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.

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

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

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




回答2:


Update 2018/2019 (for 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"

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

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

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


Original answer (before woocommerce 3):

Before check that your WooCommerce Tax general settings match with your needs.

As cale_b suggested, you need to copy from woocommerce the templates folder inside your active child theme or theme. Then rename it woocommerce. In this woocommerce templates folder you will find inside single-product subfolder the price.php template to edit related to pricing display in single product pages.

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

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

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




回答3:


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

  • Woocommerce: Tax tab: Display Prices in the Shop / Display Prices During Cart and Checkout


来源:https://stackoverflow.com/questions/37227730/display-woocommerce-product-price-with-and-without-tax-and-tax-amount

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!