Skip to content

Commit

Permalink
Added compatibility for Tiered Pricing Table for WooCommerce Premium
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulsky committed Oct 5, 2024
1 parent 32c7eb6 commit ee80f05
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 42 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog
All notable changes to the Wdevs Tax Switch plugin will be documented in this file.

## [1.1.1] - 2024-10-05
### Added
- Compatibility for Tiered Pricing Table for WooCommerce Premium


## [1.1.0] - 2024-10-04
### Added
- Compatibility for [Tiered Pricing Table for WooCommerce](https://wordpress.org/plugins/tier-pricing-table/) (Single product page)
Expand Down
5 changes: 4 additions & 1 deletion 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.0.1
Stable tag: 1.1.1
Requires PHP: 7.2
License: GPL-2.0+
License URI: http://www.gnu.org/licenses/gpl-2.0.txt
Expand Down Expand Up @@ -78,6 +78,9 @@ Some WooCommerce Blocks are not fully compatible with this plugin as they do not

== Changelog ==

= 1.1.1 =
* Added Compatibility for Tiered Pricing Table for WooCommerce Premium (single product page).

= 1.1.0 =
* Added compatibility for [Tiered Pricing Table for WooCommerce](https://wordpress.org/plugins/tier-pricing-table/) (single product page).
* Added compatibility for [WooCommerce Product Table](https://wordpress.org/plugins/wc-product-table-lite/)
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.0",
"version": "1.2.0",
"title": "Tax Switch for WooCommerce",
"category": "woocommerce",
"icon": "money-alt",
Expand Down
199 changes: 164 additions & 35 deletions block/src/includes/TieredPriceTableCompatibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import TaxSwitchHelper from './TaxSwitchHelper';

class TieredPriceTableCompatibility {
static initialize( originalTaxDisplay ) {
//tpt is using 250?
const vm = this;
setTimeout( function () {
vm.setPrices( originalTaxDisplay );
}, 300 );

this.registerWooCommerceTieredPriceTableEvents( originalTaxDisplay );
}

Expand All @@ -17,15 +23,18 @@ class TieredPriceTableCompatibility {
if (
settings.url.indexOf( 'wc-ajax=get_pricing_table' ) !== -1
) {
vm.setPrices( originalTaxDisplay );
setTimeout( function () {
vm.setPrices( originalTaxDisplay );
}, 10 );
}
} );
jQuery( '.tpt__tiered-pricing' ).on(

jQuery( document ).on(
'tiered_price_update',
function ( event, data ) {
// setTimeout( function () {
// setTimeout(function() {
vm.setFormattedPrices( originalTaxDisplay, data );
// }, 10 );
// }, 10);
}
);

Expand All @@ -42,32 +51,53 @@ class TieredPriceTableCompatibility {
}

static getMainElement() {
const wrapper = jQuery( '.tiered-pricing-wrapper' );
const wrapper = jQuery( '.single-product .tiered-pricing-wrapper' );
if ( ! wrapper.length ) {
return null;
}

const mainElement = wrapper.find( '[data-regular-price]' );
const mainElement = wrapper.first().find( '[data-regular-price]' );
if ( ! mainElement.length ) {
return null;
}

return mainElement;
return mainElement.first();
}

static getSinglePrices() {
static getSingleUnitElement() {
const mainElement = this.getMainElement();
if ( ! mainElement ) {
return null;
}

const singleUnitElement = mainElement.find(
'[data-tiered-quantity="1"]'
);
let singleUnitElement = mainElement
.find( '[data-tiered-quantity="1"]' )
.first();

if ( ! singleUnitElement.length ) {
return null;
let lowestQuantity = null;

mainElement.find( '[data-tiered-quantity]' ).each( function () {
let quantity = parseInt(
jQuery( this ).attr( 'data-tiered-quantity' ),
10
);

if ( ! lowestQuantity || quantity < lowestQuantity ) {
lowestQuantity = quantity;
singleUnitElement = jQuery( this );
}
} );

if ( ! singleUnitElement || ! singleUnitElement.length ) {
return null;
}
}

return singleUnitElement;
}

static getLowestPrices( singleUnitElement ) {
let priceIncludingVat = singleUnitElement.data(
'tiered-price-include-taxes'
);
Expand All @@ -93,14 +123,18 @@ class TieredPriceTableCompatibility {
if ( ! mainElement ) {
return;
}
const singleUnitElement = this.getSingleUnitElement();
if ( ! singleUnitElement ) {
return;
}
const lowestUnitPrices = this.getLowestPrices( singleUnitElement );

const singleUnitPrices = this.getSinglePrices();
if ( ! singleUnitPrices ) {
if ( ! lowestUnitPrices ) {
return;
}

const priceIncludingVat = singleUnitPrices[ 0 ];
const priceExcludingVat = singleUnitPrices[ 1 ];
const priceIncludingVat = lowestUnitPrices[ 0 ];
const priceExcludingVat = lowestUnitPrices[ 1 ];

const displayIncludingVat =
TaxSwitchHelper.displayIncludingVat( originalTaxDisplay );
Expand Down Expand Up @@ -139,10 +173,14 @@ class TieredPriceTableCompatibility {
}
} );

jQuery( '.qty' ).trigger( 'change' );
//tpt is using 300?
setTimeout( function () {
jQuery( '.qty' ).trigger( 'change' );
}, 350 );
}

static setFormattedPrices( originalTaxDisplay, wctptData ) {
const vm = this;
const wctptInstance = wctptData.__instance;
if ( ! wctptInstance ) {
return;
Expand All @@ -152,18 +190,23 @@ class TieredPriceTableCompatibility {
return;
}

const singleUnitPrices = this.getSinglePrices();
if ( ! singleUnitPrices ) {
const singleUnitElement = this.getSingleUnitElement();
if ( ! singleUnitElement ) {
return;
}
const lowestUnitPrices = this.getLowestPrices( singleUnitElement );
if ( ! lowestUnitPrices ) {
return;
}

const priceIncludingVat = singleUnitPrices[ 0 ];
const priceExcludingVat = singleUnitPrices[ 1 ];
const priceIncludingVat = lowestUnitPrices[ 0 ];
const priceExcludingVat = lowestUnitPrices[ 1 ];

const displayIncludingVat =
TaxSwitchHelper.displayIncludingVat( originalTaxDisplay );

const unitElements = mainElement.find( '[data-tiered-quantity]' );

unitElements.each( function ( index, element ) {
element = jQuery( element );
let tieredIncludingVat = element.data(
Expand All @@ -181,24 +224,16 @@ class TieredPriceTableCompatibility {
wctptInstance.formatting.formatPrice( tieredExcludingVat );

if ( element.find( '.wts-price-wrapper' ).length === 0 ) {
const html =
'<div class="wts-price-wrapper">' +
'<div class="wts-price-incl ' +
( displayIncludingVat ? 'wts-active' : 'wts-inactive' ) +
'">' +
tieredIncludingVatHTML +
'</div>' +
'<div class="wts-price-excl ' +
( ! displayIncludingVat ? 'wts-active' : 'wts-inactive' ) +
'">' +
tieredExcludingVatHTML +
'</div>' +
'</div>';
const html = vm.getWtsHtml(
displayIncludingVat,
tieredIncludingVatHTML,
tieredExcludingVatHTML
);
element.find( '.amount' ).replaceWith( html );
}

if ( element.hasClass( 'tiered-pricing--active' ) ) {
if ( element.attr( 'data-tiered-quantity' ) == 1 ) {
if ( element.is( singleUnitElement ) ) {
jQuery(
'.wts-price-incl .tiered-pricing-dynamic-price-wrapper'
).html( tieredIncludingVatHTML );
Expand All @@ -225,6 +260,100 @@ class TieredPriceTableCompatibility {
}
}
} );

const productId = wctptData.parentId;
let summaryTable;
if ( productId ) {
summaryTable = jQuery( '.tier-pricing-summary-table' ).filter(
`[data-product-id=${ productId }]`
);
} else {
summaryTable = jQuery( '.tier-pricing-summary-table' );
}

if ( summaryTable ) {
const summaryProductPriceHtml = vm.getWtsHtml(
displayIncludingVat,
wctptInstance.formatting.formatPrice(
wctptData.pricing.price_incl_tax,
false
),
wctptInstance.formatting.formatPrice(
wctptData.pricing.price_excl_tax,
false
),
true
);
summaryTable
.find( '[data-tier-pricing-table-summary-product-price]' )
.html( summaryProductPriceHtml );

const summaryTotalHtml = vm.getWtsHtml(
displayIncludingVat,
wctptInstance.formatting.formatPrice(
wctptData.pricing.price_incl_tax * wctptData.quantity,
false
),
wctptInstance.formatting.formatPrice(
wctptData.pricing.price_excl_tax * wctptData.quantity,
false
),
true
);
summaryTable
.find( '[data-tier-pricing-table-summary-total]' )
.html( summaryTotalHtml );
}
}

static getWtsHtml(
displayIncludingVat,
tieredIncludingVatHTML,
tieredExcludingVatHTML,
setText = false
) {
let includingTextHtml = '';
let excludingTextHtml = '';
if ( setText ) {
const includingTextElement = jQuery(
'.wts-price-wrapper .wts-price-incl .wts-vat-text'
).first();
if ( includingTextElement ) {
const excludingTextElement = jQuery(
'.wts-price-wrapper .wts-price-excl .wts-vat-text'
).first();
if ( excludingTextElement ) {
const spaceNode = jQuery(
document.createTextNode( ' ' )
).get( 0 ).nodeValue;
includingTextHtml =
spaceNode +
includingTextElement.clone().prop( 'outerHTML' );
excludingTextHtml =
spaceNode +
excludingTextElement.clone().prop( 'outerHTML' );
}
}
}

const html =
'<div class="wts-price-wrapper">' +
'<div class="wts-price-incl ' +
( displayIncludingVat ? 'wts-active' : 'wts-inactive' ) +
'">' +
tieredIncludingVatHTML +
'' +
includingTextHtml +
'</div>' +
'<div class="wts-price-excl ' +
( ! displayIncludingVat ? 'wts-active' : 'wts-inactive' ) +
'">' +
tieredExcludingVatHTML +
'' +
excludingTextHtml +
'</div>' +
'</div>';
return html;
}
}

Expand Down
8 changes: 5 additions & 3 deletions includes/class-wdevs-tax-switch-compatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ public function __construct( $plugin_name, $version ) {
}

public function enqueue_compatibility_scripts() {
if ( is_plugin_active( 'tier-pricing-table/tier-pricing-table.php' ) ) {
$wctpt_asset = require( plugin_dir_path( dirname( __FILE__ ) ) . 'build/woocommerce-tiered-price-table.asset.php' );
wp_enqueue_script( 'wdevs-tax-switch-woocommerce-tiered-price-table', plugin_dir_url( dirname( __FILE__ ) ) . 'build/woocommerce-tiered-price-table.js', $wctpt_asset['dependencies'], $wctpt_asset['version'] );
if ( is_plugin_active( 'tier-pricing-table/tier-pricing-table.php' ) || is_plugin_active( 'tier-pricing-table-premium/tier-pricing-table.php' ) ) {
if ( is_product() ) {
$wctpt_asset = require( plugin_dir_path( dirname( __FILE__ ) ) . 'build/woocommerce-tiered-price-table.asset.php' );
wp_enqueue_script( 'wdevs-tax-switch-woocommerce-tiered-price-table', plugin_dir_url( dirname( __FILE__ ) ) . 'build/woocommerce-tiered-price-table.js', $wctpt_asset['dependencies'], $wctpt_asset['version'] );
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions wdevs-tax-switch.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Plugin Name: Tax Switch for WooCommerce
* Plugin URI: https://wijnberg.dev
* Description: Let customers toggle between inclusive and exclusive VAT pricing in your WooCommerce store.
* Version: 1.1.0
* Version: 1.1.1
* Author: Wijnberg Developments
* Author URI: https://wijnberg.dev/
* License: GPL-2.0+
Expand All @@ -41,7 +41,7 @@
* Start at version 1.0.0 and use SemVer - https://semver.org
* Rename this for your plugin and update it as you release new versions.
*/
define( 'WDEVS_TAX_SWITCH_VERSION', '1.1.0' );
define( 'WDEVS_TAX_SWITCH_VERSION', '1.1.1' );

/**
* The code that runs during plugin activation.
Expand Down

0 comments on commit ee80f05

Please sign in to comment.