Skip to content

Commit

Permalink
Added compatibility for Product Add-Ons for WooCommerce
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulsky committed Nov 7, 2024
1 parent 66b170f commit 0b2f317
Show file tree
Hide file tree
Showing 16 changed files with 679 additions and 208 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog
All notable changes to the Tax Switch for Woocommerce plugin will be documented in this file.
All notable changes to the Tax Switch for WooCommerce plugin will be documented in this file.

## [1.1.11] - 2024-11-06
### Added
- Compatibility for Product Add-Ons for WooCommerce

## [1.1.10] - 2024-11-05
### Added
Expand Down
23 changes: 20 additions & 3 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: wijnbergdevelopments
Tags: woocommerce, tax, vat
Requires at least: 5.0
Tested up to: 6.6
Stable tag: 1.1.10
Stable tag: 1.1.11
Requires PHP: 7.2
License: GPL-2.0+
License URI: http://www.gnu.org/licenses/gpl-2.0.txt
Expand All @@ -13,16 +13,30 @@ Let customers toggle between inclusive and exclusive VAT pricing in your WooComm
== Description ==
Tax Switch for WooCommerce enhances your WooCommerce store by allowing users to toggle between displaying prices including or excluding VAT. This plugin adds a customizable switch component and provides a flexible way to display both price versions.

Key features:
=== Key features ===

* Display customizable switches where you want
* Gutenberg block support
* Shortcode for easy integration
* Flexible display options for prices with and without VAT
* Saves the state of the switch for the user
* Remembers the user's preference for future visits

For more information about this plugin, please visit the [plugin page](https://products.wijnberg.dev/product/wordpress/plugins/tax-switch-for-woocommerce/).

=== Compatibility ===

This plugin integrates with WooCommerce's standard filters and actions for price display and calculation. While most plugins work out of the box, some third-party plugins use custom price building methods that require specific compatibility integrations. The following plugins have been tested and confirmed compatible:

* WooCommerce Product Table
* Tiered Pricing Table for WooCommerce (+ Premium)
* Measurement Price Calculator for WooCommerce
* Discount Rules for WooCommerce
* YITH WooCommerce Product Add-Ons (+ & Extra Options Premium)
* JetEngine Listing Grid (Elementor)
* Product Add-Ons for WooCommerce

If you encounter any compatibility issues with other plugins not listed above, please let us know. Your feedback helps us improve the plugin and extend compatibility to more third-party solutions.

= Requirements =

* WooCommerce plugin installed and activated
Expand Down Expand Up @@ -77,6 +91,9 @@ You can also use this shortcode in your theme files with the do_shortcode() func
Some WooCommerce Blocks are not fully compatible with this plugin as they do not use standard WooCommerce filters for price display. This is a known limitation of WooCommerce Blocks and not specific to this plugin. You can fix this by using WooCommerce shortcodes instead of the WooCommerce Blocks.

== Changelog ==
= 1.1.11 =
* Added compatibility for Product Add-Ons for WooCommerce

= 1.1.10 =
* Added compatibility for JetEngine Listing Grid 'infinity scroll'

Expand Down
2 changes: 1 addition & 1 deletion block/src/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "wdevs/tax-switch",
"version": "1.1.10",
"version": "1.1.11",
"title": "Tax Switch for WooCommerce",
"category": "woocommerce",
"icon": "money-alt",
Expand Down
3 changes: 2 additions & 1 deletion block/src/compatibility/TieredPriceTable.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import jQuery from 'jquery';
import TaxSwitchHelper from '../includes/TaxSwitchHelper';
import TaxSwitchElementBuilder from '../includes/TaxSwitchElementBuilder';

class TieredPriceTable {
constructor( originalTaxDisplay ) {
Expand Down Expand Up @@ -234,7 +235,7 @@ class TieredPriceTable {

if ( setText ) {
if ( ! vm.vatTexts ) {
vm.vatTexts = TaxSwitchHelper.getVatTexts();
vm.vatTexts = TaxSwitchElementBuilder.getVatTexts();
}

if ( vm.vatTexts ) {
Expand Down
93 changes: 93 additions & 0 deletions block/src/compatibility/WoocommerceProductAddons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import jQuery from 'jquery';
import TaxSwitchHelper from '../includes/TaxSwitchHelper';
import TaxSwitchElementBuilder from '../includes/TaxSwitchElementBuilder';

class WoocommerceProductAddOns {
constructor( originalTaxDisplay, baseTaxRate ) {
this.originalTaxDisplay = originalTaxDisplay;
this.taxRate = baseTaxRate;
this.taxSwitchElementBuilder = new TaxSwitchElementBuilder(
this.originalTaxDisplay
);
}

init() {
const vm = this;
vm.registerWooCommerceEvents();
}

registerWooCommerceEvents() {
// Check if accounting en woocommerce_addons_params are available
//from woocommerce-product-addons/assets/js/frontend/addons.js
if (
typeof window.accounting === 'undefined' ||
typeof window.accounting.unformat === 'undefined' ||
typeof window.woocommerce_addons_params === 'undefined'
) {
return;
}

const vm = this;

jQuery( 'form.cart' ).on( 'updated_addons', function () {
const $amounts = jQuery( this ).find(
'.product-addon-totals .amount'
);
const displayIncludingVat = TaxSwitchHelper.displayIncludingVat(
this.originalTaxDisplay
);
let price,
priceText,
alternatePrice,
alternatePriceText,
template,
$element;

$amounts.each( function () {
priceText = jQuery( this ).text();
price = window.accounting.unformat(
priceText,
window.woocommerce_addons_params.currency_format_decimal_sep
);
if ( price > 0 ) {
alternatePrice = TaxSwitchHelper.calculateAlternatePrice(
price,
vm.originalTaxDisplay,
vm.taxRate
);
alternatePriceText = window.accounting.formatMoney(
alternatePrice,
{
symbol: window.woocommerce_addons_params
.currency_format_symbol,
decimal:
window.woocommerce_addons_params
.currency_format_decimal_sep,
thousand:
window.woocommerce_addons_params
.currency_format_thousand_sep,
precision:
window.woocommerce_addons_params
.currency_format_num_decimals,
format: window.woocommerce_addons_params
.currency_format,
}
);

template = vm.taxSwitchElementBuilder.build(
displayIncludingVat,
priceText,
alternatePriceText,
null
);
$element = jQuery( this ).parent();
$element.html( template );
}
} );

TaxSwitchHelper.setPriceClasses( vm.originalTaxDisplay );
} );
}
}

export default WoocommerceProductAddOns;
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import jQuery from 'jquery';
import TaxSwitchHelper from '../includes/TaxSwitchHelper';
import TaxSwitchElementBuilder from '../includes/TaxSwitchElementBuilder';

class ProductAddOns {
class YithProductAddOns {
constructor( originalTaxDisplay, baseTaxRate ) {
this.originalTaxDisplay = originalTaxDisplay;
this.taxRate = baseTaxRate;
Expand All @@ -17,6 +18,9 @@ class ProductAddOns {
).clone();
}
this.vatTexts = null;
this.taxSwitchElementBuilder = new TaxSwitchElementBuilder(
this.originalTaxDisplay
);
}

init() {
Expand Down Expand Up @@ -111,7 +115,11 @@ class ProductAddOns {
if ( ! isNaN( parseFloat( price ) ) ) {
// Calculate alternate price
const alternatePrice =
this.calculateAlternatePrice( price );
TaxSwitchHelper.calculateAlternatePrice(
price,
this.originalTaxDisplay,
this.taxRate
);

// Format prices
const formattedOriginal = window.floatToWcPrice( price );
Expand Down Expand Up @@ -154,7 +162,11 @@ class ProductAddOns {
if ( ! price ) return;

// Calculate alternate price
const alternatePrice = this.calculateAlternatePrice( price );
const alternatePrice = TaxSwitchHelper.calculateAlternatePrice(
price,
this.originalTaxDisplay,
this.taxRate
);

// Format prices using YITH WAPO's formatting function
const formattedOriginal = window.floatToWcPrice( price );
Expand All @@ -167,116 +179,35 @@ class ProductAddOns {
);
}

calculateAlternatePrice( price ) {
// Guard clauses
if ( ! price || price <= 0 || ! this.taxRate ) {
return price;
}

const displayIncludingVat = this.originalTaxDisplay === 'incl';
const taxMultiplier = 1 + this.taxRate / 100;

let alternatePrice;
if ( displayIncludingVat ) {
alternatePrice = price / taxMultiplier;
} else {
alternatePrice = price * taxMultiplier;
}

return Number( alternatePrice.toFixed( 2 ) );
}

replacePriceDisplay(
$element,
originalPrice,
alternatePrice,
setText = false
) {
const vm = this;
const displayIncludingVat = TaxSwitchHelper.displayIncludingVat(
this.originalTaxDisplay
);

const prices = this.getPricesBasedOnTaxDisplay(
originalPrice,
alternatePrice
);
const includingPrice = prices.including;
const excludingPrice = prices.excluding;

function getVisibilityClass( isVisible ) {
return isVisible ? 'wts-active' : 'wts-inactive';
}

function createPriceElement( price, isIncludingVat ) {
const visibilityClass = getVisibilityClass(
isIncludingVat === displayIncludingVat
);
const priceType = isIncludingVat ? 'incl' : 'excl';

return `
<span class="wts-price-${ priceType } ${ visibilityClass }">
${ price }
</span>
`;
}

let template = `
<span class="wts-price-container">
<span class="wts-price-wrapper">
${ createPriceElement( includingPrice, true ) }
${ createPriceElement( excludingPrice, false ) }
</span>
`;

let texts = null;
if ( setText ) {
if ( ! vm.vatTexts ) {
vm.vatTexts = TaxSwitchHelper.getVatTexts(
this.initialPriceElement
vm.vatTexts = TaxSwitchElementBuilder.getVatTexts(
vm.initialPriceElement
);
}

if ( vm.vatTexts ) {
function createTextElement( text, isIncludingVat ) {
const visibilityClass = getVisibilityClass(
isIncludingVat === displayIncludingVat
);
const priceType = isIncludingVat ? 'incl' : 'excl';

return `
<span class="wts-price-${ priceType } ${ visibilityClass }">
${ text }
</span>
`;
}

template += `
<span class="wts-price-wrapper">
${ createTextElement( vm.vatTexts.including, true ) }
${ createTextElement( vm.vatTexts.excluding, false ) }
</span>
`;
}
}

template += '</span>';

$element.html( template.trim() );
}

getPricesBasedOnTaxDisplay( originalPrice, alternatePrice ) {
if ( this.originalTaxDisplay === 'incl' ) {
return {
including: originalPrice,
excluding: alternatePrice,
};
texts = vm.vatTexts;
}
const displayIncludingVat = TaxSwitchHelper.displayIncludingVat(
this.originalTaxDisplay
);
const template = vm.taxSwitchElementBuilder.build(
displayIncludingVat,
originalPrice,
alternatePrice,
texts
);

return {
including: alternatePrice,
excluding: originalPrice,
};
$element.html( template );
}
}

export default ProductAddOns;
export default YithProductAddOns;
Loading

0 comments on commit 0b2f317

Please sign in to comment.