Plugin Directory

Changeset 3320210


Ignore:
Timestamp:
06/30/2025 06:51:59 PM (9 months ago)
Author:
ceretax
Message:

Ceretax Woo V1.4.3

Location:
ceretax
Files:
25 added
6 edited

Legend:

Unmodified
Added
Removed
  • ceretax/trunk/ceretax.php

    r3285848 r3320210  
    44 * Plugin URI: https://wordpress.org/plugins/ceretax/
    55 * Description: Simplify sales tax complexity with CereTax for WooCommerce.
    6  * Version: 1.4.2
     6 * Version: 1.4.3
    77 * Author: CereTax, Inc.
    88 * Author URI: https://www.ceretax.com/
     
    1414 *
    1515 * Woo: 12345:342928dfsfhsf8429842374wdf4234sfd
    16  * WC requires at least: 6.0
    17  * WC tested up to: 8.0
     16 * WC requires at least: 7.0.0
     17 * WC tested up to: 9.9.5
    1818 *
    1919 * License: GNU General Public License v3.0
  • ceretax/trunk/changelog.txt

    r3285848 r3320210  
    5252* fix - System now supports multiple partial refunds on a single order, providing greater flexibility in transaction management
    5353* fix - Corrected recurring subscription product tax display issue on the checkout page to show accurate tax amounts
     54
     552025-05-28 - version 1.4.3
     56* fix - Resolved PHP warning errors
     57* add - Included 'shipFromAddress' parameter in the ceretax API request
  • ceretax/trunk/inc/admin/class-cwafc-admin-action.php

    r3285848 r3320210  
    4848            // Add admin order style and script.
    4949            add_action( 'admin_footer', array( $this, 'action__cwafc_admin_footer_function' ), 20 );
     50            // Add add store address validate button WC general setting tab.
     51            add_filter( 'woocommerce_general_settings', array( $this, 'filter__cwafc_add_store_address_validate_button' ) );
     52            // Add render store address validate button.
     53            add_action( 'woocommerce_admin_field_validate_store_address_button', array( $this, 'action__cwafc_render_store_address_validate_button' ) );
     54            // Validate_store_address.
     55            add_action( 'woocommerce_update_options_general', array( $this, 'action__cwafc_validate_store_address' ) );
     56            // Add custom style after total.
     57            add_action( 'woocommerce_admin_order_totals_after_total', array( $this, 'action__cwafc_add_after_total_style' ) );
    5058        }
    5159
     
    413421        public function action__cwafc_admin_footer_function() {
    414422            $order_id = null;
     423            // phpcs:disable WordPress.Security.NonceVerification.Recommended
    415424            if ( ( isset( $_GET['post'] ) && ! empty( $_GET['post'] ) ) || ( isset( $_GET['id'] ) && ! empty( $_GET['id'] ) ) ) {
    416425                if ( isset( $_GET['post'] ) && ! empty( $_GET['post'] ) ) {
    417                     $order_id = $_GET['post'];
    418                 } else if ( isset( $_GET['id'] ) && ! empty( $_GET['id'] ) ) {
    419                     $order_id = $_GET['id'];
     426                    $order_id = sanitize_text_field( wp_unslash( $_GET['post'] ) );
     427                } elseif ( isset( $_GET['id'] ) && ! empty( $_GET['id'] ) ) {
     428                    $order_id = sanitize_text_field( wp_unslash( $_GET['id'] ) );
    420429                } else {
    421430                    $order_id = '';
    422431                }
    423432            }
     433            // phpcs:enable WordPress.Security.NonceVerification.Recommended
    424434
    425435            $cere_tax_name = get_option( CWAFC_PREFIX . '_cere_tax_name', 'Tax' );
     
    434444                }
    435445            }
    436             if ( ( ! empty( $order_id ) && ! empty( $ceretax_data ) && $fee_amount > 0 ) || ( 'shop_order' === get_post_type( $order_id ) && empty( $ceretax_data ) ) ) { ?>
    437                 <style>
    438                     /* Hide the 3rd tr of .wc-order-totals if .wc-used-coupons is found */
    439                     .wc-order-data-row:has(.wc-used-coupons) .wc-order-totals:first-of-type tr:nth-of-type(3) {
    440                         display: none !important;
    441                     }
    442 
    443                     /* Hide the 2nd tr of .wc-order-totals if .wc-used-coupons is not found */
    444                     .wc-order-data-row:not(:has(.wc-used-coupons)) .wc-order-totals:first-of-type tr:nth-of-type(2) {
    445                         display: none !important;
    446                     }
    447 
    448                 </style>
    449             <?php } if ( ! empty( $order_id ) && ! empty( $ceretax_data ) ) { ?>
     446            if ( ! empty( $order_id ) && ! empty( $ceretax_data ) ) { ?>
    450447                <style>
    451448                    /* Hide Tax column for ceretax order item list */
     
    456453
    457454                </style>
    458             <?php
    459             }
    460             ?>
     455            <?php } ?>
    461456            <style>
    462457                /* Ceretax table stysle */
     
    481476                }
    482477            </style>
    483             <?php
    484             // Refund order script.
    485             if ( ! empty( $order_id ) && ! empty( $ceretax_data ) ) { ?>
     478            <?php if ( ! empty( $order_id ) && ! empty( $ceretax_data ) ) { // Refund order script. ?>
    486479            <script>
    487480                jQuery(function($) {
     
    565558                    jQuery( '.woocommerce_order_items #order_fee_line_items tr' ).each( function() {
    566559                        var feeName = jQuery( this ).find('.name .view').text().trim();
    567                         var taxName = '<?php echo get_option( CWAFC_PREFIX . '_cere_tax_name', 'Tax' ); ?>';
     560                        var taxName = '<?php echo esc_js( get_option( CWAFC_PREFIX . '_cere_tax_name', 'Tax' ) ); ?>';
    568561                        if ( feeName === taxName ) {
    569562                            jQuery( this ).find( '.refund input' ).hide();
     
    584577            $ceretax_data_lineitem = get_post_meta( $order->get_id(), 'ceretax_data_lineitem', true );
    585578            $ceretax_data          = get_post_meta( $order->get_id(), 'ceretax_data', true );
    586             // if ( ! empty( $ceretax_data_lineitem ) ) {
    587579            if ( ! empty( $ceretax_data_lineitem ) && ! empty( $ceretax_data ) && ! empty( $order ) ) {
    588580                $cere_tax_line_item_column_name = get_option( CWAFC_PREFIX . '_cere_tax_line_item_column_name', 'Tax Total' );
     
    601593        public function action__cwafc_woocommerce_admin_order_item_values( $product, $item, $item_id ) {
    602594
    603             // phpcs:disable WordPress.Security.NonceVerification.Missing
     595            // phpcs:disable WordPress.Security.NonceVerification
    604596            $order_id = null;
    605597            if ( isset( $_GET['post'] ) && ! empty( $_GET['post'] ) ) {
    606598                $order_id = sanitize_text_field( wp_unslash( $_GET['post'] ) );
    607             } else if ( isset( $_GET['id'] ) && ! empty( $_GET['id'] ) ) {
     599            } elseif ( isset( $_GET['id'] ) && ! empty( $_GET['id'] ) ) {
    608600                $order_id = sanitize_text_field( wp_unslash( $_GET['id'] ) );
    609601            } else {
     
    613605                $order_id = sanitize_text_field( wp_unslash( $_POST['order_id'] ) );
    614606            }
    615             // phpcs:enable WordPress.Security.NonceVerification.Missing
     607            // phpcs:enable WordPress.Security.NonceVerification
    616608
    617609            $cere_tax_name         = get_option( CWAFC_PREFIX . '_cere_tax_name', 'Tax' );
     
    623615
    624616                if ( ! empty( $product ) ) {
    625 
    626                     // $refunded_qty = -1 * $order->get_total_refunded_for_item( $item_id );
    627 
    628617                    echo '<td class="ceretax_line_value" width="7%">';
    629618                    if ( is_object( $ceretax_data_lineitem ) && is_object( $ceretax_data ) ) {
     
    643632                            $line_item_total_tax        = 0;
    644633
    645                             $filtered = array_filter( $ceretax_items_data, function ( $filtered_item ) use ( $product_id ) {
    646                                 return isset( $filtered_item->itemNumber ) && $filtered_item->itemNumber == $product_id;
    647                             });
    648 
    649                             $refunded_filtered = array_filter( $ceretax_data, function ( $refunded_item ) use ( $product_id ) {
    650                                 return isset( $refunded_item->itemNumber ) && $refunded_item->itemNumber == $product_id;
    651                             });
    652 
    653                             $line_item_revenue_amount = ! empty( $filtered ) ? reset( $filtered )->revenue : 0;
     634                            $filtered = array_filter(
     635                                $ceretax_items_data,
     636                                function ( $filtered_item ) use ( $product_id ) {
     637                                    return isset( $filtered_item->itemNumber ) && $filtered_item->itemNumber == $product_id; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
     638                                }
     639                            );
     640
     641                            $refunded_filtered = array_filter(
     642                                $ceretax_data,
     643                                function ( $refunded_item ) use ( $product_id ) {
     644                                    return isset( $refunded_item->itemNumber ) && $refunded_item->itemNumber == $product_id; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
     645                                }
     646                            );
     647
     648                            $line_item_revenue_amount = ! empty( $filtered ) ? round( reset( $filtered )->revenue, 2 ) : 0;
    654649                            $line_item_taxe_breaks    = ! empty( $filtered ) ? reset( $filtered )->taxes : array();
    655650
    656651                            if ( ! empty( $line_item_taxe_breaks ) && is_array( $line_item_taxe_breaks ) ) {
    657652                                foreach ( $line_item_taxe_breaks as $key => $val ) {
    658                                     if ( ! empty( $val->calculationBaseAmt ) && ( $val->calculationBaseAmt >= $line_item_revenue_amount ) ) {
    659                                         $line_item_calc_base_amount  = $val->calculationBaseAmt;
    660                                         $line_item_total_tax_amount += $val->totalTax;
     653                                    if ( ! empty( $val->calculationBaseAmt ) && ( $val->calculationBaseAmt >= $line_item_revenue_amount ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName
     654                                        $line_item_calc_base_amount  = $val->calculationBaseAmt; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
     655                                        $line_item_total_tax_amount += $val->totalTax; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
    661656                                    }
    662657                                }
     
    666661                            }
    667662
    668                             $total_line_tax          = ! empty( $filtered ) ? reset( $filtered )->totalTaxLine : null;
    669                             $total_line_refunded_tax = ! empty( $refunded_filtered ) ? reset( $refunded_filtered )->totalTaxLine : null;
     663                            $total_line_tax                 = ! empty( $filtered ) ? reset( $filtered )->totalTaxLine : null;
     664                            $total_line_refunded_tax        = ! empty( $refunded_filtered ) ? reset( $refunded_filtered )->totalTaxLine : null;
    670665                            $ceretax_refunded_line_item_tax = $item->get_meta( '_ceretax_refunded_line_item_tax' );
    671666                            echo '<div class="view">';
    672                                 echo wc_price( $line_item_total_tax, array( 'currency' => $order->get_currency() ) );
    673                                 if ( is_array( $order->get_refunds() ) && count( $order->get_refunds() ) > 0 && ! empty( $ceretax_refunded_line_item_tax ) ) {
    674                                     echo '<small class="refunded">' . wc_price( -1 * $ceretax_refunded_line_item_tax, array( 'currency' => $order->get_currency() ) ) . '</small>';
    675                                 }
     667                            echo wp_kses_post( wc_price( $line_item_total_tax, array( 'currency' => $order->get_currency() ) ) );
     668                            if ( is_array( $order->get_refunds() ) && count( $order->get_refunds() ) > 0 && ! empty( $ceretax_refunded_line_item_tax ) ) {
     669                                echo '<small class="refunded">' . wp_kses_post( wc_price( -1 * $ceretax_refunded_line_item_tax, array( 'currency' => $order->get_currency() ) ) ) . '</small>';
     670                            }
    676671                            echo '</div>';
    677672
     
    688683                    echo '<td class="ceretax_line_value" width="7%">';
    689684
    690                     $shipping_tax_amount = 0;
    691                     $shipping_line_total = $item['total'];
     685                    $shipping_tax_amount            = 0;
     686                    $shipping_line_total            = $item['total'];
    692687                    $ceretax_refunded_line_item_tax = $item->get_meta( '_ceretax_refunded_line_item_tax' );
    693688                    if ( ! empty( $item['total'] ) && is_object( $ceretax_data_lineitem ) ) {
    694689                        $ceretax_items_data = $ceretax_data_lineitem->invoice->lineItems;
    695690                        if ( ! empty( $ceretax_items_data ) ) {
    696                             $tt = 0;
    697691                            $ceretax_items_count = 0;
    698692                            foreach ( $ceretax_items_data as $item_data ) {
    699                                 $line_item_tax_breaks = $item_data->taxes;
     693                                $line_item_tax_breaks         = $item_data->taxes;
    700694                                $transaction_charges_tax      = 0;
    701695                                $tras_charge_calc_base_amount = 0;
    702696                                if ( ! empty( $line_item_tax_breaks ) ) {
    703697                                    foreach ( $line_item_tax_breaks as $key => $val ) {
    704                                         if ( ! empty( $val->transactionChargesCalculationBaseAmt ) ) {
    705                                             $tras_charge_calc_base_amount = $val->transactionChargesCalculationBaseAmt;
    706                                             $transaction_charges_tax      = $transaction_charges_tax + $val->transactionChargesTax;
     698                                        if ( ! empty( $val->transactionChargesCalculationBaseAmt ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName
     699                                            $tras_charge_calc_base_amount = $val->transactionChargesCalculationBaseAmt; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
     700                                            $transaction_charges_tax      = $transaction_charges_tax + $val->transactionChargesTax; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
    707701                                        }
    708702                                    }
    709703                                }
    710704                                if ( ! empty( $tras_charge_calc_base_amount ) ) {
    711                                     $ceretax_items_count++;
     705                                    $ceretax_items_count++; // phpcs:ignore WordPress.PHP.PreIncrement.PreIncrement
    712706                                    $shipping_tax_amount = $shipping_tax_amount + ( $transaction_charges_tax ) * ( $shipping_line_total / $tras_charge_calc_base_amount );
    713707                                }
     
    719713                    }
    720714                    echo '<div class="view">';
    721                         echo wc_price( $shipping_tax_amount, array( 'currency' => $order->get_currency() ) );
    722                         if ( is_array( $order->get_refunds() ) && count( $order->get_refunds() ) > 0 && ! empty( $ceretax_refunded_line_item_tax ) ) {
    723                             echo '<small class="refunded">' . wc_price( -1 * $ceretax_refunded_line_item_tax, array( 'currency' => $order->get_currency() ) ) . '</small>';
    724                         }
     715                    echo wp_kses_post( wc_price( $shipping_tax_amount, array( 'currency' => $order->get_currency() ) ) );
     716                    if ( is_array( $order->get_refunds() ) && count( $order->get_refunds() ) > 0 && ! empty( $ceretax_refunded_line_item_tax ) ) {
     717                        echo '<small class="refunded">' . wp_kses_post( wc_price( -1 * $ceretax_refunded_line_item_tax, array( 'currency' => $order->get_currency() ) ) ) . '</small>';
     718                    }
    725719                    echo '</div>';
    726720
     
    735729            }
    736730        }
     731
     732        /**
     733         * Add store address validate button.
     734         *
     735         * @param array $settings Setting fields.
     736         * @return $settings
     737         */
     738        public function filter__cwafc_add_store_address_validate_button( $settings ) {
     739            $new_settings = array();
     740            foreach ( $settings as $setting ) {
     741                $new_settings[] = $setting;
     742                // Insert custom button after the postcode field.
     743                if ( isset( $setting['id'] ) && 'store_address' === $setting['id'] && 'sectionend' === $setting['type'] ) {
     744                    $new_settings[] = array(
     745                        'type'  => 'validate_store_address_button', // Custom type, we'll render it below.
     746                        'id'    => 'validate_store_address',
     747                        'title' => '', // No label.
     748                    );
     749                }
     750            }
     751            return $new_settings;
     752        }
     753
     754        /**
     755         * Render store address validate button.
     756         *
     757         * @return void
     758         */
     759        public function action__cwafc_render_store_address_validate_button() {
     760            ?>
     761            <p class="submit">
     762                <button name="save" class="button-primary woocommerce-save-button" type="submit" value="Validate Address"><?php esc_html_e( 'Validate Address', 'woocommerce' ); ?></button>
     763            </p>
     764            <?php
     765        }
     766
     767        /**
     768         * Validate store address.
     769         *
     770         * @return mix
     771         */
     772        public function action__cwafc_validate_store_address() {
     773
     774            // phpcs:disable WordPress.Security.NonceVerification
     775            $address_1       = isset( $_POST['woocommerce_store_address'] ) ? sanitize_text_field( wp_unslash( $_POST['woocommerce_store_address'] ) ) : '';
     776            $address_2       = isset( $_POST['woocommerce_store_address_2'] ) ? sanitize_text_field( wp_unslash( $_POST['woocommerce_store_address_2'] ) ) : '';
     777            $city            = isset( $_POST['woocommerce_store_city'] ) ? sanitize_text_field( wp_unslash( $_POST['woocommerce_store_city'] ) ) : '';
     778            $postcode        = isset( $_POST['woocommerce_store_postcode'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_POST['woocommerce_store_postcode'] ) ) ) : '';
     779            $default_country = isset( $_POST['woocommerce_default_country'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_POST['woocommerce_default_country'] ) ) ) : '';
     780            // phpcs:enable WordPress.Security.NonceVerification
     781
     782            // Get country and state separately.
     783            list( $country, $state ) = array_pad( explode( ':', $default_country ), 2, '' );
     784
     785            // Get ceretax env and api key.
     786            $cere_tax_tax_env = get_option( CWAFC_PREFIX . '_cere_tax_environment' );
     787            $cere_tax_api_key = get_option( CWAFC_PREFIX . '_cere_tax_api_key' );
     788
     789            if ( 'production' === $cere_tax_tax_env ) {
     790                $api_url = 'https://av.prod.ceretax.net/validate';
     791            } else {
     792                $api_url = 'https://av.cert.ceretax.net/validate';
     793            }
     794
     795            $request_body = array(
     796                array(
     797                    'addressLine1' => $address_1,
     798                    'city'         => $city,
     799                    'state'        => $state,
     800                    'postalCode'   => $postcode,
     801                    'country'      => $country,
     802                ),
     803            );
     804
     805            $body = wp_json_encode( $request_body );
     806
     807            // add api request data in wc logs if log option enabled.
     808            $this->cwafc_wc_add_custom_logs( $request_body, 'ceretax-store-address-validation-request' );
     809
     810            $response = wp_remote_post(
     811                $api_url,
     812                array(
     813                    'method'      => 'POST',
     814                    'data_format' => 'body',
     815                    'body'        => $body,
     816                    'headers'     => array(
     817                        'Accept'       => 'application/json',
     818                        'x-api-key'    => $cere_tax_api_key,
     819                        'Content-Type' => 'application/json',
     820                    ),
     821                    'timeout'     => 60,
     822                )
     823            );
     824
     825            // add api response data in wc logs if log option enabled.
     826            $this->cwafc_wc_add_custom_logs( $response, 'ceretax-store-address-validation-response' );
     827
     828            if ( is_wp_error( $response ) ) {
     829                $notice_msg  = __( 'Request failed: ', 'ceretax' );
     830                $notice_msg .= $response->get_error_message();
     831                WC_Admin_Settings::add_error( $notice_msg );
     832                return false;
     833            } else {
     834                $data = wp_remote_retrieve_body( $response );
     835                $data = json_decode( $data, true );
     836            }
     837
     838            if ( isset( $data['results'][0]['errorMessages'] ) ) {
     839                $notice_msg  = __( 'Address not validate: ', 'ceretax' );
     840                $notice_msg .= $data['results'][0]['errorMessages'][0]['message'];
     841                WC_Admin_Settings::add_error( $notice_msg );
     842                return false;
     843            } else {
     844                $validated_address_details = $data['results'][0]['validatedAddressDetails'];
     845                if ( ! empty( $validated_address_details ) ) {
     846
     847                    // Get validated address.
     848                    $validated_address_line_1 = sanitize_text_field( wp_unslash( $data['results'][0]['validatedAddressDetails']['addressLine1'] ) );
     849                    $validated_address_line_2 = sanitize_text_field( wp_unslash( $data['results'][0]['validatedAddressDetails']['addressLine2'] ) );
     850                    $validated_city           = sanitize_text_field( wp_unslash( $data['results'][0]['validatedAddressDetails']['city'] ) );
     851                    $validated_postal_code    = sanitize_text_field( wp_unslash( strtoupper( $data['results'][0]['submittedAddressDetails']['postalCode'] ) ) );
     852
     853                    // Update validated address.
     854                    update_option( 'woocommerce_store_address', $validated_address_line_1 );
     855                    update_option( 'woocommerce_store_address_2', $validated_address_line_2 );
     856                    update_option( 'woocommerce_store_city', $validated_city );
     857                    update_option( 'woocommerce_store_postcode', $validated_postal_code );
     858
     859                    $notice_msg = __( 'Address validate successfully !', 'ceretax' );
     860                    WC_Admin_Settings::add_message( $notice_msg );
     861
     862                } else {
     863                    $notice_msg = $data['results'][0]['errorMessages'][0]['message'];
     864                    WC_Admin_Settings::add_error( $notice_msg );
     865                    return false;
     866                }
     867            }
     868        }
     869
     870        /**
     871         * Add WooCommerce logs for custom API call.
     872         *
     873         * @param object $log_data   pass the log data.
     874         * @param string $log_source pass the source of the log.
     875         * @return void
     876         */
     877        public function cwafc_wc_add_custom_logs( $log_data, $log_source ) {
     878            $enable_logging = get_option( CWAFC_PREFIX . '_cere_tax_enable_logging' );
     879            if ( 'yes' === $enable_logging ) {
     880                $logger = wc_get_logger();
     881                $logger->info( wp_json_encode( $log_data, JSON_PRETTY_PRINT ), array( 'source' => $log_source ) );
     882            }
     883        }
     884
     885        /**
     886         * Add custom style after total.
     887         *
     888         * @param int $order_id Order id.
     889         * @return void
     890         */
     891        public function action__cwafc_add_after_total_style( $order_id ) {
     892            $order = is_a( $order_id, 'WC_Order' ) ? $order_id : wc_get_order( $order_id );
     893
     894            if ( ! $order instanceof WC_Order ) {
     895                return;
     896            }
     897
     898            $ceretax_data_raw = get_post_meta( $order->get_id(), 'ceretax_data', true );
     899            if ( empty( $ceretax_data_raw ) ) {
     900                return;
     901            }
     902
     903            $ceretax_data = json_decode( $ceretax_data_raw );
     904            if ( isset( $ceretax_data->invoice->totalTaxInvoice ) && empty( $ceretax_data->invoice->totalTaxInvoice ) ) {
     905                return;
     906            }
     907
     908            $order_total_fees = $order->get_total_fees();
     909            if ( $order_total_fees < 0 ) {
     910                return;
     911            }
     912
     913            $cere_tax_name      = get_option( CWAFC_PREFIX . '_cere_tax_name', 'Tax' );
     914            $cere_tax_fee_exist = false;
     915            foreach ( $order->get_fees() as $fee_id => $fee ) {
     916                if ( $fee->get_name() === $cere_tax_name ) {
     917                    $cere_tax_fee_exist = true;
     918                    break;
     919                }
     920            }
     921
     922            if ( ! $cere_tax_fee_exist ) {
     923                return;
     924            }
     925
     926            if ( 0 < count( $order->get_coupon_codes() ) && 0 < $order->get_total_discount() ) {
     927                echo '<style>
     928                    .wc-order-data-row .wc-order-totals:first-of-type tr:nth-of-type(3) {
     929                        display: none !important;
     930                    }
     931                </style>';
     932            } else {
     933                echo '<style>
     934                    .wc-order-data-row .wc-order-totals:first-of-type tr:nth-of-type(2) {
     935                        display: none !important;
     936                    }
     937                </style>';
     938            }
     939        }
    737940    }
    738941
  • ceretax/trunk/inc/class-cwafc.php

    r3285848 r3320210  
    8181            // Woocommerce overide the plugin template.
    8282            add_filter( 'woocommerce_locate_template', array( $this, 'filter__cwafc_woo_adon_plugin_template' ), 10, 3 );
    83             // Add/Update order meta.
    84             add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'action__cwafc_checkout_field_update_order_meta' ) );
    8583            // Add tax calculation table on admin order edit page.
    86             // add_action( 'woocommerce_admin_order_items_after_shipping', array( $this, 'action__cwafc_add_tax_calculation_table_after_shipping' ), 99 );
    8784            add_action( 'add_meta_boxes', array( $this, 'action__cwafc_add_tax_calculation_table' ) );
    8885            // Add tax calculation on order place.
    8986            add_action( 'woocommerce_new_order', array( $this, 'action__cwafc_add_tax_calculation_when_order_placed' ), 10, 2 );
     87            // Clear the session after order placed.
     88            add_action( 'woocommerce_thankyou', array( $this, 'action__cwafc_clear_ceretax_session_after_order_placed' ), 10 );
    9089            // Add tax calculation on subscription renewal order place.
    9190            add_filter( 'wcs_renewal_order_created', array( $this, 'filter__cwafc_add_tax_calculation_when_renewal_order_placed' ), 10 );
     
    105104            // Clear Avatax from Ceretax renewal orders.
    106105            add_filter( 'wcs_new_order_created', array( $this, 'action__cwafc_clear_avatx_new_order_created' ), 10, 3 );
     106            // Include free fees in order totals.
     107            add_filter( 'woocommerce_get_order_item_totals_excl_free_fees', '__return_false' );
    107108        }
    108109
     
    121122                return;
    122123            }
     124
     125            $order            = wc_get_order( $order_id );
     126            $cere_tax_name    = get_option( CWAFC_PREFIX . '_cere_tax_name', 'Tax' );
     127            $has_cere_tax_fee = false;
     128
     129            // Check if the fee already exists in the order.
     130            foreach ( $order->get_fees() as $fee ) {
     131                if ( $fee->get_name() === $cere_tax_name ) {
     132                    $has_cere_tax_fee = true;
     133                    break;
     134                }
     135            }
     136
     137            // If the fee does not exist, return early.
     138            if ( ! $has_cere_tax_fee ) {
     139                return;
     140            }
    123141            $ceretax_data      = json_decode( $ceretax_data );
    124             $cere_tax_name     = get_option( CWAFC_PREFIX . '_cere_tax_name', 'Tax' );
    125142            $total_tax_invoice = $ceretax_data->invoice->totalTaxInvoice;
    126             $order             = wc_get_order( $order_id );
    127143            $fee_amount        = null;
    128144            $order_total_fees  = $order->get_total_fees();
    129             $order_remain_fees = 0;
     145            $order_remain_fees = null;
    130146
    131147            // If refunded order.
     
    142158                        $order_remain_fees = (float) $order_total_fees - (float) $fee_amount;
    143159                    }
    144                     $fee->save();
    145160                    break;
    146161                }
    147162            }
    148             if ( 0 < $order_remain_fees ) {
     163            if ( 0.0 !== $order_remain_fees && is_numeric( $order_remain_fees ) ) {
    149164                echo '<tr>';
    150165                echo '<td class="label">' . esc_html( __( 'Fees', 'ceretax' ) ) . ':</td>';
    151166                echo '<td width="1%"></td>';
    152                 echo '<td class="total">' . wc_price( $order_remain_fees, array( 'currency' => $order->get_currency() ) ) . '</td>';
     167                echo '<td class="total">' . wp_kses_post( wc_price( $order_remain_fees, array( 'currency' => $order->get_currency() ) ) ) . '</td>';
    153168                echo '</tr>';
    154169            }
     
    156171            echo '<td class="label">' . esc_html( $cere_tax_name ) . ':</td>';
    157172            echo '<td width="1%"></td>';
    158             echo '<td class="total">' . wc_price( $total_tax_invoice, array( 'currency' => $order->get_currency() ) ) . '</td>';
     173            echo '<td class="total">' . wp_kses_post( wc_price( $total_tax_invoice, array( 'currency' => $order->get_currency() ) ) ) . '</td>';
    159174            echo '</tr>';
    160175        }
     
    268283            $tax_rate = $this->cwafc_get_tax_rate_from_api( $address_1, $address_2, $country, $city, $state, $postcode );
    269284
    270             if ( ! empty( $tax_rate ) ) {
     285            if ( isset( $tax_rate ) && is_numeric( $tax_rate ) ) {
    271286                WC()->session->set( 'ceretax_rate', $tax_rate );
    272287                wp_send_json_success();
     
    410425                    );
    411426
    412                     if ( isset( $tax_rate ) && '' !== $tax_rate ) {
     427                    if ( isset( $tax_rate ) && is_numeric( $tax_rate ) ) {
    413428                        WC()->session->set( 'ceretax_rate', $tax_rate );
    414429                    }
     
    425440            ob_start();
    426441            wc_print_notice( $notice_msg, $notice_type );
    427             $notice = ob_get_clean();
    428             wp_send_json_success(
    429                 array(
    430                     'notice'           => $notice,
    431                     'validatedAddress' => $validated_address_details,
    432                 )
    433             );
     442            $notice       = ob_get_clean();
     443            $response_msg = array( 'notice' => $notice );
     444            if ( ! empty( $validated_address_details ) ) {
     445                $response_msg['validatedAddress'] = $validated_address_details;
     446            }
     447            wp_send_json_success( $response_msg );
    434448        }
    435449
     
    449463            if ( empty( $custom_tax_rate ) ) {
    450464                $cere_tax_name = get_option( CWAFC_PREFIX . '_cere_tax_name', 'Tax' );
    451                 $tax_str = '<tr>
     465                $tax_str       = '<tr>
    452466                    <th>' . esc_html( $cere_tax_name ) . '</th>
    453467                    <td data-title="Tax">
     
    465479         * Calculate cart tax
    466480         *
     481         * @param object $cart Cart object.
    467482         * @return mix
    468483         */
     
    478493
    479494            $checkout_data = WC()->session->get( 'checkout_data' );
    480 
    481             $ceretax_stau = WC()->session->get( 'ceretax_stau' );
     495            $ceretax_stau  = WC()->session->get( 'ceretax_stau' );
    482496
    483497            // phpcs:disable WordPress.Security.NonceVerification.Missing
    484 
    485498            if ( is_cart() && isset( $_POST['calc_shipping'] ) ) {
    486499
     
    510523                    $calc_shipping_postcode,
    511524                );
    512                 if ( isset( $tax_rate ) && '' !== $tax_rate ) {
     525                if ( isset( $tax_rate ) && is_numeric( $tax_rate ) ) {
    513526                    WC()->session->set( 'ceretax_rate', $tax_rate );
    514527                }
     
    522535                    $checkout_data['shipping_postcode']
    523536                );
    524                 if ( isset( $tax_rate ) && '' !== $tax_rate ) {
     537                if ( isset( $tax_rate ) && is_numeric( $tax_rate ) ) {
    525538                    WC()->session->set( 'ceretax_rate', $tax_rate );
    526539                }
     
    552565                );
    553566
    554                 if ( isset( $tax_rate ) && '' !== $tax_rate ) {
     567                if ( isset( $tax_rate ) && is_numeric( $tax_rate ) ) {
    555568                    WC()->session->set( 'ceretax_rate', $tax_rate );
    556569                }
    557570            }
    558 
    559571            // phpcs:enable WordPress.Security.NonceVerification.Missing
     572
    560573            $custom_tax_rate = WC()->session->get( 'ceretax_rate', 0 );
    561574            $cere_tax_name   = get_option( CWAFC_PREFIX . '_cere_tax_name', 'Tax' );
    562             if ( ! empty( $custom_tax_rate ) ) {
     575
     576            if ( isset( $custom_tax_rate ) && is_numeric( $custom_tax_rate ) ) {
    563577                $tax = $custom_tax_rate;
    564578
     
    581595
    582596                            if ( class_exists( 'WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $product_id ) ) {
    583                                 $filtered = array_filter( $ceretax_tmp_items_data, function ( $filtered_item ) use ( $product_id ) {
    584                                     return isset( $filtered_item->itemNumber ) && $filtered_item->itemNumber == $product_id;
    585                                 });
     597                                $filtered                 = array_filter(
     598                                    $ceretax_tmp_items_data,
     599                                    function ( $filtered_item ) use ( $product_id ) {
     600                                        return isset( $filtered_item->itemNumber ) && $filtered_item->itemNumber == $product_id; // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison, WordPress.NamingConventions.ValidVariableName
     601                                    }
     602                                );
    586603                                $line_item_revenue_amount = ! empty( $filtered ) ? reset( $filtered )->revenue : 0;
    587604                                $line_item_taxe_breaks    = ! empty( $filtered ) ? reset( $filtered )->taxes : array();
     
    589606                                if ( ! empty( $line_item_taxe_breaks ) && is_array( $line_item_taxe_breaks ) ) {
    590607                                    foreach ( $line_item_taxe_breaks as $key => $val ) {
    591                                         if ( ! empty( $val->calculationBaseAmt ) && ( $val->calculationBaseAmt >= $line_item_revenue_amount ) ) {
    592                                             $line_item_calc_base_amount      = $val->calculationBaseAmt;
    593                                             $line_item_recurring_tax_amount += $val->totalTax;
     608                                        if ( ! empty( $val->calculationBaseAmt ) && ( $val->calculationBaseAmt >= $line_item_revenue_amount ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName
     609                                            $line_item_calc_base_amount      = $val->calculationBaseAmt; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
     610                                            $line_item_recurring_tax_amount += $val->totalTax; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
    594611                                        }
    595612                                    }
     
    605622                    }
    606623                } else {
    607                     $cart->add_fee( $cere_tax_name, $tax, true, '' );
    608                 }
    609                 // WC()->cart->add_fee( $cere_tax_name, $tax, true, '' );
    610             } elseif ( is_checkout() ) {
    611                 // Ensure the fee is added even if the tax is 0.00.
    612                 $tax = 0;
    613                 WC()->cart->add_fee( $cere_tax_name, $tax, true, '' );
     624                    $cart->add_fee( $cere_tax_name, $tax, false );
     625                }
    614626            }
    615627        }
     
    639651                        'state'     => $order->get_shipping_state(),
    640652                        'postcode'  => $order->get_shipping_postcode(),
     653                        'country'   => $order->get_shipping_country(),
    641654                    );
    642655                } else {
     
    647660                        'state'     => $order->get_billing_state(),
    648661                        'postcode'  => $order->get_billing_postcode(),
     662                        'country'   => $order->get_billing_country(),
    649663                    );
    650664                }
     
    712726                        'state'     => $order->get_shipping_state(),
    713727                        'postcode'  => $order->get_shipping_postcode(),
     728                        'country'   => $order->get_shipping_country(),
    714729                    );
    715730                } else {
     
    720735                        'state'     => $order->get_billing_state(),
    721736                        'postcode'  => $order->get_billing_postcode(),
     737                        'country'   => $order->get_billing_country(),
    722738                    );
    723739                }
     
    811827
    812828        /**
    813          * Add tax calculation table after shipping
    814          *
    815          * @param string $order_id Order ID.
    816          * @return void
    817          */
    818         public function action__cwafc_add_tax_calculation_table_after_shipping( $order_id ) {
    819 
    820             $ceretax_data = json_decode( get_post_meta( $order_id, 'ceretax_data', true ) );
    821             $tax_table    = '';
    822             if ( is_object( $ceretax_data ) ) {
    823                 $ceretax_items_data = $ceretax_data->invoice->lineItems;
    824                 if ( ! empty( $ceretax_items_data ) ) {
    825                     $tax_table .= '<tr><td colspan="7"><table class="ceratax-tax-table">';
    826                     foreach ( $ceretax_items_data as $ceretax_item_data_key => $ceretax_item_data ) {
    827                         if ( ! empty( $ceretax_item_data->taxes ) ) {
    828                             $tax_table .= '<thead>
    829                                 <tr>
    830                                     <th>' . __( 'Authority', 'ceretax' ) . '</th>
    831                                     <th>' . __( 'Description', 'ceretax' ) . '</th>
    832                                     <th>' . __( 'Tax Type', 'ceretax' ) . '</th>
    833                                     <th>' . __( 'Tax Type Class', 'ceretax' ) . '</th>
    834                                     <th>' . __( 'Tax Type Ref', 'ceretax' ) . '</th>
    835                                     <th>' . __( 'Taxable', 'ceretax' ) . '</th>
    836                                     <th>' . __( 'Calc Base', 'ceretax' ) . '</th>
    837                                     <th>' . __( 'NonTaxable', 'ceretax' ) . '</th>
    838                                     <th>' . __( 'Exempt', 'ceretax' ) . '</th>
    839                                     <th>' . __( '% Taxable', 'ceretax' ) . '</th>
    840                                     <th>' . __( 'Tax rate', 'ceretax' ) . '</th>
    841                                     <th>' . __( 'Total Tax', 'ceretax' ) . '</th>
    842                                 </tr>
    843                             <thead>';
    844                             $tax_table .= '<tbody>';
    845                             foreach ( $ceretax_item_data->taxes as $ceretax_item ) {
    846                                 // phpcs:disable
    847                                 $tax_table .= '<tr>
    848                                     <td>' . $ceretax_item->taxAuthorityName . '</td>
    849                                     <td>' . $ceretax_item->description . '</td>
    850                                     <td>' . $ceretax_item->taxTypeDesc . '</td>
    851                                     <td>' . $ceretax_item->taxTypeClassDesc . '</td>
    852                                     <td>' . $ceretax_item->taxTypeRefDesc . '</td>
    853                                     <td>' . $ceretax_item->taxable . '</td>
    854                                     <td>' . $ceretax_item->calculationBaseAmt . '</td>
    855                                     <td>' . $ceretax_item->nonTaxableAmount . '</td>
    856                                     <td>' . $ceretax_item->exemptAmount . '</td>
    857                                     <td>' . $ceretax_item->percentTaxable . '</td>
    858                                     <td>' . $ceretax_item->rate . '</td>
    859                                     <td>' . $ceretax_item->totalTax . '</td>
    860                                 </tr>';
    861                                 // phpcs:enable
    862                             }
    863                             $tax_table .= '</tbody>';
    864                         }
    865                     }
    866                     $tax_table .= '</table></td></tr>';
    867                 }
    868             }
    869             echo wp_kses_post( $tax_table );
    870         }
    871 
    872         /**
    873829         * Add tax calculation table metaxbox.
    874830         *
     
    878834            global $post;
    879835            $ceretax_data = json_decode( get_post_meta( $post->ID, 'ceretax_data', true ) );
    880             if ( ! empty( $ceretax_data ) ) {
    881                 add_meta_box(
    882                     'cwafc_ceretax_metabox',
    883                     __( 'Ceretax Data', 'ceretax' ),
    884                     array( $this, 'cwafc_ceretax_metabox_callback' ),
    885                     array( 'shop_order', 'shop_subscription' ),
    886                     'normal',
    887                     'default'
    888                 );
    889             }
     836
     837            // If it's empty, return early.
     838            if ( empty( $ceretax_data ) ) {
     839                return;
     840            }
     841
     842            $order = wc_get_order( $post->ID );
     843            // If the order does not exist, return early.
     844            if ( ! $order ) {
     845                return;
     846            }
     847
     848            $cere_tax_name    = get_option( CWAFC_PREFIX . '_cere_tax_name', 'Tax' );
     849            $has_cere_tax_fee = false;
     850
     851            // Check if the fee exists in the order.
     852            foreach ( $order->get_fees() as $fee ) {
     853                if ( $fee->get_name() === $cere_tax_name ) {
     854                    $has_cere_tax_fee = true;
     855                    break;
     856                }
     857            }
     858
     859            // If the fee does not exist, return early.
     860            if ( ! $has_cere_tax_fee ) {
     861                return;
     862            }
     863
     864            add_meta_box(
     865                'cwafc_ceretax_metabox',
     866                __( 'Ceretax Data', 'ceretax' ),
     867                array( $this, 'cwafc_ceretax_metabox_callback' ),
     868                array( 'shop_order', 'shop_subscription' ),
     869                'normal',
     870                'default'
     871            );
    890872        }
    891873
     
    964946            $order_ceretax_data         = get_post_meta( $order_id, 'ceretax_data', true );
    965947            $order_avatax_data          = get_post_meta( $order_id, '_wc_avatax_tax_calculated', true );
     948            $traffic_source_data        = get_post_meta( $order_id, '_traffic_source', true );
    966949            // Check if order have other tax system.
    967950            if ( empty( $order_ceretax_data ) && ! empty( $order_avatax_data ) ) {
     951                return false;
     952            }
     953            if ( ! empty( $traffic_source_data ) && 'meta_insta_shopify' === $traffic_source_data ) {
    968954                return false;
    969955            }
     
    984970                    'state'     => $order->get_shipping_state(),
    985971                    'postcode'  => $order->get_shipping_postcode(),
     972                    'country'   => $order->get_shipping_country(),
    986973                );
    987974            } else {
     
    992979                    'state'     => $order->get_billing_state(),
    993980                    'postcode'  => $order->get_billing_postcode(),
     981                    'country'   => $order->get_billing_country(),
    994982                );
    995983            }
     
    998986                $res = $this->cwafc_calculate_cere_tax( $address_data, $order );
    999987            }
    1000 
    1001             if ( is_admin() && ! empty( $res ) ) {
     988            if ( isset( $res ) && is_numeric( $res ) ) {
    1002989                $fee_exists = false;
    1003990                foreach ( $order->get_fees() as $fee ) {
     
    10221009                // Recalculate order totals.
    10231010                $order->calculate_totals();
     1011                do_action( 'woocommerce_order_after_calculate_totals', $order->get_id(), $order );
    10241012                // Save the order.
    10251013                $order->save();
    10261014            }
     1015        }
     1016
     1017        /**
     1018         * Add address validation button on checkout page.
     1019         *
     1020         * @return void
     1021         */
     1022        public function action__cwafc_clear_ceretax_session_after_order_placed() {
     1023            WC()->session->__unset( 'ceretax_rate' );
     1024            WC()->session->__unset( 'ceretax_stau' );
     1025            WC()->session->__unset( 'checkout_data' );
    10271026        }
    10281027
     
    10381037            $order_ceretax_data         = get_post_meta( $renewal_order->get_id(), 'ceretax_data', true );
    10391038            $order_avatax_data          = get_post_meta( $renewal_order->get_id(), '_wc_avatax_tax_calculated', true );
     1039            $traffic_source_data        = get_post_meta( $renewal_order->get_id(), '_traffic_source', true );
    10401040            $order_contains_renewal     = ( function_exists( 'wcs_order_contains_renewal' ) ) ? wcs_order_contains_renewal( $renewal_order->get_id() ) : true;
    10411041            // Check if order have other tax system.
    10421042            if ( empty( $order_ceretax_data ) && ! empty( $order_avatax_data ) && ! $order_contains_renewal ) {
    10431043                return $renewal_order;
     1044            }
     1045            if ( ! empty( $traffic_source_data ) && 'meta_insta_shopify' === $traffic_source_data ) {
     1046                return false;
    10441047            }
    10451048            if ( 'yes' !== $cere_tax_post_transactions ) {
     
    10571060                    'state'     => $r_order->get_shipping_state(),
    10581061                    'postcode'  => $r_order->get_shipping_postcode(),
     1062                    'country'   => $r_order->get_shipping_country(),
    10591063                );
    10601064            } else {
     
    10651069                    'state'     => $r_order->get_billing_state(),
    10661070                    'postcode'  => $r_order->get_billing_postcode(),
     1071                    'country'   => $r_order->get_billing_country(),
    10671072                );
    10681073            }
     
    10711076                $res = $this->cwafc_calculate_cere_tax( $address_data, $r_order );
    10721077            }
    1073 
    1074             if ( isset( $res ) && null !== $res ) {
     1078            if ( isset( $res ) && is_numeric( $res ) ) {
    10751079                $fee_exists = false;
    10761080                foreach ( $r_order->get_fees() as $fee ) {
     
    11101114         */
    11111115        public function action__cwafc_order_after_calculate_totals( $and_taxes, $order ) {
    1112 
    11131116            // Check static flag first.
    11141117            if ( self::$is_calculating ) {
     
    11211124            $order_ceretax_data         = get_post_meta( $order->get_id(), 'ceretax_data', true );
    11221125            $order_avatax_data          = get_post_meta( $order->get_id(), '_wc_avatax_tax_calculated', true );
     1126            $traffic_source_data        = get_post_meta( $order->get_id(), '_traffic_source', true );
     1127
    11231128            // Check if order have other tax system.
    11241129            if ( empty( $order_ceretax_data ) && ! empty( $order_avatax_data ) ) {
    11251130                return false;
    11261131            }
     1132
     1133            if ( ! empty( $traffic_source_data ) && 'meta_insta_shopify' === $traffic_source_data ) {
     1134                return false;
     1135            }
     1136
    11271137            // Check order if refunded.
    11281138            if ( $order instanceof WC_Order_Refund || 'refunded' === $order->get_status() ) {
    11291139                return false;
    11301140            }
     1141
    11311142            if ( 'yes' !== $cere_tax_post_transactions ) {
    11321143                return false;
    11331144            }
     1145
    11341146            // Check for paid order.
    11351147            if ( $order && $order->is_paid() ) {
     
    11451157                    'state'     => $order->get_shipping_state(),
    11461158                    'postcode'  => $order->get_shipping_postcode(),
     1159                    'country'   => $order->get_shipping_country(),
    11471160                );
    11481161            } else {
     
    11531166                    'state'     => $order->get_billing_state(),
    11541167                    'postcode'  => $order->get_billing_postcode(),
     1168                    'country'   => $order->get_billing_country(),
    11551169                );
    11561170            }
     
    11601174                $res = $this->cwafc_calculate_cere_tax( $address_data, $order );
    11611175            }
    1162 
    1163             if ( ! empty( $res ) ) {
     1176            if ( isset( $res ) && is_numeric( $res ) ) {
    11641177                $fee_exists = false;
    11651178                foreach ( $order->get_fees() as $fee ) {
     
    11821195                    $order->add_item( $item_fee );
    11831196                }
    1184                 // Set the transient to prevent recursion.
    1185                 // set_transient( 'prevent_order_calculate_totals', true, 10 );
    1186 
    11871197                // Recalculate order totals.
    11881198                $order->calculate_totals();
    11891199
    1190                 // Remove the transient after recalculation.
    1191                 // delete_transient( 'prevent_order_calculate_totals' );
    1192 
    11931200                // Save the order.
    11941201                $order->save();
     
    12091216            $order  = wc_get_order( $order_id );
    12101217            $refund = wc_get_order( $refund_id );
     1218
     1219            $traffic_source_data = get_post_meta( $order_id, '_traffic_source', true );
     1220            if ( ! empty( $traffic_source_data ) && 'meta_insta_shopify' === $traffic_source_data ) {
     1221                return false;
     1222            }
    12111223
    12121224            // Save ceretax line item tax in order line items.
     
    12161228                $line_item_json   = stripslashes( sanitize_text_field( wp_unslash( $_POST['line_item_tax_totals'] ) ) );
    12171229                $line_item_data   = json_decode( $line_item_json, true );
    1218                 $line_item_result = array_map( function( $item ) {
    1219                     return $item['undefined'];
    1220                 }, $line_item_data );
    1221                 $line_item_result = array_filter( $line_item_result, function( $value ) {
    1222                     return ! empty( $value );
    1223                 });
     1230                $line_item_result = array_map(
     1231                    function ( $item ) {
     1232                        return $item['undefined'];
     1233                    },
     1234                    $line_item_data
     1235                );
     1236                $line_item_result = array_filter(
     1237                    $line_item_result,
     1238                    function ( $value ) {
     1239                        return ! empty( $value );
     1240                    }
     1241                );
    12241242
    12251243                // For order items.
     
    12661284                    'state'     => $order->get_shipping_state(),
    12671285                    'postcode'  => $order->get_shipping_postcode(),
     1286                    'country'   => $order->get_shipping_country(),
    12681287                );
    12691288            } else {
     
    12741293                    'state'     => $order->get_billing_state(),
    12751294                    'postcode'  => $order->get_billing_postcode(),
     1295                    'country'   => $order->get_billing_country(),
    12761296                );
    12771297            }
     
    12801300                $res = $this->cwafc_calculate_cere_tax_refunded( $address_data, $refund, $order );
    12811301            }
    1282 
    12831302            $is_run = false;
    1284             if ( $is_run && ! empty( $res ) ) {
     1303            if ( $is_run && isset( $res ) && is_numeric( $res ) ) {
    12851304                $fee_exists = false;
    12861305                foreach ( $order->get_fees() as $fee ) {
     
    13701389            }
    13711390
     1391            // Get store address.
     1392            $store_address         = get_option( 'woocommerce_store_address' );
     1393            $store_address2        = get_option( 'woocommerce_store_address_2' );
     1394            $store_city            = get_option( 'woocommerce_store_city' );
     1395            $store_postcode        = get_option( 'woocommerce_store_postcode' );
     1396            $store_default_country = get_option( 'woocommerce_default_country' );
     1397
     1398            // Get country and state separately.
     1399            list( $store_country, $store_state ) = array_pad( explode( ':', $store_default_country ), 2, '' );
     1400
    13721401            // Caclulate tax if only CereTax calculation option is not enabled.
    13731402            if ( ! $this->cwafc_is_ceretax_enable() ) {
     
    13851414            $item_count            = 0;
    13861415            $order_status          = $order->get_status();
    1387             $created_via           = $order->get_meta( '_created_via' );
     1416            $created_via           = $order->get_created_via();
    13881417            $order_parent_id       = $order->get_parent_id();
    13891418            $order_type            = $order->get_type();
     
    14401469                    ),
    14411470                    'situs'              => array(
    1442                         'taxSitusRule'  => 'T',
    1443                         'shipToAddress' => array(
     1471                        'taxSitusRule'    => 'T',
     1472                        'shipFromAddress' => array(
     1473                            'addressLine1'    => $store_address,
     1474                            'addressLine2'    => $store_address2,
     1475                            'city'            => $store_city,
     1476                            'state'           => $store_state,
     1477                            'postalCode'      => $store_postcode,
     1478                            'country'         => $store_country,
     1479                            'validateAddress' => $cere_tax_validate_address_on_transaction,
     1480                        ),
     1481                        'shipToAddress'   => array(
    14441482                            'addressLine1'    => $address_data['address_1'],
    14451483                            'addressLine2'    => $address_data['address_2'],
     
    14471485                            'state'           => $address_data['state'],
    14481486                            'postalCode'      => $address_data['postcode'],
     1487                            'country'         => $address_data['country'],
    14491488                            'validateAddress' => $cere_tax_validate_address_on_transaction,
    14501489                        ),
     
    14711510                    );
    14721511                }
    1473                 $item_count++;
     1512                $item_count++; // phpcs:ignore Generic.CodeAnalysis.UnusedIncrement, WordPress.PHP.PreIncrement.PreIncrement
    14741513            }
    14751514
     
    15181557
    15191558            $ceretax_stau = get_post_meta( $order->get_id(), 'ceretax_stau_' . $order->get_id(), true );
    1520 
    1521             // if ( ( empty( $ceretax_stau ) || 'not_found' === $ceretax_stau ) && ! empty( $body ) && ! empty( $cere_tax_api_key ) ) {
    15221559            if ( empty( $ceretax_stau ) && ! empty( $body ) && ! empty( $cere_tax_api_key ) ) {
    1523 
    15241560                $response = wp_remote_post(
    15251561                    $api_url,
     
    15701606                $order_line_item_count     = get_post_meta( $order->get_id(), 'ceretax_order_line_item_count', true );
    15711607
    1572                 if ( 'true' === $wps_upsell_order_started && 'true' !== $wps_separate_upsell_order && $order_line_item_count != count( $order->get_items() ) ) {
     1608                if ( 'true' === $wps_upsell_order_started && 'true' !== $wps_separate_upsell_order && count( $order->get_items() ) != $order_line_item_count ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
    15731609
    15741610                    update_post_meta( $order->get_id(), 'wps_separate_upsell_order', 'true' );
     
    16111647
    16121648                } else {
    1613                    
    16141649                    $response = wp_remote_post(
    16151650                        $api_url . '?systemTraceAuditNumber=' . $ceretax_stau,
     
    17011736            }
    17021737
     1738            // Get store address.
     1739            $store_address         = get_option( 'woocommerce_store_address' );
     1740            $store_address2        = get_option( 'woocommerce_store_address_2' );
     1741            $store_city            = get_option( 'woocommerce_store_city' );
     1742            $store_postcode        = get_option( 'woocommerce_store_postcode' );
     1743            $store_default_country = get_option( 'woocommerce_default_country' );
     1744
     1745            // Get country and state separately.
     1746            list( $store_country, $store_state ) = array_pad( explode( ':', $store_default_country ), 2, '' );
     1747
    17031748            // Caclulate tax if only CereTax calculation option is not enabled.
    17041749            if ( ! $this->cwafc_is_ceretax_enable() ) {
     
    17141759            $item_count            = 0;
    17151760            $order_status          = $order->get_status();
    1716             $created_via           = $order->get_meta( '_created_via' );
     1761            $created_via           = $order->get_created_via();
    17171762            $order_parent_id       = $order->get_parent_id();
    17181763            $order_type            = $order->get_type();
     
    17581803                    ),
    17591804                    'situs'              => array(
    1760                         'taxSitusRule'  => 'T',
    1761                         'shipToAddress' => array(
     1805                        'taxSitusRule'    => 'T',
     1806                        'shipFromAddress' => array(
     1807                            'addressLine1'    => $store_address,
     1808                            'addressLine2'    => $store_address2,
     1809                            'city'            => $store_city,
     1810                            'state'           => $store_state,
     1811                            'postalCode'      => $store_postcode,
     1812                            'country'         => $store_country,
     1813                            'validateAddress' => $cere_tax_validate_address_on_transaction,
     1814                        ),
     1815                        'shipToAddress'   => array(
    17621816                            'addressLine1'    => $address_data['address_1'],
    17631817                            'addressLine2'    => $address_data['address_2'],
     
    17651819                            'state'           => $address_data['state'],
    17661820                            'postalCode'      => $address_data['postcode'],
     1821                            'country'         => $address_data['country'],
    17671822                            'validateAddress' => $cere_tax_validate_address_on_transaction,
    17681823                        ),
     
    17891844                    );
    17901845                }
    1791                 $item_count++;
     1846                $item_count++; // phpcs:ignore WordPress.PHP.PreIncrement.PreIncrement
    17921847            }
    17931848
     
    19361991            $shipping_zones                           = WC_Shipping_Zones::get_zones();
    19371992
     1993            // Get store address.
     1994            $store_address         = get_option( 'woocommerce_store_address' );
     1995            $store_address2        = get_option( 'woocommerce_store_address_2' );
     1996            $store_city            = get_option( 'woocommerce_store_city' );
     1997            $store_postcode        = get_option( 'woocommerce_store_postcode' );
     1998            $store_default_country = get_option( 'woocommerce_default_country' );
     1999
     2000            // Get country and state separately.
     2001            list( $store_country, $store_state ) = array_pad( explode( ':', $store_default_country ), 2, '' );
     2002
    19382003            // Caclulate tax if only CereTax calculation option is not enabled.
    19392004            if ( ! $this->cwafc_is_ceretax_enable() ) {
     
    20042069                        ),
    20052070                        'situs'              => array(
    2006                             'taxSitusRule'  => 'T',
    2007                             'shipToAddress' => array(
     2071                            'taxSitusRule'    => 'T',
     2072                            'shipFromAddress' => array(
     2073                                'addressLine1'    => $store_address,
     2074                                'addressLine2'    => $store_address2,
     2075                                'city'            => $store_city,
     2076                                'state'           => $store_state,
     2077                                'postalCode'      => $store_postcode,
     2078                                'country'         => $store_country,
     2079                                'validateAddress' => $cere_tax_validate_address_on_transaction,
     2080                            ),
     2081                            'shipToAddress'   => array(
    20082082                                'addressLine1'    => $shipping_address_1,
    20092083                                'addressLine2'    => $shipping_address_2,
     
    20112085                                'state'           => $state,
    20122086                                'postalCode'      => $postcode,
     2087                                'country'         => $country,
    20132088                                'validateAddress' => $cere_tax_validate_address_on_transaction,
    20142089                            ),
     
    20942169
    20952170                if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
    2096                     WC()->session->set( 'ceretax_data', $data );
    20972171                    return $data['invoice']['totalTaxInvoice'];
    20982172                }
     
    21322206
    21332207                if ( isset( $data['invoice']['totalTaxInvoice'] ) ) {
    2134                     WC()->session->set( 'ceretax_data', $data );
    21352208                    return $data['invoice']['totalTaxInvoice'];
    21362209                }
     
    21622235            // Caclulate tax if only CereTax calculation option is not enabled.
    21632236            $cere_tax_enable = get_option( CWAFC_PREFIX . '_cere_tax_enable' );
    2164             return 'yes' === $cere_tax_enable ? true : false;   
     2237            return 'yes' === $cere_tax_enable ? true : false;
    21652238        }
    21662239
     
    22582331        public function action__cwafc_clear_avatx_new_order_created( $order, $subscription, $type ) {
    22592332
    2260             if ( ! class_exists( 'WC_AvaTax_Order_Handler' ) && ! empty( $subscription->get_meta( '_wc_avatax_tax_calculated' ) ) && $subscription->get_meta( '_wc_avatax_tax_calculated' ) == 'yes' && empty( $order->get_meta( '_wc_avatax_tax_calculated' ) ) ) {
     2333            if ( ! class_exists( 'WC_AvaTax_Order_Handler' ) && ! empty( $subscription->get_meta( '_wc_avatax_tax_calculated' ) ) && 'yes' == $subscription->get_meta( '_wc_avatax_tax_calculated' ) && empty( $order->get_meta( '_wc_avatax_tax_calculated' ) ) ) {
    22612334
    22622335                // Clear avatax data from order item meta.
     
    22962369                    }
    22972370                }
    2298                 if ( $require_recalculation == 1 ) {
     2371                if ( 1 == $require_recalculation ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
    22992372                    $order->save();
    23002373                    $order->calculate_totals();
     
    23062379}
    23072380
    2308 /**
    2309  * CWAFC create function to return instance.
    2310  *
    2311  * @return object
    2312  */
    23132381if ( ! function_exists( 'cwafc' ) ) {
     2382    /**
     2383     * CWAFC create function to return instance.
     2384     *
     2385     * @return object
     2386     */
    23142387    function cwafc() {
    23152388        return CWAFC::get_instance();
  • ceretax/trunk/readme.txt

    r3285848 r3320210  
    33Tags: sales tax, taxes, tax, tax calculation, sales tax compliance
    44Requires at least: 4.0.1
    5 Tested up to: 6.6.1
     5Tested up to: 6.8.1
    66Requires PHP: 7.4
    7 Stable tag: 1.4.2
     7Stable tag: 1.4.3
    88License: GNU General Public License v3.0
    99URI: http://www.gnu.org/licenses/gpl-3.0.html
    1010WC requires at least: 7.0.0
    11 WC tested up to: 9.2.0
     11WC tested up to: 9.9.5
    1212
    1313Simplify sales tax complexity with CereTax for WooCommerce.
     
    3535
    3636== Changelog ==
     37
     38= 1.4.3 =
     39* fix - Resolved PHP warning errors
     40* add - Included 'shipFromAddress' parameter in the ceretax API request
    3741
    3842= 1.4.2 =
     
    9094== Upgrade Notice ==
    9195
     96= 1.4.3 =
     97* fix - Resolved PHP warning errors
     98* add - Included 'shipFromAddress' parameter in the ceretax API request
     99
    92100= 1.4.2 =
    93101* fix - Tax box now hidden in refunds to avoid confusion, as it no longer reflects accurate post-refund data
  • ceretax/trunk/woocommerce/cart/shipping-calculator.php

    r3219747 r3320210  
    1313 * @see     https://woocommerce.com/document/template-structure/
    1414 * @package WooCommerce\Templates
    15  * @version 7.0.1
     15 * @version 9.7.0
    1616 */
    1717
Note: See TracChangeset for help on using the changeset viewer.