How to get WooCommerce shipping methods cost and settings? How to get WooCommerce shipping methods cost and settings? wordpress wordpress

How to get WooCommerce shipping methods cost and settings?


Because WC()->shipping->get_shipping_methods() doesn't load the shipping methods that are set on each shipping zone, it just loads all available shipping methods that you can set in any shipping zone, with all default values and available fields for settingsā€¦

Remember that shipping methods costs and settings are set by shipping zone and are related to locations (Region, country, state or postcode).

So as shipping methods rates are different for each shipping zone you need to get first, one or all shipping Zones that are set in shipping section settings.

Then from a shipping zone you can get all shipping methods rates that is set for it like:

// Get all your existing shipping zones IDS$zone_ids = array_keys( array('') + WC_Shipping_Zones::get_zones() );// Loop through shipping Zones IDsforeach ( $zone_ids as $zone_id ) {    // Get the shipping Zone object    $shipping_zone = new WC_Shipping_Zone($zone_id);    // Get all shipping method values for the shipping zone    $shipping_methods = $shipping_zone->get_shipping_methods( true, 'values' );    // Loop through each shipping methods set for the current shipping zone    foreach ( $shipping_methods as $instance_id => $shipping_method )     {        // The dump of protected data from the current shipping method        var_dump($shipping_method);    }}

This time, as you will see, you get all settings (custom label, costs and other settings) for each shipping method rate set in a shipping zone.