我在Woo-commerce网站上有大约10,000个订单。大约0.4%没有保存所需的自定义字段'custom_location'。我不知道这是怎么可能的,也找不到重现的方法。
此外,我使用字段的选定值将次值保存到DB。即使数据库中的'custom_location'为空,也会正确保存辅助值。很明显,$ _POST ['custom_location']包含有效数据,但它没有保存...为什么?
// Here I create the custom select field
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields');
function custom_override_checkout_fields( $fields ) {
// <select> options
$args = array('post_type' => 'MY_CUSTOM_POST_TYPE',
'posts_per_page' => -1,
'order' => 'ASC',
'post_status' => 'publish',
'suppress_filters' => true);
$locations = get_posts($args);
$ops = array('default' => 'Select a location');
foreach($locations as $l) {
$ops[$l->ID] = __($l->post_title, 'woocommerce' );
}
// Add 'custom_location' field to 'shipping'
$fields['shipping'] = array( 'custom_location' => array(
'label' => __('Location', 'woocommerce'),
'placeholder' => _x('', 'empty', 'woocommerce'),
'required' => true,
'clear' => false,
'type' => 'select',
'options' => $ops
))
+ $fields['shipping'];
return $fields;
}
// Save related data
add_action( 'woocommerce_checkout_update_order_meta', 'save_extra_data' );
function save_extra_data( $order_id ) {
$loc = $_POST['custom_location'];
// This was saved correctly to DB even when 'custom_location' is null in DB!
$meta = get_post_meta($loc, 'extra-shipping-data', true);
update_post_meta( $order_id, '_shipping_extra', $meta );
}
答案 0 :(得分:0)
如果客户的帐单和送货地址相同,您的位置将被保存为空。在您的代码中,缺少function process_stuff() {
do_stuff;
webcall(link, params, callback);
}
function callback(data){
do_stuff2;
}
function webcall(link, params, callback) {
//httprequest...;
//on success
callback(response)
}
的声明。稍作修改,您可以最大限度地减少代码和工作量。确保将locations
替换为您的选项。
ops