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.
woocommerce_shipping_zone_after_methods_table
. I was able to add field to the form. And usewoocommerce_before_shipping_zone_object_save
to save the value, which was blocker for me. Adding the field in a such a way that it starts getting posted back is tricky as this is an ajax request. I tried a lot but no matter what I do, thezone_custom
field I created wasnt getting posted. – Nerverackingecho
, it means that yourif
doesn't evaluate to true. Tryecho 'this runs for shipping_options and section is ' . $current_section;
to see what value you get for that. It may not be a string and it may not ever contain the value you think it should. – Sandasandakan