Populate woocommerce_wp_select field from database
please help me out with this..I've tried too many different things...
I'm trying to populate a woocommerce select field with data from the database.
// Add fields & settings to the custom product tab
$SQL = "SELECT DISTINCT table_name FROM wp_lbc_prices";
$array = $wpdb->get_results( $SQL, ARRAY_A);
add_action( 'woocommerce_product_data_panels',
'wcpt_roller_blind_options_product_tab_content' );
function wcpt_roller_blind_options_product_tab_content() {
?><div id='roller_blind_options' class='panel woocommerce_options_panel'><?php
?><div class='options_group'><?php
woocommerce_wp_select( array(
'id' => 'roller_blind_tables',
'label' => __( 'Price Tables', 'wcpt' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Select Associated Price Table.', 'wcpt' ),
'options' => $array
));
?></div>
</div><?php
Certainly, the query to the DB works and a result is being returned... but I'm not sure how to make an acceptable array (I'm used to asp.net which seems to make this more simple!). The data I am returning does not need an ID so the value and text of the dropdown can be the same.
mysql wordpress select woocommerce
add a comment |
please help me out with this..I've tried too many different things...
I'm trying to populate a woocommerce select field with data from the database.
// Add fields & settings to the custom product tab
$SQL = "SELECT DISTINCT table_name FROM wp_lbc_prices";
$array = $wpdb->get_results( $SQL, ARRAY_A);
add_action( 'woocommerce_product_data_panels',
'wcpt_roller_blind_options_product_tab_content' );
function wcpt_roller_blind_options_product_tab_content() {
?><div id='roller_blind_options' class='panel woocommerce_options_panel'><?php
?><div class='options_group'><?php
woocommerce_wp_select( array(
'id' => 'roller_blind_tables',
'label' => __( 'Price Tables', 'wcpt' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Select Associated Price Table.', 'wcpt' ),
'options' => $array
));
?></div>
</div><?php
Certainly, the query to the DB works and a result is being returned... but I'm not sure how to make an acceptable array (I'm used to asp.net which seems to make this more simple!). The data I am returning does not need an ID so the value and text of the dropdown can be the same.
mysql wordpress select woocommerce
add a comment |
please help me out with this..I've tried too many different things...
I'm trying to populate a woocommerce select field with data from the database.
// Add fields & settings to the custom product tab
$SQL = "SELECT DISTINCT table_name FROM wp_lbc_prices";
$array = $wpdb->get_results( $SQL, ARRAY_A);
add_action( 'woocommerce_product_data_panels',
'wcpt_roller_blind_options_product_tab_content' );
function wcpt_roller_blind_options_product_tab_content() {
?><div id='roller_blind_options' class='panel woocommerce_options_panel'><?php
?><div class='options_group'><?php
woocommerce_wp_select( array(
'id' => 'roller_blind_tables',
'label' => __( 'Price Tables', 'wcpt' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Select Associated Price Table.', 'wcpt' ),
'options' => $array
));
?></div>
</div><?php
Certainly, the query to the DB works and a result is being returned... but I'm not sure how to make an acceptable array (I'm used to asp.net which seems to make this more simple!). The data I am returning does not need an ID so the value and text of the dropdown can be the same.
mysql wordpress select woocommerce
please help me out with this..I've tried too many different things...
I'm trying to populate a woocommerce select field with data from the database.
// Add fields & settings to the custom product tab
$SQL = "SELECT DISTINCT table_name FROM wp_lbc_prices";
$array = $wpdb->get_results( $SQL, ARRAY_A);
add_action( 'woocommerce_product_data_panels',
'wcpt_roller_blind_options_product_tab_content' );
function wcpt_roller_blind_options_product_tab_content() {
?><div id='roller_blind_options' class='panel woocommerce_options_panel'><?php
?><div class='options_group'><?php
woocommerce_wp_select( array(
'id' => 'roller_blind_tables',
'label' => __( 'Price Tables', 'wcpt' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Select Associated Price Table.', 'wcpt' ),
'options' => $array
));
?></div>
</div><?php
Certainly, the query to the DB works and a result is being returned... but I'm not sure how to make an acceptable array (I'm used to asp.net which seems to make this more simple!). The data I am returning does not need an ID so the value and text of the dropdown can be the same.
mysql wordpress select woocommerce
mysql wordpress select woocommerce
asked Nov 28 '18 at 18:00
user2687383user2687383
33
33
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
- You need first to be sure of
table_name
or replace it by the correct column slug to be queried from your database tablewp_lbc_prices
. - You need to replace the
WPDB
methodget_results()
byget_col()
which query a single column and gives an array natively. - Prepare the array to copying the values to the keys using
array_combine()
I have completed your code with the hooked function that adds a custom product tab.
Your revisited code will be:
add_filter( 'woocommerce_product_data_tabs', 'add_roller_blind_product_data_tab' );
function add_roller_blind_product_data_tab( $tabs ) {
$tabs['roller_blind'] = array(
'label' => __( 'Roller blind', 'wcpt' ),
'target' => 'roller_blind_options', // <== to be used in the <div> class of the content
//'class' => array('show_if_simple'), // or 'hide_if_simple' or 'show_if_variable'…
);
return $tabs;
}
add_action( 'woocommerce_product_data_panels', 'display_roller_blind_product_data_tab_content' );
function display_roller_blind_product_data_tab_content() {
global $wpdb;
echo '<div id="roller_blind_options" class="panel woocommerce_options_panel">
<div class="options_group">';
$sql_query = $wpdb->get_col( "SELECT DISTINCT table_name FROM {$wpdb->prefix}wp_lbc_prices" );
$options = sizeof($sql_query) > 0 ? array_combine( $sql_query, $sql_query ) : array();
woocommerce_wp_select( array(
'id' => 'roller_blind_tables',
'label' => __( 'Price Tables', 'wcpt' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Select Associated Price Table.', 'wcpt' ),
'options' => $options
));
echo '</div></div>';
}
It should better work now. But as this is a custom database table is not testable.
Thanks so much! The code works great, and you've given me some additional methods to look into and use for the future. And thanks also for the explainations.
– user2687383
Nov 29 '18 at 4:41
add a comment |
get_results function return an array with indexes so the values can be like array of objects like this array([0]=>object,[1]=>object)
can you please share what you are getting in $array variable ?
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53525467%2fpopulate-woocommerce-wp-select-field-from-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
- You need first to be sure of
table_name
or replace it by the correct column slug to be queried from your database tablewp_lbc_prices
. - You need to replace the
WPDB
methodget_results()
byget_col()
which query a single column and gives an array natively. - Prepare the array to copying the values to the keys using
array_combine()
I have completed your code with the hooked function that adds a custom product tab.
Your revisited code will be:
add_filter( 'woocommerce_product_data_tabs', 'add_roller_blind_product_data_tab' );
function add_roller_blind_product_data_tab( $tabs ) {
$tabs['roller_blind'] = array(
'label' => __( 'Roller blind', 'wcpt' ),
'target' => 'roller_blind_options', // <== to be used in the <div> class of the content
//'class' => array('show_if_simple'), // or 'hide_if_simple' or 'show_if_variable'…
);
return $tabs;
}
add_action( 'woocommerce_product_data_panels', 'display_roller_blind_product_data_tab_content' );
function display_roller_blind_product_data_tab_content() {
global $wpdb;
echo '<div id="roller_blind_options" class="panel woocommerce_options_panel">
<div class="options_group">';
$sql_query = $wpdb->get_col( "SELECT DISTINCT table_name FROM {$wpdb->prefix}wp_lbc_prices" );
$options = sizeof($sql_query) > 0 ? array_combine( $sql_query, $sql_query ) : array();
woocommerce_wp_select( array(
'id' => 'roller_blind_tables',
'label' => __( 'Price Tables', 'wcpt' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Select Associated Price Table.', 'wcpt' ),
'options' => $options
));
echo '</div></div>';
}
It should better work now. But as this is a custom database table is not testable.
Thanks so much! The code works great, and you've given me some additional methods to look into and use for the future. And thanks also for the explainations.
– user2687383
Nov 29 '18 at 4:41
add a comment |
- You need first to be sure of
table_name
or replace it by the correct column slug to be queried from your database tablewp_lbc_prices
. - You need to replace the
WPDB
methodget_results()
byget_col()
which query a single column and gives an array natively. - Prepare the array to copying the values to the keys using
array_combine()
I have completed your code with the hooked function that adds a custom product tab.
Your revisited code will be:
add_filter( 'woocommerce_product_data_tabs', 'add_roller_blind_product_data_tab' );
function add_roller_blind_product_data_tab( $tabs ) {
$tabs['roller_blind'] = array(
'label' => __( 'Roller blind', 'wcpt' ),
'target' => 'roller_blind_options', // <== to be used in the <div> class of the content
//'class' => array('show_if_simple'), // or 'hide_if_simple' or 'show_if_variable'…
);
return $tabs;
}
add_action( 'woocommerce_product_data_panels', 'display_roller_blind_product_data_tab_content' );
function display_roller_blind_product_data_tab_content() {
global $wpdb;
echo '<div id="roller_blind_options" class="panel woocommerce_options_panel">
<div class="options_group">';
$sql_query = $wpdb->get_col( "SELECT DISTINCT table_name FROM {$wpdb->prefix}wp_lbc_prices" );
$options = sizeof($sql_query) > 0 ? array_combine( $sql_query, $sql_query ) : array();
woocommerce_wp_select( array(
'id' => 'roller_blind_tables',
'label' => __( 'Price Tables', 'wcpt' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Select Associated Price Table.', 'wcpt' ),
'options' => $options
));
echo '</div></div>';
}
It should better work now. But as this is a custom database table is not testable.
Thanks so much! The code works great, and you've given me some additional methods to look into and use for the future. And thanks also for the explainations.
– user2687383
Nov 29 '18 at 4:41
add a comment |
- You need first to be sure of
table_name
or replace it by the correct column slug to be queried from your database tablewp_lbc_prices
. - You need to replace the
WPDB
methodget_results()
byget_col()
which query a single column and gives an array natively. - Prepare the array to copying the values to the keys using
array_combine()
I have completed your code with the hooked function that adds a custom product tab.
Your revisited code will be:
add_filter( 'woocommerce_product_data_tabs', 'add_roller_blind_product_data_tab' );
function add_roller_blind_product_data_tab( $tabs ) {
$tabs['roller_blind'] = array(
'label' => __( 'Roller blind', 'wcpt' ),
'target' => 'roller_blind_options', // <== to be used in the <div> class of the content
//'class' => array('show_if_simple'), // or 'hide_if_simple' or 'show_if_variable'…
);
return $tabs;
}
add_action( 'woocommerce_product_data_panels', 'display_roller_blind_product_data_tab_content' );
function display_roller_blind_product_data_tab_content() {
global $wpdb;
echo '<div id="roller_blind_options" class="panel woocommerce_options_panel">
<div class="options_group">';
$sql_query = $wpdb->get_col( "SELECT DISTINCT table_name FROM {$wpdb->prefix}wp_lbc_prices" );
$options = sizeof($sql_query) > 0 ? array_combine( $sql_query, $sql_query ) : array();
woocommerce_wp_select( array(
'id' => 'roller_blind_tables',
'label' => __( 'Price Tables', 'wcpt' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Select Associated Price Table.', 'wcpt' ),
'options' => $options
));
echo '</div></div>';
}
It should better work now. But as this is a custom database table is not testable.
- You need first to be sure of
table_name
or replace it by the correct column slug to be queried from your database tablewp_lbc_prices
. - You need to replace the
WPDB
methodget_results()
byget_col()
which query a single column and gives an array natively. - Prepare the array to copying the values to the keys using
array_combine()
I have completed your code with the hooked function that adds a custom product tab.
Your revisited code will be:
add_filter( 'woocommerce_product_data_tabs', 'add_roller_blind_product_data_tab' );
function add_roller_blind_product_data_tab( $tabs ) {
$tabs['roller_blind'] = array(
'label' => __( 'Roller blind', 'wcpt' ),
'target' => 'roller_blind_options', // <== to be used in the <div> class of the content
//'class' => array('show_if_simple'), // or 'hide_if_simple' or 'show_if_variable'…
);
return $tabs;
}
add_action( 'woocommerce_product_data_panels', 'display_roller_blind_product_data_tab_content' );
function display_roller_blind_product_data_tab_content() {
global $wpdb;
echo '<div id="roller_blind_options" class="panel woocommerce_options_panel">
<div class="options_group">';
$sql_query = $wpdb->get_col( "SELECT DISTINCT table_name FROM {$wpdb->prefix}wp_lbc_prices" );
$options = sizeof($sql_query) > 0 ? array_combine( $sql_query, $sql_query ) : array();
woocommerce_wp_select( array(
'id' => 'roller_blind_tables',
'label' => __( 'Price Tables', 'wcpt' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Select Associated Price Table.', 'wcpt' ),
'options' => $options
));
echo '</div></div>';
}
It should better work now. But as this is a custom database table is not testable.
answered Nov 28 '18 at 22:44
LoicTheAztecLoicTheAztec
95k1370111
95k1370111
Thanks so much! The code works great, and you've given me some additional methods to look into and use for the future. And thanks also for the explainations.
– user2687383
Nov 29 '18 at 4:41
add a comment |
Thanks so much! The code works great, and you've given me some additional methods to look into and use for the future. And thanks also for the explainations.
– user2687383
Nov 29 '18 at 4:41
Thanks so much! The code works great, and you've given me some additional methods to look into and use for the future. And thanks also for the explainations.
– user2687383
Nov 29 '18 at 4:41
Thanks so much! The code works great, and you've given me some additional methods to look into and use for the future. And thanks also for the explainations.
– user2687383
Nov 29 '18 at 4:41
add a comment |
get_results function return an array with indexes so the values can be like array of objects like this array([0]=>object,[1]=>object)
can you please share what you are getting in $array variable ?
add a comment |
get_results function return an array with indexes so the values can be like array of objects like this array([0]=>object,[1]=>object)
can you please share what you are getting in $array variable ?
add a comment |
get_results function return an array with indexes so the values can be like array of objects like this array([0]=>object,[1]=>object)
can you please share what you are getting in $array variable ?
get_results function return an array with indexes so the values can be like array of objects like this array([0]=>object,[1]=>object)
can you please share what you are getting in $array variable ?
answered Nov 28 '18 at 19:08
Waqas AliWaqas Ali
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53525467%2fpopulate-woocommerce-wp-select-field-from-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown