Skip to content

Commit

Permalink
UNPLUGGED-1204 | Added a way to show prices with the correct taxes in…
Browse files Browse the repository at this point in the history
… 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
davidmolinacano authored Aug 4, 2023
1 parent 9e91acb commit d2ce1fc
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 deletions.
6 changes: 4 additions & 2 deletions doofinder-for-woocommerce/doofinder-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
67 changes: 67 additions & 0 deletions doofinder-for-woocommerce/includes/class-tax-prices-handler.php
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,
)
);
}
}
7 changes: 5 additions & 2 deletions doofinder-for-woocommerce/readme.txt
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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit d2ce1fc

Please sign in to comment.