Changeset 3444271
- Timestamp:
- 01/21/2026 04:45:11 PM (2 months ago)
- Location:
- ceretax
- Files:
-
- 26 added
- 2 edited
-
tags/1.4.6 (added)
-
tags/1.4.6/assets (added)
-
tags/1.4.6/assets/css (added)
-
tags/1.4.6/assets/css/admin.css (added)
-
tags/1.4.6/assets/js (added)
-
tags/1.4.6/assets/js/admin.js (added)
-
tags/1.4.6/assets/js/front.js (added)
-
tags/1.4.6/ceretax.php (added)
-
tags/1.4.6/changelog.txt (added)
-
tags/1.4.6/inc (added)
-
tags/1.4.6/inc/admin (added)
-
tags/1.4.6/inc/admin/class-cwafc-admin-action.php (added)
-
tags/1.4.6/inc/admin/class-cwafc-admin-action.php-live020725 (added)
-
tags/1.4.6/inc/admin/class-cwafc-admin-filter.php (added)
-
tags/1.4.6/inc/admin/class-cwafc-admin.php (added)
-
tags/1.4.6/inc/class-cwafc.php (added)
-
tags/1.4.6/inc/front (added)
-
tags/1.4.6/inc/front/class-cwafc-front-action.php (added)
-
tags/1.4.6/inc/front/class-cwafc-front-filter.php (added)
-
tags/1.4.6/inc/front/class-cwafc-front.php (added)
-
tags/1.4.6/languages (added)
-
tags/1.4.6/languages/ceretax.pot (added)
-
tags/1.4.6/readme.txt (added)
-
tags/1.4.6/woocommerce (added)
-
tags/1.4.6/woocommerce/cart (added)
-
tags/1.4.6/woocommerce/cart/shipping-calculator.php (added)
-
trunk/ceretax.php (modified) (1 diff)
-
trunk/inc/class-cwafc.php (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
ceretax/trunk/ceretax.php
r3393901 r3444271 4 4 * Plugin URI: https://wordpress.org/plugins/ceretax/ 5 5 * Description: Simplify sales tax complexity with CereTax for WooCommerce. 6 * Version: 1.4. 56 * Version: 1.4.6 7 7 * Author: CereTax, Inc. 8 8 * Author URI: https://www.ceretax.com/ -
ceretax/trunk/inc/class-cwafc.php
r3393901 r3444271 581 581 $tax = $custom_tax_rate; 582 582 //error_log('tax: '.$tax); 583 // Add recurring fee for subscription product.584 583 if ( ! empty( $cart->recurring_cart_key ) ) { 585 584 $ceretax_tmp_data = get_option( 'ceretax_tmp_data' ); … … 614 613 $line_item_tax_amount = 0; 615 614 $retail_delivery_fee_amount = 0; 616 615 617 616 if ( ! empty( $line_item_taxe_breaks ) && is_array( $line_item_taxe_breaks ) ) { 618 617 foreach ( $line_item_taxe_breaks as $val ) { … … 631 630 } elseif ( strtoupper( $state ) === 'MN' && $line_item_revenue_amount >= 100 ) { 632 631 $retail_delivery_fee_amount += $val->rate; 633 632 634 633 } 635 634 // do NOT add anything if state is different 636 635 continue; 637 636 } 638 637 639 638 // Normal percentage-based taxes, prorated 640 639 if ( ! empty( $val->calculationBaseAmt ) && ( $val->calculationBaseAmt >= $line_item_revenue_amount ) ) { … … 642 641 $line_item_tax_amount += (float) $val->totalTax; 643 642 } 644 643 645 644 // Accumulate transaction (shipping) charges tax for recurring totals 646 645 if ( isset( $val->transactionChargesTax ) && $val->transactionChargesTax > 0 && strtoupper( $val->taxTypeClassDesc ) !== 'RETAIL DELIVERY FEE' ) { … … 649 648 } 650 649 } 651 650 652 651 // Pro-rate percentage-based tax 653 652 $line_item_total_tax = 0; … … 657 656 $line_item_total_tax = $line_item_tax_amount; 658 657 } 659 658 660 659 // Accumulate percentage-based taxes 661 660 $total_recurring_percentage_taxes += $line_item_total_tax; 662 661 663 662 // Apply Retail Delivery Fee once per subscription period/interval group 664 663 if ( $retail_delivery_fee_amount > 0 ) { … … 696 695 } 697 696 } 698 697 699 698 // Combine percentage taxes with RDF based on interval distribution 700 699 // - If exactly one distinct interval group: apply RDF once (use that group's fixed amount) … … 707 706 } 708 707 $total_recurring_fee = $total_recurring_percentage_taxes + $rdf_total_for_recurring + (float) $total_transaction_charges_tax; 709 708 710 709 //Fallback if nothing matched from Ceretax API 711 710 if ( $total_recurring_fee <= 0 && ! empty( $custom_tax_rate ) ) { … … 859 858 $order->add_item( $fee ); 860 859 861 // Recalculate order totals. 862 $order->calculate_totals(); 860 // Recalculate order totals to ensure order total includes the fee. 861 // Use static flag to prevent recursive calls from woocommerce_order_after_calculate_totals hook. 862 if ( ! self::$is_calculating ) { 863 self::$is_calculating = true; 864 $order->calculate_totals(); 865 self::$is_calculating = false; 866 } 867 868 // Save the order to persist the updated totals. 863 869 $order->save(); 864 870 … … 1118 1124 $order->add_item( $item_fee ); 1119 1125 } 1120 // Recalculate totals only in admin to prevent checkout conflicts. 1121 if ( $order->get_total() > 0 && is_admin() ) { 1122 $order->calculate_totals(); 1123 // Fires after order totals are recalculated. 1124 // do_action( 'woocommerce_order_after_calculate_totals', $order->get_id(), $order ); 1125 } 1126 // Save the order. 1126 1127 // Always recalculate totals after modifying fees to ensure order total includes tax. 1128 // Use static flag to prevent recursive calls from woocommerce_order_after_calculate_totals hook. 1129 if ( ! self::$is_calculating ) { 1130 $should_recalc = false; 1131 1132 if ( is_admin() ) { 1133 // Admin, subscriptions, order edit screens – always safe to recalc. 1134 $should_recalc = true; 1135 } elseif ( (float) $order->get_total() > 0 ) { 1136 // Frontend, but not a zero-total (voucher) order. 1137 $should_recalc = true; 1138 } 1139 1140 if ( $should_recalc ) { 1141 self::$is_calculating = true; 1142 $order->calculate_totals(); 1143 self::$is_calculating = false; 1144 } 1145 } 1146 1147 // Save the order to persist the updated totals. 1127 1148 $order->save(); 1128 1149 } … … 1211 1232 $r_order->add_item( $item_fee ); 1212 1233 } 1213 // Recalculate order totals. 1214 $r_order->calculate_totals(); 1234 // Recalculate order totals to ensure order total includes the fee. 1235 // Use static flag to prevent recursive calls from woocommerce_order_after_calculate_totals hook. 1236 if ( ! self::$is_calculating ) { 1237 self::$is_calculating = true; 1238 $r_order->calculate_totals(); 1239 self::$is_calculating = false; 1240 } 1215 1241 // Save the order. 1216 1242 $r_order->save(); … … 1290 1316 if ( isset( $res ) && is_numeric( $res ) ) { 1291 1317 $fee_exists = false; 1318 $fee_modified = false; 1319 1292 1320 foreach ( $order->get_fees() as $fee ) { 1293 1321 if ( $fee->get_name() === $cere_tax_name ) { 1322 $old_amount = $fee->get_total(); 1294 1323 $new_fee_amount = $res; 1295 $fee->set_amount( $new_fee_amount ); 1296 $fee->set_total( $new_fee_amount ); 1297 $fee->save(); 1324 1325 // Only update if the amount has changed to avoid unnecessary recalculations. 1326 if ( abs( $old_amount - $new_fee_amount ) > 0.01 ) { 1327 $fee->set_amount( $new_fee_amount ); 1328 $fee->set_total( $new_fee_amount ); 1329 $fee->save(); 1330 $fee_modified = true; 1331 } 1298 1332 $fee_exists = true; 1299 1333 break; … … 1308 1342 $item_fee->set_total( $new_fee_amount ); 1309 1343 $order->add_item( $item_fee ); 1310 } 1311 // Recalculate order totals. 1312 $order->calculate_totals(); 1313 1314 // Save the order. 1315 $order->save(); 1344 $fee_modified = true; 1345 } 1346 1347 // Only recalculate if fees were actually modified. 1348 if ( $fee_modified ) { 1349 // Recalculate order totals to ensure order total includes the fee. 1350 // The static flag prevents infinite recursion since we're already inside 1351 // the woocommerce_order_after_calculate_totals hook. 1352 $order->calculate_totals(); 1353 1354 // Save the order to persist the updated totals. 1355 $order->save(); 1356 } 1316 1357 } 1317 1358 self::$is_calculating = false; … … 1436 1477 $refund->add_item( $item_fee ); 1437 1478 } 1438 // Recalculate order totals. 1439 $refund->calculate_totals(); 1479 // Recalculate order totals to ensure refund total includes the fee. 1480 // Use static flag to prevent recursive calls from woocommerce_order_after_calculate_totals hook. 1481 if ( ! self::$is_calculating ) { 1482 self::$is_calculating = true; 1483 $refund->calculate_totals(); 1484 self::$is_calculating = false; 1485 } 1440 1486 1441 1487 // Save the order. … … 2509 2555 if ( 1 == $require_recalculation ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison 2510 2556 $order->save(); 2511 $order->calculate_totals(); 2557 // Recalculate order totals after removing tax items. 2558 // Use static flag to prevent recursive calls from woocommerce_order_after_calculate_totals hook. 2559 if ( ! self::$is_calculating ) { 2560 self::$is_calculating = true; 2561 $order->calculate_totals(); 2562 self::$is_calculating = false; 2563 } 2512 2564 } 2513 2565 }
Note: See TracChangeset
for help on using the changeset viewer.