1

I added new section with name donate_section before shipping form in checkout page and in it I added Select Drop-down name _done_select.

I need to populate it from database.

I'm using below query but still not able to fetch data from database function:

donate_section_donate_dropdown( $fields ) {
    
    global $wpdb;
    $ngos = $wpdb->get_results("SELECT * FROM wp_wc_temp_ngo");
    
    $output = array();
    
    $output[0] = "-----------------------";
    
    foreach( $ngos as $key => $row) {
        $output[$row->id] = $row->firstname;
    }
    
    $shipping_address1_args = wp_parse_args( array(
        'type'       => 'select',
        'options'    => $output,
    ), $fields['donate_section']['_donate_dropdown'] );
    $fields['donate_section']['_donate_dropdown'] = $shipping_address1_args;
    //$fields['shipping']['shipping_first_name']['default'] = 'aaa';
    return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'donate_section_donate_dropdown' );
Ruvee
  • 8,611
  • 4
  • 18
  • 44
ANEJ
  • 13
  • 2
  • Do you get any errors/warnings? – Ruvee Jan 29 '21 at 21:25
  • thank you for your reply no i'm not getting anything I used checkout fields to add custom section and in it I added select (Drop down) and trying to populate it from database – ANEJ Jan 29 '21 at 21:28

1 Answers1

0

Ok! Couple of points in your code!

First of all, I would give woocommerce_checkout_fields filter hook, a priority. Like so:

add_filter( 'woocommerce_checkout_fields', 'donate_section_donate_dropdown', 20 );

Then next point is your foreach loop! Change it with following foreach loop:

foreach ($ngos as $row) {
    $output[$row->id] = $row->firstname;
  };

And the last point is the way you would construct the drop-down menu:

$fields['shipping']['_donate_dropdown'] = array(
    'type' => 'select',
    'label' => 'Donations',
    'priority' => 25 # use this number to move your drop-down, up and down the list of fields
  );

 $fields['shipping']['_donate_dropdown']['options'] = $output;

So the end script for your functions.php would be like this:

add_filter( 'woocommerce_checkout_fields', 'donate_section_donate_dropdown', 20);

donate_section_donate_dropdown( $fields ) {
    
    global $wpdb;

    $ngos = $wpdb->get_results("SELECT * FROM wp_wc_temp_ngo");
    
    $output = array();
    
    $output[0] = "-----------Select a donation option-----------";
    
    foreach( $ngos as $row) {
        $output[$row->id] = $row->firstname;
    }
    
    $fields['shipping']['_donate_dropdown'] = array(
    'type' => 'select',
    'label' => 'Donations',
    'priority' => 25 # use this number to move your drop-down up and down the list of fields
  );

    $fields['shipping']['_donate_dropdown']['options'] = $output;

    return $fields;
}

Hope this would save you some time and frustrations! I just tested it on my own woocommerce installation and it works fine!

Ruvee
  • 8,611
  • 4
  • 18
  • 44
  • really apricated your support your solution working perfectly but my problem that i added section using Checkout Field Editor for WooCommerce name it donate_section and place it after billing form and after that created select (dropdown) _donate_dropdown if I placed ['donate_section']['_donate_dropdown'] it will not be populated from database thanks again – ANEJ Jan 30 '21 at 00:03
  • The ```woocommerce_checkout_fields``` hook would return ```billing array``` and ```shipping array```. If you have already added another section to these two arrays, then use that custom array that you've added to populate the drop-down. – Ruvee Jan 30 '21 at 00:30
  • I think it's easier for you to debug this if you run: ```add_filter( 'woocommerce_checkout_fields', 'donate_section_donate_dropdown', 20);``` and then ```donate_section_donate_dropdown( $fields ) { print_r($fields); return $fields;}``` – Ruvee Jan 30 '21 at 00:30
  • You could see if your custom section has actually exist or not by ```print_r($fields)```. If it exists then you're all set! If it doesn't, then you could append your donation drop-down to the shipping section or to the billing section. – Ruvee Jan 30 '21 at 00:34