From a3f97e8278d6e13bb8a39decd7586911a7e88765 Mon Sep 17 00:00:00 2001 From: Mahyar Rezghi Date: Sun, 30 Nov 2025 13:04:22 +0330 Subject: [PATCH] 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 90eaf4ab..62edfa8f 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 467e8cc8..024e7bbd 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 ""