Woocommerce 如何在结帐页面上从数据库创建和填充自定义下拉列表
Woocommerce how to create and populate a custom drop-down from database on the checkout page
我在 checkout page
中的发货表格前添加了名称为 donate_section
的新部分,并在其中添加了 Select 下拉名称 _done_select
.
我需要从数据库中填充它。
我正在使用以下查询,但仍然无法从数据库函数中获取数据:
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' );
好的!您的代码中有几点!
首先,我会给woocommerce_checkout_fields
过滤器钩子一个优先级。像这样:
add_filter( 'woocommerce_checkout_fields', 'donate_section_donate_dropdown', 20 );
然后下一点是您的 foreach
循环!使用以下 foreach
循环更改它:
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;
所以你的 functions.php
的结束脚本应该是这样的:
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;
}
希望这会为您节省一些时间和挫折感!我刚刚在我自己的 woocommerce 安装上测试了它,它工作正常!
我在 checkout page
中的发货表格前添加了名称为 donate_section
的新部分,并在其中添加了 Select 下拉名称 _done_select
.
我需要从数据库中填充它。
我正在使用以下查询,但仍然无法从数据库函数中获取数据:
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' );
好的!您的代码中有几点!
首先,我会给woocommerce_checkout_fields
过滤器钩子一个优先级。像这样:
add_filter( 'woocommerce_checkout_fields', 'donate_section_donate_dropdown', 20 );
然后下一点是您的 foreach
循环!使用以下 foreach
循环更改它:
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;
所以你的 functions.php
的结束脚本应该是这样的:
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;
}
希望这会为您节省一些时间和挫折感!我刚刚在我自己的 woocommerce 安装上测试了它,它工作正常!