Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinstern/8857267 to your computer and use it in GitHub Desktop.
Save justinstern/8857267 to your computer and use it in GitHub Desktop.
An example of using the WooCommerce Admin Custom Order Fields 'wc_admin_custom_order_field_options' filter to return a dynamic set of options. In this sample we're pulling all WordPress terms, for the field which we have named "From Database". Options could be pulled from any other database/table, remote service, or anywhere! To use this code, c…
<?php
/*
* Returns a set of admin custom order field options from the database, rather
* than as configured by the admin
*/
add_filter( 'wc_admin_custom_order_field_options', function( $options, $field ) {
global $wpdb;
// return all the configured WordPress terms as the field options for the field named "From Database"
if ( 'From Database' == $field->label ) {
// clear out any passed options so we can set our own
$options = array();
$results = $wpdb->get_results( "SELECT slug, name FROM {$wpdb->terms} ORDER BY name" );
foreach ( $results as $row ) {
$options[] = array( 'label' => $row->name, 'value' => $row->slug, 'default' => false );
}
}
return $options;
}, 10, 2 );
@Asenio
Copy link

Asenio commented Feb 7, 2014

Sounds great! Waiting for the release...

When i try to add other fields with your plugin, no fields shows up in the checkout form. How do i call the fields from your plugin to show up?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment