gerche
Forum Replies Created
-
Forum: Plugins
In reply to: [WooCommerce] How to choose the tax class for shippingHi @shameemreza,
Thanks for the feedback and apologies for going out of the support scope with my follow up question.
The “tax_class” attribute didn’t seem to exist on the $rate object but I did find a “taxes” attribute with an array containing the total tax cost. This allowed me to calculate the actual shipping tax cost myself and overwrite it. I’ll post the code incase anybody else would ever need it:
add_filter( 'woocommerce_package_rates', 'custom_shipping_tax_class', 10, 2 ); function custom_shipping_tax_class( $rates, $package ) { $tax_class_6 = false; // Check each cart item for their tax class foreach ( $package['contents'] as $cart_item ) { $product = $cart_item['data']; $tax_status = $product->get_tax_status(); if ( 'taxable' === $tax_status ) { $product_tax_class = $product->get_tax_class(); if ( 'gereduceerd-tarief' === $product_tax_class ) { $tax_class_6 = true; } } } $shipping_tax_class = ''; if ( $tax_class_6 ) { $shipping_tax_class = 'gereduceerd-tarief'; } if ( ! empty( $shipping_tax_class ) ) { foreach ( $rates as $rate_id => $rate ) { if ( 'flat_rate' === $rate->method_id ) { $tax_rates = WC_Tax::get_rates( $shipping_tax_class ); // Calculate the tax based on the cost $taxes = WC_Tax::calc_shipping_tax( $rate->cost, $tax_rates ); $rate->taxes = $taxes; // Update the rate in the array $rates[ $rate_id ] = $rate; } } } return $rates; }
Thanks for the help!
- This reply was modified 8 months, 2 weeks ago by gerche.
Forum: Plugins
In reply to: [WooCommerce] How to choose the tax class for shippingHi @shameemreza,
Thank you for your reply.
I checked out the plugins and it seems they can’t exactly achieve this (unless I’m missing something).
Non of them allow me to manage the logic of the shipping tax class selection.The advanced shipping plugin has a “tax” option but this only uses the total tax amount not the classes of the products:
And the table rate plugin also has no option to select the tax class. Even if this dropdown had the option it would allow me to set the cost of the shipping and not the tax class on it:
I was hoping to perhaps achieve the result with some php code but am not sure how to overwrite the shipping tax class after checking if there is an item with the reduced tax in the chart. I got the following code working and logging “Applying 6% tax class to shipping.” but am missing the last step. Any ideas?
add_action( 'woocommerce_cart_calculate_fees', 'custom_shipping_tax_class', 10, 1 ); function custom_shipping_tax_class( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { error_log('Exiting Custom Shipping Tax Class function due to admin page or non-AJAX call.'); return; } $tax_class_6 = false; // Check each cart item for their tax class foreach ( $cart->get_cart() as $cart_item ) { $product = $cart_item['data']; $tax_status = $product->get_tax_status(); if ( 'taxable' === $tax_status ) { $product_tax_class = $product->get_tax_class(); error_log('Product ID ' . $product->get_id() . ' is taxable with tax class: ' . $product_tax_class); if ( 'gereduceerd-tarief' === $product_tax_class ) { $tax_class_6 = true; } } } error_log('Tax Class 6: ' . ($tax_class_6 ? 'true' : 'false')); // Determine the tax class for shipping based on the cart items $shipping_tax_class = ''; if ( $tax_class_6 ) { $shipping_tax_class = 'gereduceerd-tarief'; } if ( ! empty( $shipping_tax_class ) ) { error_log('Applying 6% tax class to shipping.'); // Apply the reduced tax class to shipping } else { error_log('No shipping tax class applied.'); } }