Display Woocommerce product price with and without tax and tax amount

后端 未结 4 904
没有蜡笔的小新
没有蜡笔的小新 2020-12-31 13:00

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 t

4条回答
  •  醉梦人生
    2020-12-31 13:25

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

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

    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:

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

    (formatted)

    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

提交回复
热议问题