How to add a custom field to work with my custom shipping method in woocommer
-
I want to calculate the delivery rate by neighborhoods without using zip code. I made a custom delivery method as below, but I can only use the default woocommerce address fields.
In the case I am using the default woocommerce city address field, it works but I want to be able to create a field named “neighborhood” and be able to use it with my custom method.
my custom method
function my_custom_shipping_init() { class my_custom_shipping_method extends WC_Shipping_Method { public function __construct() { $this->id = 'my_custom_shipping'; $this->method_title = __('Taxa de entregar por bairros', 'my-custom-shipping'); $this->enabled = 'yes'; $this->title = __('Taxa de entregar por bairros', 'my-custom-shipping'); $this->method_description = __('Plugin to calculate My Custom Shipping Cost', 'my-custom-shipping'); } function calculate_shipping( $fields = array() ) { $shipping_cost = 0; $destination_district = $fields['billing']['bairro']; switch($destination_district){ case 'Alpha': $shipping_cost = 5; break; case 'Beta': $shipping_cost = 7; break; } $this->add_rate(array('id' => 'my_custom_shipping', 'label' => 'Taxa de Entrega', 'cost' => $shipping_cost, 'taxes' => '', 'calc_tax' => 'per_order')); } } } add_action( 'woocommerce_shipping_init', 'my_custom_shipping_init' );
my custom field
function my_custom_shipping_method( $methods ) { $methods[] = 'my_custom_shipping_method'; return $methods; } add_filter( 'woocommerce_shipping_methods', 'my_custom_shipping_method' ); add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' ); // Our hooked in function - $fields is passed via the filter! function custom_override_checkout_fields( $fields ) { $fields['billing']['bairro'] = array( 'label' => __('Bairro', 'woocommerce'), 'type' => 'text', 'required' => true, 'class' => array('form-row-wide'), 'clear' => true ); return $fields; }
And i’ve tried also
function calculate_shipping( $data = array() ) { $shipping_cost = 0; $destination_district = $data['billing']['bairro']; switch($destination_district){ case 'Alpha': $shipping_cost = 5; break; case 'Beta': $shipping_cost = 7; break; }
Anyone can help?
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘How to add a custom field to work with my custom shipping method in woocommer’ is closed to new replies.