-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UNPLUGGED-1204 | Added a way to show prices with the correct taxes in…
… the REST API (#172) * feat: Added a way to show prices with the correct taxes in the REST API * feat: included filters for variants prices * feat: included filters inside initialize_rest_endpoints * feat: Added fallback in case sale price has not been defined * feat: isolated price logic into a different class * feat: updated version to 2.0.5 * feat: changed tested up to version
- Loading branch information
1 parent
9e91acb
commit d2ce1fc
Showing
4 changed files
with
77 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
doofinder-for-woocommerce/includes/class-tax-prices-handler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Doofinder\WP; | ||
|
||
class Tax_Prices_Handler { | ||
|
||
public static function apply_correct_taxes_for_product_prices_in_rest_api(): void { | ||
add_filter( 'woocommerce_product_get_regular_price', array( __CLASS__, 'filter_woocommerce_prices_with_taxes' ), 99, 2 ); | ||
add_filter( 'woocommerce_product_get_sale_price', array( __CLASS__, 'filter_woocommerce_prices_with_taxes' ), 99, 2 ); | ||
add_filter( 'woocommerce_product_get_price', array( __CLASS__, 'filter_woocommerce_prices_with_taxes' ), 99, 2 ); | ||
add_filter( 'woocommerce_product_variation_get_regular_price', array( __CLASS__, 'filter_woocommerce_prices_with_taxes' ), 99, 2 ); | ||
add_filter( 'woocommerce_product_variation_get_sale_price', array( __CLASS__, 'filter_woocommerce_prices_with_taxes' ), 99, 2 ); | ||
add_filter( 'woocommerce_product_variation_get_price', array( __CLASS__, 'filter_woocommerce_prices_with_taxes' ), 99, 2 ); | ||
} | ||
|
||
public static function filter_woocommerce_prices_with_taxes( $price, $product ) { | ||
// Only affects the prices shown on the REST API requests | ||
if ( ! function_exists( 'WC' ) || ! WC()->is_rest_api_request() ) { | ||
return $price; | ||
} | ||
|
||
// Just in case the sale price has not been defined, we're going to use the price instead | ||
if ( empty( $price ) ) { | ||
return $product->price; | ||
} | ||
|
||
$raw_real_price = self::get_raw_real_price( $price, $product ); | ||
// Idea extracted directly from the original WC function wc_price() | ||
$args = array(); | ||
$args = apply_filters( | ||
'wc_price_args', | ||
wp_parse_args( | ||
$args, | ||
array( | ||
'ex_tax_label' => false, | ||
'currency' => '', | ||
'decimal_separator' => wc_get_price_decimal_separator(), | ||
'thousand_separator' => wc_get_price_thousand_separator(), | ||
'decimals' => wc_get_price_decimals(), | ||
'price_format' => get_woocommerce_price_format(), | ||
) | ||
) | ||
); | ||
$price = apply_filters( 'formatted_woocommerce_price', number_format( $raw_real_price, $args['decimals'], $args['decimal_separator'], $args['thousand_separator'] ), $price, $args['decimals'], $args['decimal_separator'], $args['thousand_separator'], $price ); | ||
if ( apply_filters( 'woocommerce_price_trim_zeros', false ) && $args['decimals'] > 0 ) { | ||
$price = wc_trim_zeros( $price ); | ||
} | ||
return $price; | ||
} | ||
|
||
private static function get_raw_real_price( $price, $product ) { | ||
$woocommerce_tax_display_shop = get_option( 'woocommerce_tax_display_shop', 'incl' ); | ||
return 'incl' === $woocommerce_tax_display_shop ? | ||
wc_get_price_including_tax( | ||
$product, | ||
array( | ||
'price' => $price, | ||
) | ||
) : | ||
wc_get_price_excluding_tax( | ||
$product, | ||
array( | ||
'price' => $price, | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
=== Doofinder WP & WooCommerce Search === | ||
Contributors: Doofinder | ||
Tags: search, autocomplete | ||
Version: 2.0.4 | ||
Version: 2.0.5 | ||
Requires at least: 4.1 | ||
Tested up to: 6.1 | ||
Tested up to: 6.2.2 | ||
Requires PHP: 5.6 | ||
Stable tag: trunk | ||
License: GPLv2 or later | ||
|
@@ -82,6 +82,9 @@ Just send your questions to <mailto:[email protected]> and we will try to an | |
|
||
== Changelog == | ||
|
||
= 2.0.5 = | ||
Bugfix: Prices reflect the correct taxes now | ||
|
||
= 2.0.4 = | ||
Minor bugfix | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters