• Resolved robinjoshua

    (@robinjoshua)


    Hello,

    My current filter (see below) to unset shipping methods is not working in the current version of WooCommerce. I was wondering whether someone could help me out, since I am not really familiar with Php.

    With kind regards,

    Robin

    // add filter and function to hide method

    add_filter( ‘woocommerce_available_shipping_methods’, ‘custom_shipping_methods’ , 10, 1 );

    function custom_shipping_methods( $available_methods ){

    if ( cart_has_product_with_orange_cats() ) {

    foreach($available_methods as $key => $method){

    if($key == ‘local_pickup’){

    continue;
    }

    unset($available_methods[$key]);

    }

    // remove the rate you want
    }

    // return the available methods without the one you unset.

    return $available_methods;
    }

    https://www.ads-software.com/plugins/woocommerce/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Looks like you might need to use woocommerce_shipping_methods instead of woocommerce_available_shipping_methods?

    https://docs.woothemes.com/wc-apidocs/source-class-WC_Shipping.html#112

    Hello robinjoshua,
    use ‘woocommerce_package_rates’ filter to unset shipping methods.
    check this

    Thread Starter robinjoshua

    (@robinjoshua)

    @khusbu padalia

    I’ve found something with package rates filter (see below), however that did not work. When i add this in my functions.php the site crashes, and i don’t know why. I’ve checked with a code checker and there were no errors in the php code itself.

    What i want is:

    I have two shipping methods:
    1. flat_rate
    2. Local_pickup

    Some products are too heavy and therefore can only be picked up.

    Thanks for the help

    add_filter( ‘woocommerce_package_rates’, ‘custom_shipping_methods’ , 10, 2 );
    function custom_shipping_methods( $rates, $package ){
    if ( cart_has_product_with_orange_cats() ) {
    foreach($rates as $key => $method){
    if( $key == ‘local_delivery’ || $key == ‘local_pickup’){
    continue;
    }
    unset($rates[$key]);
    }
    // remove the rate you want
    }
    // return the available methods without the one you unset.
    return $rates;
    }?

    @robinjoshua,
    If you want to apply only local_delivery and local_pickup shipping method based on your cart condition, then replace with below code.

    add_filter('woocommerce_package_rates', 'display_shipping_method_based_on_state', 10, 2);
    if(!function_exists('display_shipping_method_based_on_state')) {
        function display_shipping_method_based_on_state($rates,$package) {
            global $woocommerce;
            if(cart_has_product_with_orange_cats()) {
                $local_pickup = $rates['local_pickup'];
                $local_delivery = $rates['local_delivery'];
                $rates = array();
                $rates['local_pickup'] = $local_pickup;
                $rates['local_delivery'] = $local_delivery;
            }
            return $rates;
        }
    }

    Thread Starter robinjoshua

    (@robinjoshua)

    @khusbu padalia

    You are my hero! ??
    Thank you so much.

    Robin

    @robinjoshua

    Most Welcome

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘WooCommerce shipping methods filter’ is closed to new replies.