【发布时间】:2019-09-25 14:02:05
【问题描述】:
我一直在尝试将自定义“选择”字段添加到 woocommerce 结帐。选择选项由数组中的条目组成,而数组又由来自自定义帖子类型查询的标题和日期组成。
这一切都很好,我可以添加新的自定义帖子并将标题和日期连接起来,添加到数组中,然后添加到结帐时的选择下拉列表中,但是当我提交表单并完成订单时所选选择字段值的索引被添加到元数据中,而不是值。
我在此处添加了图像以向您展示我的意思。例如如果我从“Tain Farmers Market”下拉列表中选择第三个选项,即数组索引2,这就是为订单元保存的内容,而不是该索引条目中的值。
屏幕截图包括显示数组内容的 var 转储结果以及它下方的选择字段。
image of how the array appears in var dump the order meta showing in the admin screen, displaying the index not the value
这是我涵盖此部分的代码,任何指针都会非常方便。我觉得可能有一些简单的解决方案可以在处理订单时获取所选索引处的值?
/**
* Add custom Pickup Field field to the checkout page
*/
add_filter( 'woocommerce_after_order_notes', 'hiwoo_add_checkout_fields' );
function hiwoo_add_checkout_fields( $checkout ) {
$args = array(
'post_type' => 'pickup-point',
'posts_per_page' => -1,
'post_status' => 'publish'
);
// Custom query to pull in the pickup points.
$query = new WP_Query( $args );
$pickup_comb_option = [];
while ($query->have_posts()) {
$query->the_post();
$postid = $post->ID;
$pickuptitle = get_the_title($post);
$pickupdate = get_post_meta(get_the_id($post), 'available_date', true);
$pickupoption = $pickuptitle . ' - ' . $pickupdate;
array_push($pickup_comb_option, $pickupoption);
}
var_dump($pickup_comb_option);
// Restore original post data.
wp_reset_postdata();
echo '<div id="custom_checkout_field"><h2>' . __('Order Pickup Location/Date') . '</h2>';
woocommerce_form_field( 'pickup_point_options', array(
'type' => 'select',
'class' => array(
'my-field-class form-row-wide'
) ,
'label' => __('Select Pickup Location/Date') ,
'placeholder' => __('Pickup Point') ,
'options' => $pickup_comb_option,
),
$checkout->get_value( 'pickup_point_options' ));
echo '</div>';
} /* Close custom field function */
/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['pickup_point_options'] )
wc_add_notice( __( 'Please select a pickup location from the list.' ), 'error' );
}
/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['pickup_point_options'] ) ) {
update_post_meta( $order_id, 'Pickup Location/Date', sanitize_text_field( $_POST['pickup_point_options'] ) );
}
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Pickup Location/Date').':</strong> ' . get_post_meta( $order->id, 'Pickup Location/Date', true ) . '</p>';
}
结帐页面上为选择字段生成的html是:
array(3) {
[0]=>
string(32) "Tain Farmers Market - 2019-12-25"
[1]=>
string(30) "Hi Create Offices - 2019-10-25"
[2]=>
string(27) "Dornoch Stores - 2019-09-26"
}
<select name="pickup_point_options" id="pickup_point_options" class="select " data-placeholder="Pickup Point">
<option value="0">Tain Farmers Market - 2019-12-25</option>
<option value="1">Hi Create Offices - 2019-10-25</option>
<option value="2">Dornoch Stores - 2019-09-26</option>
</select>
【问题讨论】:
-
您的问题包含太多似乎没有必要的信息。我不清楚最终保存了什么。是数组索引还是选择框中显示的文本? (在这种情况下,值是一个坏词,因为值应该是数组索引)。你能显示选择的生成的 HTML 吗?
-
抱歉 - 澄清一下。当您保存表单(即完成订单)时,保存到 order_meta 中的是该条目在数组中的索引,即 2,而不是值,即“Tain Farmers Market”
-
您说的是“Tain Farmers Market”的价值。我假设这是从用户的角度陈述的。当我谈到价值时,我指的是选择选项中价值属性的实际内容。能不能展示一下
<select>产生的html? -
谢谢,我已经更新了原始帖子以包含选择字段出现在结帐页面上时的 HTML 内容。谢谢
标签: php wordpress woocommerce