• Resolved alordiel

    (@alordiel)


    Hello,

    I was using the code below to hide all shipping methods if a coupon for free shipping is applied and to leave only the free option. Now it is not working and I think that i has to do something with the new implementation of the Shipping zones ( currently I’m not using any shipping zone and have left everything to default to the global one).

    add_filter( 'woocommerce_package_rates', 'hide_table_rate_shipping_when_free_is_available' , 10, 1 );
    function hide_table_rate_shipping_when_free_is_available( $available_methods ) {
    
    	if( isset( $available_methods['free_shipping'] ) ) {
    
    		foreach( $available_methods as $method_name => $method ) {
    
    			// check if method starts with 'table_rate' and remove it
    			if ( $method_name !='free_shipping'  )
    				unset( $available_methods[ $method_name ] );
    		}
    	}
    
    	return $available_methods;
    }

    Any clues where to dig for some info is welcomed. Thanks a lot.

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    $method_name will differ with zones – it will have an instance ID inside.

    Thread Starter alordiel

    (@alordiel)

    Thank you, Mike, I got the hint i needed.

    Here is a dump of the structure of the $available_methods for some reference.

    array(5) {
    	["flat_rate:4"]=> object(WC_Shipping_Rate)#7688 (6) {
    		["id"]=> string(11) "flat_rate:4"
    		["label"]=> string(68) "Shipping Name"
    		["cost"]=> string(5) "15.00"
    		["taxes"]=> array(0) { }
    		["method_id"]=> string(9) "flat_rate"
    		["meta_data":"WC_Shipping_Rate":private]=> array(1) {
    			["Item"]=> string(119) "Product name"
    		}
    	}
    	["local_pickup:7"]=> object(WC_Shipping_Rate)#7672 (6) {
    		["id"]=> string(14) "local_pickup:7"
    		["label"]=> string(68) "Shipping Name"
    		["cost"]=> string(5) "10.00"
    		["taxes"]=> array(0) { }
    		["method_id"]=> string(12) "local_pickup"
    		["meta_data":"WC_Shipping_Rate":private]=> array(1) {
    			["Item"]=> string(119) "Product name × 1"
    		}
    	}
    	["free_shipping:6"]=> object(WC_Shipping_Rate)#7689 (6) {
    		["id"]=> string(15) "free_shipping:6"
    		["label"]=> string(35) "Безплатна доставка"
    		["cost"]=> string(4) "0.00"
    		["taxes"]=> array(0) { }
    		["method_id"]=> string(13) "free_shipping"
    		["meta_data":"WC_Shipping_Rate":private]=> array(1) {
    			["Item"]=> string(119) "Product name × 1"
    		}
    	}
    	["free_shipping:5"]=> object(WC_Shipping_Rate)#7690 (6) {
    		["id"]=> string(15) "free_shipping:5"
    		["label"]=> string(62) "Shipping Name"
    		["cost"]=> string(4) "0.00"
    		["taxes"]=> array(0) { }
    		["method_id"]=> string(13) "free_shipping"
    		["meta_data":"WC_Shipping_Rate":private]=> array(1) {
    			["Item"]=> string(119) "Product name × 1"
    		}
    	}
    	["five_work_days"]=> object(WC_Shipping_Rate)#7691 (6) {
    		["id"]=> string(14) "five_work_days"
    		["label"]=> string(80) "Shipping Name"
    		["cost"]=> string(4) "0.00"
    		["taxes"]=> array(0) { }
    		["method_id"]=> string(14) "five_work_days" ["meta_data":"WC_Shipping_Rate":private]=> array(0) { }
    	}
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Are threre changes in the filter woocommerce_package_rates after 2.6’ is closed to new replies.