• 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( $package = array() ) {
                $shipping_cost = 0;
                $destination_district = $package['destination']['area'];
                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

    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['shipping']['bairro'] = array(
        'label'     => __('Bairro', 'woocommerce'),
     	'type'      => 'text',
     	'required'  => true,
     	'class'     => array('form-row-wide'),
     	'clear'     => true
      );
    
      return $fields;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to add a custom field to work with my custom shipping method in woocommerce?’ is closed to new replies.