Maciej Kravchyk
Maciej Kravchyk

Reputation: 16607

Check if product prices includes tax in Woocommerce 3+

How can you programmatically check if a product's price includes tax? I checked the WC_Product reference, but couldn't find anything like that.

Upvotes: 2

Views: 4569

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 253901

You will simply use the dedicated conditional function wc_prices_include_tax() in an IF statement:

if( wc_prices_include_tax() ) {
    // Price include tax
} else {
    // Price doesn't include tax
}

It will check if taxes are enabled in Woocommerce and if your product prices general setting are including tax or not.

For example wc_prices_include_tax() is used by wc_get_price_including_tax() a WC_Product price function (not a method), used herself in wc_get_price_to_display() price function when products prices need to be displayed including taxes in product pages…

If product prices need to be displayed excluding taxes in product pages, wc_get_price_to_display() will use wc_get_price_excluding_tax()

wc_get_price_to_display(), wc_get_price_including_tax() and wc_get_price_excluding_tax() have 2 arguments:

$product (mandatory) the WC_Product object
$args (optional) an array containing the product price and the quantity

Related: Display Woocommerce product price with and without tax and tax amount


On Cart, Checkout and Orders there is another general setting that allow you to display prices with or without taxes. You can use the following to check if prices are displayed with or without taxes:

if( get_option( 'woocommerce_tax_display_cart' ) ) {
    // Prices displayed including tax
}

Order items related:

Upvotes: 5

ArtisticPhoenix
ArtisticPhoenix

Reputation: 21661

Well product will not tell you if someone paid sales tax. For that you need something involving an order.

Something that has orders and products, something like this class:

WC_Order_Item_Product

For reference

https://docs.woocommerce.com/wc-apidocs/source-class-WC_Order_Item_Product.html#262-270

This has at least 3 relevant methods:

//Get subtotal tax.
WC_Order_Item_Product::get_subtotal_tax();

//Get total tax.
WC_Order_Item_Product::get_total_tax();

Get taxes.
WC_Order_Item_Product::get_taxes();

Note these are not "static" I just like the way it looks, and it's common on the PHP document page to do it this way :-p

Now how you get there from where you are, I don't know. Meaning how you get one of these mystical WC_Order_Item_Product objects.

Good Luck!

Upvotes: 0

Related Questions