Skip to content

Commit

Permalink
Fix: Taxes not applied to the price when displayed with shortcode (#35)
Browse files Browse the repository at this point in the history
Fixes #34
  • Loading branch information
kkarpieszuk authored Jan 17, 2023
1 parent 6610aa1 commit c9d4cd2
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 27 deletions.
6 changes: 4 additions & 2 deletions app/PriorPrice/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Hooks {
*/
public function register_hooks(): void {

$taxes = new Taxes();

$history_storage = new HistoryStorage();

$settings_data = new SettingsData();
Expand All @@ -27,7 +29,7 @@ public function register_hooks(): void {
$migrations = new Migrations( $history_storage );
$migrations->register_hooks();

$prices = new Prices( $history_storage, new SettingsData() );
$prices = new Prices( $history_storage, new SettingsData(), $taxes );
$prices->register_hooks();

$updates = new ProductUpdates( $history_storage );
Expand All @@ -36,7 +38,7 @@ public function register_hooks(): void {
$admin_assets = new AdminAssets();
$admin_assets->register_hooks();

$shortcode = new Shortcode( $history_storage );
$shortcode = new Shortcode( $history_storage, $taxes );
$shortcode->register_hooks();

$marketing = new Marketing();
Expand Down
37 changes: 18 additions & 19 deletions app/PriorPrice/Prices.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,26 @@ class Prices {
*/
private $settings_data;

public function __construct( HistoryStorage $history_storage, SettingsData $settings_data ) {
/**
* @var \PriorPrice\Taxes
*/
private $taxes;

/**
* Constructor.
*
* @since 1.0
* @since 1.6.2 uses Taxes class.
*
* @param \PriorPrice\HistoryStorage $history_storage Prices object.
* @param \PriorPrice\SettingsData $settings_data Settings data object.
* @param \PriorPrice\Taxes $taxes Taxes object.
*/
public function __construct( HistoryStorage $history_storage, SettingsData $settings_data, Taxes $taxes ) {

$this->history_storage = $history_storage;
$this->settings_data = $settings_data;
$this->taxes = $taxes;
}

/**
Expand Down Expand Up @@ -76,7 +92,7 @@ public function lowest_price_html( \WC_Product $wc_product ): string {
$lowest = $this->history_storage->get_minimal( $wc_product->get_id(), $days_number );
}

$lowest = $this->apply_taxes( $lowest, $wc_product );
$lowest = $this->taxes->apply_taxes( $lowest, $wc_product );

if ( (float) $lowest <= 0 ) {
return '';
Expand Down Expand Up @@ -127,23 +143,6 @@ private function is_not_correct_when( \WC_Product $wc_product ) : bool {
return false;
}

/**
* Apply taxes to the price.
*
* @since 1.4
*
* @param float $price
* @param \WC_Product $wc_product
*
* @return float
*/
private function apply_taxes( float $price, \WC_Product $wc_product ) : float {

return 'incl' === get_option( 'woocommerce_tax_display_shop' ) ?
(float) wc_get_price_including_tax( $wc_product, [ 'price' => $price ] ) :
(float) wc_get_price_excluding_tax( $wc_product, [ 'price' => $price ] );
}

/**
* Check if the product is the main product on the page.
*
Expand Down
16 changes: 15 additions & 1 deletion app/PriorPrice/Shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@ class Shortcode {
*/
private $history_storage;

/**
* @var \PriorPrice\Taxes
*/
private $taxes;

/**
* Constructor.
*
* @since 1.2
* @since 1.6.2 uses Taxes class.
*
* @param \PriorPrice\HistoryStorage $history_storage Prices object.
*/
public function __construct( HistoryStorage $history_storage ) {
public function __construct( HistoryStorage $history_storage, Taxes $taxes ) {

$this->history_storage = $history_storage;
$this->taxes = $taxes;
}

public function register_hooks() : void {
Expand Down Expand Up @@ -74,7 +81,14 @@ public function shortcode_callback( $atts ): string {
return '';
}

$product = wc_get_product( $id );

if ( ! $product ) {
return '';
}

$lowest = $this->history_storage->get_minimal( $id );
$lowest = $this->taxes->apply_taxes( $lowest, $product );
$lowest = (bool) $atts['show_currency'] ? sprintf( get_woocommerce_price_format(), get_woocommerce_currency_symbol(), $lowest ) : $lowest;

return sprintf( '<div class="wc-price-history-shortcode">%s</div>', $lowest );
Expand Down
23 changes: 23 additions & 0 deletions app/PriorPrice/Taxes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace PriorPrice;

class Taxes {

/**
* Apply taxes to the price.
*
* @since 1.6.2
*
* @param float $price
* @param \WC_Product $wc_product
*
* @return float
*/
public function apply_taxes( float $price, \WC_Product $wc_product ) : float {

return 'incl' === get_option( 'woocommerce_tax_display_shop' ) ?
(float) wc_get_price_including_tax( $wc_product, [ 'price' => $price ] ) :
(float) wc_get_price_excluding_tax( $wc_product, [ 'price' => $price ] );
}
}
6 changes: 3 additions & 3 deletions languages/wc-price-history.pot
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
# This file is distributed under the same license as the WC Price History plugin.
msgid ""
msgstr ""
"Project-Id-Version: WC Price History 1.5\n"
"Project-Id-Version: WC Price History 1.6.2\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/wc-price-history\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2023-01-14T14:13:25+01:00\n"
"POT-Creation-Date: 2023-01-17T14:01:36+01:00\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"X-Generator: WP-CLI 2.6.0\n"
"X-Domain: wc-price-history\n"
Expand All @@ -31,7 +31,7 @@ msgid "Konrad Karpieszuk"
msgstr ""

#. translators: %d product id, %s link to product edit screen.
#: app/PriorPrice/HistoryStorage.php:67
#: app/PriorPrice/HistoryStorage.php:69
msgid "Product #%1$d is on sale but has no sale start date. Please edit this product and set starting date for sale: %2$s"
msgstr ""

Expand Down
2 changes: 1 addition & 1 deletion plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: WC Price History
* Description: Track WooCommerce© Products prior prices history and display the lowest price in the last 30 days. This plugin allows your WC shop to be compliant with European Commission Directive 98/6/EC Article 6a which specifies price reduction announcement policy.
* Author: Konrad Karpieszuk
* Version: 1.6.1
* Version: 1.6.2
* Text Domain: wc-price-history
* Domain Path: /languages/
* Requires at least: 5.8
Expand Down
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Tags: WooCommerce, prices, history, prior, omnibus, european, 30days
Requires at least: 5.8
Tested up to: 6.1.1
Requires PHP: 7.2
Stable tag: 1.6.1
Stable tag: 1.6.2
License: MIT License
License URI: https://mit-license.org/
Donate link: https://buycoffee.to/wpzlecenia
Expand Down Expand Up @@ -93,6 +93,9 @@ Please submit the [GitHub issue](https://github.com/kkarpieszuk/wc-price-history

== Changelog ==

= 1.6.2 =
* Fix: Taxes not applied to the price when displayed with shortcode (#34).

= 1.6.1 =
* Fixed issue with timezones offsets when saving history

Expand Down

0 comments on commit c9d4cd2

Please sign in to comment.