• Resolved mirwordpress

    (@mirwordpress)


    is there a way to override woo’s free shipping, for a specific shipping class. I want it to revert to Flat rate, and use a rate set within there for that class.

    I have a function to hide it entirely, but this is not exactly what i want ( see below ). I also have a function to reset the free shipping amount for a class, and rename it to the type of shipping – but this affects the tax calculation ( see my previous question today )

    
    add_filter( 'woocommerce_package_rates', 'hide_free_shipping_conditionally', 10, 2 );
    function hide_free_shipping_conditionally( $rates, $package ) {
        // Define the targeted shipping classes slugs (not names)
        $targeted_classes = array( 'kern-scales' ); 
        
        $found = $others = false; // Initializing
        
        // Loop through cart items for current shipping package
        foreach( $package['contents'] as $item ) {
            if ( in_array( $item['data']->get_shipping_class(), $targeted_classes ) ) {
                $found = true;
            } else {
                $others = true;
            }
        }
        
        // When there are only items from specific shipping classes exclusively
        if ( $found && ! $others ) {
            // Loop through shipping methods for current shipping package
            foreach( $rates as $rate_key => $rate ) {
                // Targetting Free shipping methods
                if ( 'free_shipping' === $rate->method_id ) {
                    unset($rates[$rate_key]); // Remove free shipping option(s)
                   // set($rates[$rate_key])->method_id == 'flat_rate';
                 
                } 
            }
        }
        return $rates;
    }
    • This topic was modified 2 years, 10 months ago by mirwordpress.
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Function to override Woocommerce’s inbuilt ‘free shipping’’ is closed to new replies.