From a3f97e8278d6e13bb8a39decd7586911a7e88765 Mon Sep 17 00:00:00 2001 From: Mahyar Rezghi Date: Sun, 30 Nov 2025 13:04:22 +0330 Subject: [PATCH 1/2] feat(tax): add universal tax fallback feature - Add enable_universal_tax setting toggle - Add universal_tax_rate setting (0-100%) - Implement apply_universal_tax_rate filter - Update translations for new strings - Apply universal rate when no country-specific rates match --- inc/tax/class-tax.php | 69 +++++++++++++++++++++++++++++++++++++ lang/ultimate-multisite.pot | 62 ++++++++++++++++++++++----------- 2 files changed, 110 insertions(+), 21 deletions(-) diff --git a/inc/tax/class-tax.php b/inc/tax/class-tax.php index 90eaf4ab4..62edfa8f1 100644 --- a/inc/tax/class-tax.php +++ b/inc/tax/class-tax.php @@ -32,6 +32,8 @@ public function init(): void { add_action('wu_page_wp-ultimo-settings_load', [$this, 'add_sidebar_widget']); + add_filter('wu_cart_applicable_tax_rates', [$this, 'apply_universal_tax_rate'], 10, 4); + if ($this->is_enabled()) { add_action('wp_ultimo_admin_pages', [$this, 'add_admin_page']); @@ -90,6 +92,42 @@ function () { } } + /** + * Applies universal tax rate when no specific rates are found. + * + * @since 2.0.0 + * @param array $rates Tax rates. + * @param string $country Country code. + * @param string $tax_category Tax category. + * @param object $cart Cart object. + * @return array + */ + public function apply_universal_tax_rate(array $rates, string $country, string $tax_category, $cart): array { + + if (empty($rates) && wu_should_collect_taxes() && wu_get_setting('enable_universal_tax', false)) { + $universal_rate = wu_get_setting('universal_tax_rate', 10); + + $rates = [ + [ + 'id' => 'universal-' . uniqid(), + 'title' => __('Universal Tax', 'ultimate-multisite'), + 'country' => '*', + 'state' => '', + 'city' => '', + 'tax_type' => 'percentage', + 'tax_amount' => $universal_rate, + 'tax_rate' => $universal_rate, + 'priority' => 1, + 'compound' => false, + 'type' => 'regular', + 'rate' => $universal_rate, + ] + ]; + } + + return $rates; + } + /** * Register tax settings. * @@ -132,6 +170,37 @@ public function add_settings(): void { ], ] ); + + wu_register_settings_field( + 'taxes', + 'enable_universal_tax', + [ + 'title' => __('Enable Universal Tax', 'ultimate-multisite'), + 'desc' => __('Enable a fallback universal tax rate when no country-specific rates match.', 'ultimate-multisite'), + 'type' => 'toggle', + 'default' => 0, + 'require' => [ + 'enable_taxes' => 1, + ], + ] + ); + + wu_register_settings_field( + 'taxes', + 'universal_tax_rate', + [ + 'title' => __('Universal Tax Rate (%)', 'ultimate-multisite'), + 'desc' => __('Tax rate applied when no country-specific rate matches.', 'ultimate-multisite'), + 'type' => 'number', + 'default' => 10, + 'min' => 0, + 'max' => 100, + 'require' => [ + 'enable_taxes' => 1, + 'enable_universal_tax' => 1, + ], + ] + ); } /** diff --git a/lang/ultimate-multisite.pot b/lang/ultimate-multisite.pot index 467e8cc84..024e7bbd6 100644 --- a/lang/ultimate-multisite.pot +++ b/lang/ultimate-multisite.pot @@ -9,7 +9,7 @@ msgstr "" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -"POT-Creation-Date: 2025-11-21T19:45:32+00:00\n" +"POT-Creation-Date: 2025-11-30T09:27:56+00:00\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "X-Generator: WP-CLI 2.12.0\n" "X-Domain: ultimate-multisite\n" @@ -3631,7 +3631,7 @@ msgid "Tax description. This is shown on invoices to end customers." msgstr "" #: inc/admin-pages/class-payment-edit-admin-page.php:802 -#: inc/tax/class-tax.php:204 +#: inc/tax/class-tax.php:273 msgid "Tax Rate" msgstr "" @@ -4126,8 +4126,8 @@ msgstr "" #: inc/list-tables/class-line-item-list-table.php:216 #: inc/tax/class-dashboard-taxes-tab.php:63 #: inc/tax/class-dashboard-taxes-tab.php:151 -#: inc/tax/class-tax.php:104 -#: inc/tax/class-tax.php:105 +#: inc/tax/class-tax.php:142 +#: inc/tax/class-tax.php:143 #: views/checkout/templates/order-summary/simple.php:188 msgid "Taxes" msgstr "" @@ -4937,7 +4937,7 @@ msgstr "" #: inc/admin-pages/class-tax-rates-admin-page.php:75 #: inc/admin-pages/class-tax-rates-admin-page.php:86 #: inc/admin-pages/class-tax-rates-admin-page.php:97 -#: inc/tax/class-tax.php:148 +#: inc/tax/class-tax.php:217 #: views/taxes/list.php:12 msgid "Tax Rates" msgstr "" @@ -18472,60 +18472,80 @@ msgstr "" msgid "Taxes Collected" msgstr "" -#: inc/tax/class-tax.php:115 +#: inc/tax/class-tax.php:113 +msgid "Universal Tax" +msgstr "" + +#: inc/tax/class-tax.php:153 msgid "Enable Taxes" msgstr "" -#: inc/tax/class-tax.php:116 +#: inc/tax/class-tax.php:154 msgid "Enable this option to be able to collect sales taxes on your network payments." msgstr "" -#: inc/tax/class-tax.php:126 +#: inc/tax/class-tax.php:164 msgid "Inclusive Tax" msgstr "" -#: inc/tax/class-tax.php:127 +#: inc/tax/class-tax.php:165 msgid "Enable this option if your prices include taxes. In that case, Ultimate Multisite will calculate the included tax instead of adding taxes to the price." msgstr "" -#: inc/tax/class-tax.php:189 +#: inc/tax/class-tax.php:178 +msgid "Enable Universal Tax" +msgstr "" + +#: inc/tax/class-tax.php:179 +msgid "Enable a fallback universal tax rate when no country-specific rates match." +msgstr "" + +#: inc/tax/class-tax.php:192 +msgid "Universal Tax Rate (%)" +msgstr "" + +#: inc/tax/class-tax.php:193 +msgid "Tax rate applied when no country-specific rate matches." +msgstr "" + +#: inc/tax/class-tax.php:258 msgid "Regular" msgstr "" -#: inc/tax/class-tax.php:232 +#: inc/tax/class-tax.php:301 #: inc/ui/class-site-actions-element.php:184 #: views/limitations/plugin-selector.php:80 msgid "Default" msgstr "" -#: inc/tax/class-tax.php:295 +#: inc/tax/class-tax.php:364 msgid "You don't have permission to alter tax rates" msgstr "" -#: inc/tax/class-tax.php:314 +#: inc/tax/class-tax.php:383 msgid "No tax rates present in the request" msgstr "" -#: inc/tax/class-tax.php:346 +#: inc/tax/class-tax.php:415 msgid "Tax Rates successfully updated!" msgstr "" -#: inc/tax/class-tax.php:386 -#: inc/tax/class-tax.php:390 +#: inc/tax/class-tax.php:455 +#: inc/tax/class-tax.php:459 msgid "Manage Tax Rates" msgstr "" -#: inc/tax/class-tax.php:394 +#: inc/tax/class-tax.php:463 msgid "Add different tax rates depending on the country of your customers." msgstr "" -#: inc/tax/class-tax.php:400 +#: inc/tax/class-tax.php:469 msgid "You need to activate tax support first." msgstr "" -#: inc/tax/class-tax.php:408 -#: inc/tax/class-tax.php:414 -#: inc/tax/class-tax.php:418 +#: inc/tax/class-tax.php:477 +#: inc/tax/class-tax.php:483 +#: inc/tax/class-tax.php:487 msgid "Manage Tax Rates →" msgstr "" From 7738dce8381df5e01907b9c7d14cf7750e7c9488 Mon Sep 17 00:00:00 2001 From: David Stone Date: Tue, 24 Mar 2026 20:40:34 -0600 Subject: [PATCH 2/2] refactor(tax): implement universal tax via country wildcard in tax rates UI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace the settings-based universal tax approach with the reviewer's suggested approach: add an 'Apply to all countries' (*) option to the country dropdown on the Tax Rates configuration page. Changes: - views/taxes/list.php: add '*' option at top of country dropdown - inc/functions/tax.php: handle '*' country as fallback in wu_get_applicable_tax_rates() — applies when no exact-country rates match - inc/tax/class-tax.php: remove apply_universal_tax_rate filter/method and remove enable_universal_tax/universal_tax_rate settings fields - lang/ultimate-multisite.pot: remove obsolete strings, add new 'Apply to all countries' string, update line number references This approach is simpler, consistent with how state/city already use blank/wildcard to mean 'apply to all', and requires no new settings. Addresses review feedback from superdav42 in PR #277. --- inc/functions/tax.php | 14 ++++++++ inc/tax/class-tax.php | 68 ----------------------------------- lang/ultimate-multisite.pot | 72 +++++++++++++++---------------------- views/taxes/list.php | 18 ++++++---- 4 files changed, 53 insertions(+), 119 deletions(-) diff --git a/inc/functions/tax.php b/inc/functions/tax.php index 1e833d5ac..4a56811e5 100644 --- a/inc/functions/tax.php +++ b/inc/functions/tax.php @@ -201,6 +201,20 @@ function wu_get_applicable_tax_rates($country, $tax_category = 'default', $state } } + /* + * If no country-specific rates matched, apply any rates configured + * with '*' (Apply to all countries) as a fallback. + */ + if (empty($results)) { + foreach ($tax_category['rates'] as $rate) { + if ('*' === $rate['country']) { + $rate['order'] = 1; + + $results[ $rate['id'] ] = $rate; + } + } + } + uasort($results, 'wu_sort_by_order'); return array_values($results); diff --git a/inc/tax/class-tax.php b/inc/tax/class-tax.php index 62edfa8f1..5f48052c0 100644 --- a/inc/tax/class-tax.php +++ b/inc/tax/class-tax.php @@ -32,8 +32,6 @@ public function init(): void { add_action('wu_page_wp-ultimo-settings_load', [$this, 'add_sidebar_widget']); - add_filter('wu_cart_applicable_tax_rates', [$this, 'apply_universal_tax_rate'], 10, 4); - if ($this->is_enabled()) { add_action('wp_ultimo_admin_pages', [$this, 'add_admin_page']); @@ -92,42 +90,6 @@ function () { } } - /** - * Applies universal tax rate when no specific rates are found. - * - * @since 2.0.0 - * @param array $rates Tax rates. - * @param string $country Country code. - * @param string $tax_category Tax category. - * @param object $cart Cart object. - * @return array - */ - public function apply_universal_tax_rate(array $rates, string $country, string $tax_category, $cart): array { - - if (empty($rates) && wu_should_collect_taxes() && wu_get_setting('enable_universal_tax', false)) { - $universal_rate = wu_get_setting('universal_tax_rate', 10); - - $rates = [ - [ - 'id' => 'universal-' . uniqid(), - 'title' => __('Universal Tax', 'ultimate-multisite'), - 'country' => '*', - 'state' => '', - 'city' => '', - 'tax_type' => 'percentage', - 'tax_amount' => $universal_rate, - 'tax_rate' => $universal_rate, - 'priority' => 1, - 'compound' => false, - 'type' => 'regular', - 'rate' => $universal_rate, - ] - ]; - } - - return $rates; - } - /** * Register tax settings. * @@ -171,36 +133,6 @@ public function add_settings(): void { ] ); - wu_register_settings_field( - 'taxes', - 'enable_universal_tax', - [ - 'title' => __('Enable Universal Tax', 'ultimate-multisite'), - 'desc' => __('Enable a fallback universal tax rate when no country-specific rates match.', 'ultimate-multisite'), - 'type' => 'toggle', - 'default' => 0, - 'require' => [ - 'enable_taxes' => 1, - ], - ] - ); - - wu_register_settings_field( - 'taxes', - 'universal_tax_rate', - [ - 'title' => __('Universal Tax Rate (%)', 'ultimate-multisite'), - 'desc' => __('Tax rate applied when no country-specific rate matches.', 'ultimate-multisite'), - 'type' => 'number', - 'default' => 10, - 'min' => 0, - 'max' => 100, - 'require' => [ - 'enable_taxes' => 1, - 'enable_universal_tax' => 1, - ], - ] - ); } /** diff --git a/lang/ultimate-multisite.pot b/lang/ultimate-multisite.pot index 024e7bbd6..ea1e4a489 100644 --- a/lang/ultimate-multisite.pot +++ b/lang/ultimate-multisite.pot @@ -16405,7 +16405,7 @@ msgstr "" #: views/sites/edit-placeholders.php:49 #: views/sites/edit-placeholders.php:177 #: views/taxes/list.php:102 -#: views/taxes/list.php:309 +#: views/taxes/list.php:313 msgid "Select All" msgstr "" @@ -18472,80 +18472,60 @@ msgstr "" msgid "Taxes Collected" msgstr "" -#: inc/tax/class-tax.php:113 -msgid "Universal Tax" -msgstr "" - -#: inc/tax/class-tax.php:153 +#: inc/tax/class-tax.php:115 msgid "Enable Taxes" msgstr "" -#: inc/tax/class-tax.php:154 +#: inc/tax/class-tax.php:116 msgid "Enable this option to be able to collect sales taxes on your network payments." msgstr "" -#: inc/tax/class-tax.php:164 +#: inc/tax/class-tax.php:126 msgid "Inclusive Tax" msgstr "" -#: inc/tax/class-tax.php:165 +#: inc/tax/class-tax.php:127 msgid "Enable this option if your prices include taxes. In that case, Ultimate Multisite will calculate the included tax instead of adding taxes to the price." msgstr "" -#: inc/tax/class-tax.php:178 -msgid "Enable Universal Tax" -msgstr "" - -#: inc/tax/class-tax.php:179 -msgid "Enable a fallback universal tax rate when no country-specific rates match." -msgstr "" - -#: inc/tax/class-tax.php:192 -msgid "Universal Tax Rate (%)" -msgstr "" - -#: inc/tax/class-tax.php:193 -msgid "Tax rate applied when no country-specific rate matches." -msgstr "" - -#: inc/tax/class-tax.php:258 +#: inc/tax/class-tax.php:190 msgid "Regular" msgstr "" -#: inc/tax/class-tax.php:301 +#: inc/tax/class-tax.php:233 #: inc/ui/class-site-actions-element.php:184 #: views/limitations/plugin-selector.php:80 msgid "Default" msgstr "" -#: inc/tax/class-tax.php:364 +#: inc/tax/class-tax.php:296 msgid "You don't have permission to alter tax rates" msgstr "" -#: inc/tax/class-tax.php:383 +#: inc/tax/class-tax.php:315 msgid "No tax rates present in the request" msgstr "" -#: inc/tax/class-tax.php:415 +#: inc/tax/class-tax.php:347 msgid "Tax Rates successfully updated!" msgstr "" -#: inc/tax/class-tax.php:455 -#: inc/tax/class-tax.php:459 +#: inc/tax/class-tax.php:387 +#: inc/tax/class-tax.php:391 msgid "Manage Tax Rates" msgstr "" -#: inc/tax/class-tax.php:463 +#: inc/tax/class-tax.php:395 msgid "Add different tax rates depending on the country of your customers." msgstr "" -#: inc/tax/class-tax.php:469 +#: inc/tax/class-tax.php:401 msgid "You need to activate tax support first." msgstr "" -#: inc/tax/class-tax.php:477 -#: inc/tax/class-tax.php:483 -#: inc/tax/class-tax.php:487 +#: inc/tax/class-tax.php:409 +#: inc/tax/class-tax.php:415 +#: inc/tax/class-tax.php:419 msgid "Manage Tax Rates →" msgstr "" @@ -20943,22 +20923,22 @@ msgid "No items to display" msgstr "" #: views/sites/edit-placeholders.php:209 -#: views/taxes/list.php:342 +#: views/taxes/list.php:346 msgid "Add new Row" msgstr "" #: views/sites/edit-placeholders.php:215 -#: views/taxes/list.php:348 +#: views/taxes/list.php:352 msgid "Delete Selected Rows" msgstr "" #: views/sites/edit-placeholders.php:241 -#: views/taxes/list.php:374 +#: views/taxes/list.php:378 msgid "Save your changes!" msgstr "" #: views/sites/edit-placeholders.php:245 -#: views/taxes/list.php:378 +#: views/taxes/list.php:382 msgid "Saving..." msgstr "" @@ -21007,12 +20987,16 @@ msgstr "" msgid "Loading Tax Rates..." msgstr "" -#: views/taxes/list.php:244 -#: views/taxes/list.php:259 +#: views/taxes/list.php:221 +msgid "Apply to all countries" +msgstr "" + +#: views/taxes/list.php:250 +#: views/taxes/list.php:265 msgid "Leave blank to apply to all" msgstr "" -#: views/taxes/list.php:388 +#: views/taxes/list.php:392 msgid "Save Tax Rates" msgstr "" diff --git a/views/taxes/list.php b/views/taxes/list.php index 93267768b..d1d65a587 100644 --- a/views/taxes/list.php +++ b/views/taxes/list.php @@ -215,19 +215,23 @@ case 'country': ?> - - $country_name) : ?> + - + - + - + + +