• Resolved mirwordpress

    (@mirwordpress)


    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;
    }
    • This topic was modified 2 years, 10 months ago by mirwordpress.
Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Support Paulo P – a11n

    (@paulostp)

    Hello!

    If I understood correctly, you want to have a different shipping charge for a specific product or group of products, but that amount is not being taxed at checkout.

    WooCommerce should be handling that correctly. If you haven’t already tried this, I recommend you first create the shipping class for that product in WooCommerce > Settings > Shipping > Shipping classes. Then, edit the Flat rate shipping and there, add the shipping price difference for that new class.

    Finally, you would select the aforementioned shipping class in the product page.

    Check this doc for additional details: https://woocommerce.com/document/product-shipping-classes/

    This should trigger the new shipping cost when adding that product, and also account for it in the “Tax” amount at checkout.

    Let us know how it goes.

    Thanks

    Thread Starter mirwordpress

    (@mirwordpress)

    Thank you Paulo.

    Can i edit the flat rate so that it has two values, the normal flat rate ( 7.50 ), and the one for the kern shipping class ( 20 ) ?

    I do have the shipping class set up for those products already.

    Hi @mirwordpress

    You can add more than one flat rate shipping option.

    You may find this article helpful: https://cinchws.com/how-to-set-up-multiple-flat-rate-shipping-options-in-woocommerce/

    Thread Starter mirwordpress

    (@mirwordpress)

    thank you.

    so how can I make sure my site will apply this new flat rate to the specific shipping class?

    the site also has ‘free shipping’ over a certain price, so it just seems to default to this regardless.

    This is the main problem, and that’s why I did my original function. It seems regardless of what i set in the shipping classes, if the order amount is above the free shipping threshold of 85, the shipping is defaulted to ‘free shipping ‘

    • This reply was modified 2 years, 10 months ago by mirwordpress.
    Thread Starter mirwordpress

    (@mirwordpress)

    i think i need a solution to override free shipping for that specific class & fall to flat rate where i have a cost in there for that specific shipping class.. can you guys help ?

    Hi @mirwordpress

    Can i edit the flat rate so that it has two values, the normal flat rate ( 7.50 ), and the one for the kern shipping class ( 20 ) ?

    In your flat rate shipping setting page, you can set a price specific to the Kern shipping class.

    Link to image: https://snipboard.io/NW7XSk.jpg

    the site also has ‘free shipping’ over a certain price, so it just seems to default to this regardless.

    The default shipping method will be the top in your shipping zone. You can change the default shipping method by dragging and dropping the three bard on the left.

    Link to image: https://snipboard.io/ItLs9G.jpg

    i think i need a solution to override free shipping for that specific class & fall to flat rate where i have a cost in there for that specific shipping class..

    There isn’t a built-in option to do this. But it can be done by either using a plugin like conditional shipping or applying custom code like this.

    Thread Starter mirwordpress

    (@mirwordpress)

    Hi May
    thank you for your reply.

    I have used the custom code link for your reply.

    can you help me with a small part of it.

    where it unsets the free shipping, i would then like to set the flat_rate.

    unset( $rates[‘free_shipping:3’] );

    so something like set ($rates[ ‘flat_rate’ ]);

    ? thanks again

    Thread Starter mirwordpress

    (@mirwordpress)

    I have tried this:
    WC()->shipping->set(‘chosen_shipping_methods’, array($flat_rate_kern) );

    does not work. set_method_id() only works within a new instance of an order.

    Mirko P.

    (@rainfallnixfig)

    Hi @mirwordpress,

    Thanks for getting back to us.

    We are unable to provide support for customizations per our Support Policy.

    If you need assistance with the code and/or customization, you can reach out to a web developer or one of our experts – https://woocommerce.com/customizations/.

    Let us know if you have more questions.

    Thread Starter mirwordpress

    (@mirwordpress)

    thank you but its not really a customization, i think the plugin should do this already. Its more of a problem with it’s functionality. Stack exchange don’t even help with woocommerce anymore.

    Hi @mirwordpress

    Since this functionality is not there inbuilt, you’ll need to consider custom coding.

    You may be able to find someone willing to volunteer their time and knowledge in the free PHP support forum at PHP Builder or within the Advanced WooCommerce group on Facebook.

    Additionally, as Mirko already stated, for assistance with customization or development with your site, we recommend reaching out to someone on our customizations page.

    I hope this points you in the right direction.

    We’ve not seen any activity on this thread for a while, so I’m marking this thread as closed. If you have further questions, please feel free to open a new topic.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Checkout Shipping tax calculation problem’ is closed to new replies.