Skip to main content
Removed the PHP tag. Backend WordPress code will always have PHP, so it's is redundant.
Link
Fayaz
  • 9k
  • 2
  • 32
  • 51
Improved code formatting, light body improvements, added a related tag.
Source Link

I have added a custom field in WooCommerce checkout like below:

function oc_add_checkout_fields( $fields ) {
    $fields['shipping_sub_area'] = array(
    'label'    'label'    => __( 'Sub Area' ),
    'type'    'type'     => 'select',
    'class'    'class'    => array( 'form-row-wide', 'update_totals_on_change' ),
    'priority'    'priority' => 68,
    'required'    'required' => true,
    'id'     'id'       => 'th_sub_area',
    'options'         'options'  => array(''=>'Select',
            ''        =>'Select',
            'Clifton' => 'Clifton1', 
            'DHA'     => 'DHA1',
        ),
  'DHA' => 'DHA1',),
    'custom_attributes' => array(),
        'validate'          => array(),
        'default'           => '',
    );

    return $fields;
}
add_filter( 'woocommerce_default_address_fields', 'oc_add_checkout_fields' );

I have tried different hooks and actions but iI am unable to get the value of custom added field inside the hook. For example I have used following filter:

function filter_woocommerce_cart_shipping_packages( $array ) { 
    // make filter magic happen here... 
    //die('I am hre');
    echo '<pre>';
    print_r($array);
    return $array; 
}; 
     
// add the filter 
add_filter( 'woocommerce_cart_shipping_packages', 'filter_woocommerce_cart_shipping_packages', 10, 1 ); 

// add the filter add_filter( 'woocommerce_cart_shipping_packages', 'filter_woocommerce_cart_shipping_packages', 10, 1 );

In $array$array, iI am getting:

[destination] => Array
            (
                [country] => PK
                [state] => TA
                [postcode] => 
                [city] => fds
                [address] => fsdf
                [address_1] => fsdf
                [address_2] => fsd
            )

But iI am not getting value of 'shipping_sub_area'shipping_sub_area or th_sub_areath_sub_area.

I have added a custom field in WooCommerce checkout like below

function oc_add_checkout_fields( $fields ) {
 $fields['shipping_sub_area'] = array(
    'label'        => __( 'Sub Area' ),
    'type'        => 'select',
    'class'        => array( 'form-row-wide', 'update_totals_on_change' ),
    'priority'     => 68,
    'required'     => true,
    'id'            => 'th_sub_area',
    'options'           => array(''=>'Select',
                            'Clifton' => 'Clifton1', 
                            'DHA' => 'DHA1',),
    'custom_attributes' => array(),
    'validate'          => array(),
    'default'           => '',
);

return $fields;
}
add_filter( 'woocommerce_default_address_fields', 'oc_add_checkout_fields' );

I have tried different hooks and actions but i am unable to get the value of custom added field inside the hook. For example I have used following filter:

function filter_woocommerce_cart_shipping_packages( $array ) { 
    // make filter magic happen here... 
    //die('I am hre');
   echo '<pre>';
   print_r($array);
  return $array; 
}; 
     

// add the filter add_filter( 'woocommerce_cart_shipping_packages', 'filter_woocommerce_cart_shipping_packages', 10, 1 );

In $array, i am getting:

[destination] => Array
            (
                [country] => PK
                [state] => TA
                [postcode] => 
                [city] => fds
                [address] => fsdf
                [address_1] => fsdf
                [address_2] => fsd
            )

But i am not getting value of 'shipping_sub_area' or th_sub_area.

I have added a custom field in WooCommerce checkout like below:

function oc_add_checkout_fields( $fields ) {
    $fields['shipping_sub_area'] = array(
        'label'    => __( 'Sub Area' ),
        'type'     => 'select',
        'class'    => array( 'form-row-wide', 'update_totals_on_change' ),
        'priority' => 68,
        'required' => true,
        'id'       => 'th_sub_area',
        'options'  => array(
            ''        =>'Select',
            'Clifton' => 'Clifton1', 
            'DHA'     => 'DHA1',
        ),
        'custom_attributes' => array(),
        'validate'          => array(),
        'default'           => '',
    );

    return $fields;
}
add_filter( 'woocommerce_default_address_fields', 'oc_add_checkout_fields' );

I have tried different hooks and actions but I am unable to get the value of custom added field inside the hook. For example I have used following filter:

function filter_woocommerce_cart_shipping_packages( $array ) { 
    // make filter magic happen here... 
    //die('I am hre');
    echo '<pre>';
    print_r($array);
    return $array; 
}; 
     
// add the filter 
add_filter( 'woocommerce_cart_shipping_packages', 'filter_woocommerce_cart_shipping_packages', 10, 1 ); 

In $array, I am getting:

[destination] => Array
    (
        [country] => PK
        [state] => TA
        [postcode] => 
        [city] => fds
        [address] => fsdf
        [address_1] => fsdf
        [address_2] => fsd
    )

But I am not getting value of shipping_sub_area or th_sub_area.

Source Link

What Hook/Action is performed when a field in checkout form is changed in WooCommerce

I have added a custom field in WooCommerce checkout like below

function oc_add_checkout_fields( $fields ) {
 $fields['shipping_sub_area'] = array(
    'label'        => __( 'Sub Area' ),
    'type'        => 'select',
    'class'        => array( 'form-row-wide', 'update_totals_on_change' ),
    'priority'     => 68,
    'required'     => true,
    'id'            => 'th_sub_area',
    'options'           => array(''=>'Select',
                            'Clifton' => 'Clifton1', 
                            'DHA' => 'DHA1',),
    'custom_attributes' => array(),
    'validate'          => array(),
    'default'           => '',
);

return $fields;
}
add_filter( 'woocommerce_default_address_fields', 'oc_add_checkout_fields' );

When this field is changed, a pre loader is shown on the your order section in checkout form. I want to perform following: If selected value in above field is Clifton1 then add shipping charges 100 to the cart else add shipping charges 300 to the cart,

I have tried different hooks and actions but i am unable to get the value of custom added field inside the hook. For example I have used following filter:

function filter_woocommerce_cart_shipping_packages( $array ) { 
    // make filter magic happen here... 
    //die('I am hre');
   echo '<pre>';
   print_r($array);
  return $array; 
}; 
     

// add the filter add_filter( 'woocommerce_cart_shipping_packages', 'filter_woocommerce_cart_shipping_packages', 10, 1 );

In $array, i am getting:

[destination] => Array
            (
                [country] => PK
                [state] => TA
                [postcode] => 
                [city] => fds
                [address] => fsdf
                [address_1] => fsdf
                [address_2] => fsd
            )

But i am not getting value of 'shipping_sub_area' or th_sub_area.