taxjar / taxjar-woocommerce-plugin

WooCommerce Sales Tax Plugin by TaxJar
http://www.taxjar.com/woocommerce-sales-tax-plugin/
GNU General Public License v2.0
28 stars 29 forks source link

Fitler request: TaxJar_Order_Record::should_sync() #261

Open devinsays opened 1 month ago

devinsays commented 1 month ago

We'd like to prevent TaxJar sync for any orders created more than 180 days ago. We're making this work by using the taxjar_order_sync_data filter, but it would be great to have a filter directly on the should_sync method.

Here's the code workaround we implemented to better explain the use case:

add_filter( 'taxjar_order_sync_data', [ $this, 'maybe_prevent_old_order_sync' ], 10, 2 );
/**
     * When TaxJar syncs orders to record tax, we don't want it syncing orders that are
     * more than 180 days old. At this point, tax should have already been recorded.
     *
     * This hooks into the taxjar_order_sync_data filter, which is called when building
     * the data that TaxJar will use to sync the order; if it's missing something critical,
     * such as destination country, it will not sync the order.
     *
     * @see taxjar-simplified-taxes-for-woocommerce/includes/class-taxjar-order-record.php
     * @see \TaxJar_Order_Record::should_sync(), \TaxJar_Order_Record::get_data_from_object()
     *
     * @param array     $taxjar_order_data
     * @param \WC_Order $order
     * @return array
     */
    public function maybe_prevent_old_order_sync( $taxjar_order_data, $order ) {
        $days_old = absint( ( time() - strtotime( $order->get_date_created() ) ) / DAY_IN_SECONDS );
        if ( $days_old > 180 ) {
            // Set to blank value to trigger the should_sync check to return false.
            $taxjar_order_data['to_country'] = '';
        }

        return $taxjar_order_data;
    }

}
ahumulescu commented 1 month ago

Hello,

Thank you for reaching out to us!

Have you also had a look at the 'taxjar_should_sync_order' filter?

Here are some references: https://github.com/taxjar/taxjar-woocommerce-plugin/blob/master/includes/abstract-class-taxjar-record.php#L175 https://github.com/taxjar/taxjar-woocommerce-plugin/blob/master/includes/class-wc-taxjar-transaction-sync.php#L232