0

im new in WordPress i wan to change my WooCommerce check out form postcode to dropdown box.. im using the solution given online but it's not work on my page.. pls help me. here the code i put inside flatsome child function file.

add_filter( 'woocommerce_default_address_fields' , 'customize_postcode_fields' ); function customize_postcode_fields( $adresses_fields ) {

$adresses_fields['postcode']['type'] = 'select';
$adresses_fields['postcode']['options'] = array(
    ''         => __('Select your postcode', 'woocommerce'),
    'option_1' => 'Choice 1',
    'option_2' => 'Choice 2',
    'option_3' => 'Choice 3'
);

return $adresses_fields;

}

and the result that i get after apply the code my display after apply

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
dcpress
  • 1
  • 1

1 Answers1

0

I have updated my post from earlier which may make it easier for you to interpret and put into your code (I changed it from my last answer, as what I published wont work) - the main difference is putting the array into a variable, to make it easier to change in the future. I have done some research as well and found that the information you provided was the best option for this (so I just cleaned it up a bit). However, if you could also inspect/view-source on your code

function wpe_0987_customize_postcode_fields( $postcode_field ) {

    $options = array(
        '' => __( 'Select...', 'woocommerce' ),
        'choice_1' => 'choice_1',
        'choice_2' => 'choice_2',
        'choice_3' => 'choice_3',
        'choice_4' => 'choice_4'
    );

    $fields['billing_postcode']['type'] = 'select';
    $fields['shipping_postcode']['type'] = 'select';
    $fields['billing_postcode']['options'] = $options;
    $fields['shipping_postcode']['options'] = $options;

    return $fields;

}

For Shipping Field Only!

add_filter( 'woocommerce_shipping_fields' , 'wpe_0987_customize_postcode_fields' );

For Shipping and Billing Fields

add_filter( 'woocommerce_default_address_fields' , 'wpe_0987_customize_postcode_fields' );

Also, when you create custom functions, don't forget to put a custom prefix at the start. At the moment you have 'customize_postcode_fields', you should find a series of letters/numbers or something unique to you to ensure that it doesn't clash with any other theme/plugin - eg: 'random123_customize_postcode_fields' and use that prefix on all custom functions you create in that project.

Update:

Do an 'inspect' or 'view source' on the page, check to see what the 'name' of the postcode form is, and update it with one of the two that I have provided above (shipping_postcode, billing_postcode).

Aliqua
  • 723
  • 7
  • 21
  • does this work on WordPress WooCommerce ?im new. sorry – dcpress Jun 18 '20 at 15:06
  • Woocommerce is still coded in html/php so it's worth a try. – Aliqua Jun 18 '20 at 21:32
  • i been using the code u given.. but the result still the same only show one word. add_filter( 'woocommerce_default_address_fields' , 'dcpress_customize_postcode_fields' ); function dcpress_customize_postcode_fields( $postcode_field ) { $options = array( '' => __( 'Select...', 'woocommerce' ), 'choice_1' => 'choice_1', 'choice_2' => 'choice_2', 'choice_3' => 'choice_3', 'choice_4' => 'choice_4' ); $fields['postcode']['type'] = 'select'; $fields['postcode']['options'] = $options; return $fields; } – dcpress Jun 19 '20 at 00:45
  • Updated my answer re: change postcode to billing_postcode and/or shipping_postcode – Aliqua Jun 19 '20 at 01:36
  • still cannot work.. dono which part got problem.. but thank you for ur helping.. i already find another to do it..which is create my own custom field .. thank you – dcpress Jun 19 '20 at 11:59