I am trying to add a select field to Woocommerce Shipping tab and to Shipping Zones section of it while creating a new shipping zone. I found this on official documentation of Woocommerce while searching for the solution.
What I've tried so far is:
// Fires on 'plugins_loaded' action of WordPress
public function plugins_loaded_action() {
add_filter( 'woocommerce_get_settings_shipping', array( $this, 'shipping_zone_settings_add_city_field' ), 10, 2 );
}
public function shipping_zone_settings_add_city_field( $settings, $current_section ) {
echo 'this runs only for shipping_options section';
/**
* Check the current section for shipping zone
*/
if( $current_section === 'shipping_zones' ) {
// Add city field to settings
$settings[] = array(
array(
'name' => __( 'Zone City', 'woocommerce' ),
'type' => 'title',
'desc' => __( 'Specify city names for current shipping region', 'woocommerce' ),
'id' => 'shipping_zones',
),
array(
'name' => __( 'Zone Cities', 'woocommerce' ),
'desc_tip' => __( 'Add all cities you want to be apply this shipping region for.', 'woocommerce' ),
'id' => 'wc_shipping_zone_cities',
'type' => 'multiselect',
'desc' => __( 'Cities for this shipping region', 'woocommerce' ),
),
array(
'type' => 'sectionend',
'id' => 'shipping_zones',
),
);
}
return $settings;
}
But the hooked filter function only runs for shipping_options
section as I am able to see the echo
output at top in that section only.
The Woocommerce class for shipping settings has this method for getting settings.
Clearly, I'm doing something wrong here, may be hooking to incorrect filter or anything else. Please help.