Plugin Directory

Changeset 3444271


Ignore:
Timestamp:
01/21/2026 04:45:11 PM (2 months ago)
Author:
ceretax
Message:

Release V1.4.6

Location:
ceretax
Files:
26 added
2 edited

Legend:

Unmodified
Added
Removed
  • ceretax/trunk/ceretax.php

    r3393901 r3444271  
    44 * Plugin URI: https://wordpress.org/plugins/ceretax/
    55 * Description: Simplify sales tax complexity with CereTax for WooCommerce.
    6  * Version: 1.4.5
     6 * Version: 1.4.6
    77 * Author: CereTax, Inc.
    88 * Author URI: https://www.ceretax.com/
  • ceretax/trunk/inc/class-cwafc.php

    r3393901 r3444271  
    581581                $tax = $custom_tax_rate;
    582582                //error_log('tax: '.$tax);
    583                 // Add recurring fee for subscription product.         
    584583                if ( ! empty( $cart->recurring_cart_key ) ) {
    585584                    $ceretax_tmp_data       = get_option( 'ceretax_tmp_data' );
     
    614613                                    $line_item_tax_amount       = 0;
    615614                                    $retail_delivery_fee_amount = 0;
    616 
     615               
    617616                                    if ( ! empty( $line_item_taxe_breaks ) && is_array( $line_item_taxe_breaks ) ) {
    618617                                        foreach ( $line_item_taxe_breaks as $val ) {
     
    631630                                                } elseif ( strtoupper( $state ) === 'MN' && $line_item_revenue_amount >= 100 ) {
    632631                                                    $retail_delivery_fee_amount += $val->rate;
    633 
     632               
    634633                                                }
    635634                                                // do NOT add anything if state is different
    636635                                                continue;
    637636                                            }
    638 
     637               
    639638                                            // Normal percentage-based taxes, prorated
    640639                                            if ( ! empty( $val->calculationBaseAmt ) && ( $val->calculationBaseAmt >= $line_item_revenue_amount ) ) {
     
    642641                                                $line_item_tax_amount      += (float) $val->totalTax;
    643642                                            }
    644 
     643               
    645644                                            // Accumulate transaction (shipping) charges tax for recurring totals
    646645                                            if ( isset( $val->transactionChargesTax ) && $val->transactionChargesTax > 0 && strtoupper( $val->taxTypeClassDesc ) !== 'RETAIL DELIVERY FEE' ) {
     
    649648                                        }
    650649                                    }
    651 
     650               
    652651                                    // Pro-rate percentage-based tax
    653652                                    $line_item_total_tax = 0;
     
    657656                                        $line_item_total_tax = $line_item_tax_amount;
    658657                                    }
    659 
     658               
    660659                                    // Accumulate percentage-based taxes
    661660                                    $total_recurring_percentage_taxes += $line_item_total_tax;
    662 
     661               
    663662                                    // Apply Retail Delivery Fee once per subscription period/interval group
    664663                                    if ( $retail_delivery_fee_amount > 0 ) {
     
    696695                        }
    697696                    }
    698 
     697               
    699698                    // Combine percentage taxes with RDF based on interval distribution
    700699                    // - If exactly one distinct interval group: apply RDF once (use that group's fixed amount)
     
    707706                    }
    708707                    $total_recurring_fee = $total_recurring_percentage_taxes + $rdf_total_for_recurring + (float) $total_transaction_charges_tax;
    709 
     708               
    710709                    //Fallback if nothing matched from Ceretax API
    711710                    if ( $total_recurring_fee <= 0 && ! empty( $custom_tax_rate ) ) {
     
    859858                $order->add_item( $fee );
    860859
    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.
    863869                $order->save();
    864870
     
    11181124                    $order->add_item( $item_fee );
    11191125                }
    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.
    11271148                $order->save();
    11281149            }
     
    12111232                    $r_order->add_item( $item_fee );
    12121233                }
    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                }
    12151241                // Save the order.
    12161242                $r_order->save();
     
    12901316            if ( isset( $res ) && is_numeric( $res ) ) {
    12911317                $fee_exists = false;
     1318                $fee_modified = false;
     1319               
    12921320                foreach ( $order->get_fees() as $fee ) {
    12931321                    if ( $fee->get_name() === $cere_tax_name ) {
     1322                        $old_amount = $fee->get_total();
    12941323                        $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                        }
    12981332                        $fee_exists = true;
    12991333                        break;
     
    13081342                    $item_fee->set_total( $new_fee_amount );
    13091343                    $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                }
    13161357            }
    13171358            self::$is_calculating = false;
     
    14361477                    $refund->add_item( $item_fee );
    14371478                }
    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                }
    14401486
    14411487                // Save the order.
     
    25092555                if ( 1 == $require_recalculation ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
    25102556                    $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                    }
    25122564                }
    25132565            }
Note: See TracChangeset for help on using the changeset viewer.