Checkout Shipping tax calculation problem
-
So I needed to set up a shipping charge for a specific class on my website. ( Kern Scales, €20 )
that worked fine. The problem I have is that rather than the VAT/TAX on shipping being added to the Tax total of the product, it is being added to the product total & not being accounted for properly. I have been searching for the right hook to use, to update the tax with the VAT on the shipping charge, but I cannot seem to figure it out. I think my problem is in how i set the original shipping rate for this class. I just overwrite the ‘free-shipping’ label to Kern Scales with the new shipping charge.
if there is a hook that i can use to go in to the section where i set the shipping charge to 20, please let me know. All i want to do, is add the VAT/TAX to the actual tax total of the complete order.
If anyone has any ideas pls let me know, thanks
This is my code that i have in functions.php
<?php // Set a special shipping cost for Kern-scales shipping class and display a message add_filter('woocommerce_package_rates', 'set_kern_shipping_rate', 10, 2); function set_kern_shipping_rate( $rates, $package ){ if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return $rates; // Kern shipping class slug $shipping_class_slug = 'kern-scales'; $found = false; // Loop through cart items and checking for kern-scales shipping class foreach( $package['contents'] as $cart_item ) { if( $cart_item['data']->get_shipping_class() == $shipping_class_slug ) $found = true; } // Set shipping costs to 20 euro if shipping class is found if( $found ){ foreach ( $rates as $rate_key => $rate ){ $has_taxes = false; // Targetting "flat rate" and local pickup, & free shipping if( 'flat_rate' === $rate->method_id || 'local_pickup' === $rate->method_id || 'free_shipping' === $rate->method_id ){ // Set the cost to 20 $rates[$rate_key]->cost = 20; $has_taxes = true; $taxes = $rates[$rate_key]->cost = 20 * 1.23; $rates[$rate_key]->taxes = $taxes; } } // Clear duplicated notices wc_clear_notices(); // notice to be displayed on checkout page /*wc_add_notice( __('<H2>*THERE IS A CHARGE OF €20 FOR SHIPPING FOR ALL KERN SCALES.</H2>', 'woocommerce'), 'notice' ); */ } return $rates; } add_action( 'woocommerce_before_checkout_form', 'display_custom_shipping_message_in_checkout' ); function display_custom_shipping_message_in_checkout(){ // HERE set your shipping class slug $shipping_class_slug = 'kern-scales'; $found = false; // Loop through cart items and checking for the specific defined shipping class foreach( WC()->cart->get_cart() as $cart_item ) { if( $cart_item['data']->get_shipping_class() == $shipping_class_slug ) $found = true; } if( $found ){ // Display a custom notice wc_print_notice( __('<H2>*THERE IS A CHARGE OF €20 FOR SHIPPING FOR ALL KERN SCALES.</H2>', 'woocommerce'), 'notice' ); } } /****************end of shipping func */ add_filter('woocommerce_package_rates', 'change_shipping_method_name_based_on_shipping_class', 50, 2); function change_shipping_method_name_based_on_shipping_class($rates, $package){ //set the shipping class for Kern-scales - found ID in database it is an array key $shipping_class_id = 15866; $found = false; // Check for the Kern-scales shipping class in cart items foreach( $package['contents'] as $cart_item ) { if( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ){ $found = true; break; } } // Loop through shipping methods foreach ( $rates as $rate_key => $rate ) { // Change "free shipping" Shipping method label name if ( 'free_shipping' === $rate->method_id ) { if( $found ) $rates[$rate_key]->label = __( 'Kern Scales Shipping *€20 +VAT', 'woocommerce' ); else $rates[$rate_key]->label = __( 'Free Shipping', 'woocommerce' ); } } return $rates; }
- The topic ‘Checkout Shipping tax calculation problem’ is closed to new replies.