From d2ce1fc68052c77e9bb9cd013d67d2ccf3b4e664 Mon Sep 17 00:00:00 2001 From: David Molina Cano <128705267+davidmolinacano@users.noreply.github.com> Date: Fri, 4 Aug 2023 13:46:58 +0200 Subject: [PATCH] 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 --- .../doofinder-for-woocommerce.php | 6 +- .../includes/class-tax-prices-handler.php | 67 +++++++++++++++++++ doofinder-for-woocommerce/readme.txt | 7 +- package.json | 2 +- 4 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 doofinder-for-woocommerce/includes/class-tax-prices-handler.php diff --git a/doofinder-for-woocommerce/doofinder-for-woocommerce.php b/doofinder-for-woocommerce/doofinder-for-woocommerce.php index af639462..99f98e37 100644 --- a/doofinder-for-woocommerce/doofinder-for-woocommerce.php +++ b/doofinder-for-woocommerce/doofinder-for-woocommerce.php @@ -4,7 +4,7 @@ * Plugin Name: Doofinder WP & WooCommerce Search * License: GPLv2 or later * License URI: http://www.gnu.org/licenses/gpl-2.0.html - * Version: 2.0.4 + * Version: 2.0.5 * Author: Doofinder * Description: Integrate Doofinder Search in your WordPress site or WooCommerce shop. * @@ -16,6 +16,7 @@ use WP_REST_Response; use Doofinder\WP\Multilanguage\Multilanguage; use Doofinder\WP\Admin_Notices; +use Doofinder\WP\Tax_Prices_Handler; defined('ABSPATH') or die; @@ -34,7 +35,7 @@ class Doofinder_For_WordPress * * @var string */ - public static $version = '2.0.4'; + public static $version = '2.0.5'; /** * The only instance of Doofinder_For_WordPress @@ -344,6 +345,7 @@ public static function initialize_rest_endpoints() { add_action('rest_api_init', function () { Config::register(); + Tax_Prices_Handler::apply_correct_taxes_for_product_prices_in_rest_api(); register_rest_route('doofinder/v1', '/index-status', array( 'methods' => 'POST', 'callback' => function (\WP_REST_Request $request) { diff --git a/doofinder-for-woocommerce/includes/class-tax-prices-handler.php b/doofinder-for-woocommerce/includes/class-tax-prices-handler.php new file mode 100644 index 00000000..90805ab8 --- /dev/null +++ b/doofinder-for-woocommerce/includes/class-tax-prices-handler.php @@ -0,0 +1,67 @@ +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, + ) + ); + } +} diff --git a/doofinder-for-woocommerce/readme.txt b/doofinder-for-woocommerce/readme.txt index 89c125d8..6e645ffa 100644 --- a/doofinder-for-woocommerce/readme.txt +++ b/doofinder-for-woocommerce/readme.txt @@ -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 and we will try to an == Changelog == += 2.0.5 = +Bugfix: Prices reflect the correct taxes now + = 2.0.4 = Minor bugfix diff --git a/package.json b/package.json index e1a89a06..a4c08646 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "doofinder-woocommerce", - "version": "2.0.4", + "version": "2.0.5", "description": "Integrate Doofinder in your WooCommerce site with (almost) no effort.", "main": "index.js", "scripts": {