• Hello,

    At the moment all products have the same delivery cost: €1 for each kilometer till max €80.
    But now I want to ship out small products for a flat rate.

    Is this possible ?

    Because when I add flat rate, people can choose between 2 prices.
    If they add small parts and a large bike, they can choose the flat rate of €5.5.

    I don’t want to drive 200km to deliver a bike for €5.5

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi there, I’m having exact the same problem. Any solution yet?

    Hey there,

    I also had that issue and used as workaround a little function.

    Create an own shipping class for WooReer and asign all WooReer shippable articles to that class. We use that shipping class as an indicator which articles are we dealing with

    // parse cart articles for shipping classes 'your_custom_classname' and hide flat rate delivery in that case - vice versa hide the WooReer method in case the specified class was not found
    
    add_filter( 'woocommerce_package_rates', 'hide_shipping_when_class_is_in_cart', 100, 2 );
    function hide_shipping_when_class_is_in_cart( $rates, $package ) {
        // Initialisation 
        $remove_shipping_methods = array( 'flat_rate:5' );      // your shipping method id comes here ( find it's name by it's css class and add ':' between string and its id )
        $shipping_classes = array( 'your_custom_class_name' );             // your WooReer class the id is eual to your slug: /wp-admin/admin.php?page=wc-settings&tab=shipping&section=classes
        $class_exists = false;
    
        foreach( $package['contents'] as $cart_item ){
            if( in_array( $cart_item['data']->get_shipping_class(),  $shipping_classes ) ) {
                $class_exists = true;
                break; // Stop the loop
            }
        }
        if( $class_exists ){
            // var_dump ($rates);
            foreach ($remove_shipping_methods as $shipping_method) {
                if ( isset ( $rates[$shipping_method] ) ) {
                    unset( $rates[$shipping_method] );
                }
            }
        }else{
            if ( isset( $rates['wcsdm:7'] ) ) {     // wcsdm:7 is the id for "WooReer" Plugins shipping method
                unset( $rates['wcsdm:7'] );
            }
        }
    
        return $rates;
    }

    Only thing you have to find out is the propper shipping method ID you want to disable. To do this maybe that articles helps (also see comment at the code section):
    https://www.bolderelements.net/support/knowledgebase/finding-the-shipping-options-id/

    Of course you can change the behavior of the code to your needs. At example how to behave when two different types of items are in cart? In my case I’m just going to deliver the smaller package together with the larger mile-based package in case two different types of articles are merged up in cart.

    Idea and source of the code:
    https://stackoverflow.com/questions/49284009/disable-free-shipping-based-on-shipping-classes-in-woocommerce

    https://stackoverflow.com/questions/45068536/unsetting-woocommerce-shipping-method-based-on-cart-items-shipping-classes/45069590#45069590

    Hope that helps :)!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Flat rate for certain shipping class’ is closed to new replies.