我已根据以下内容在装运方法中创建了自定义文件:
function shipping_instance_form_add_extra_fields($settings)
{
$settings[\'shipping_extra_field\'] = [
\'title\' => \'Route Number\',
\'type\' => \'text\',
\'placeholder\' => \'1,2,3...\',
\'description\' => \'Enter route number\'
];
return $settings;
}
我需要在订购时将路由器号添加到订单中,它可以更改后订单这就是我将其保存到订单中的原因。我尝试了以下方法,但没有任何乐趣:
add_action( \'woocommerce_after_order_notes\', \'my_custom_checkout_field\' );
function my_custom_checkout_field( $checkout ) {
echo \'<div id="my_custom_checkout_field" style="display:none;"><h2>\' . __(\'Route Number\') . \'</h2>\';
woocommerce_form_field( \'route_number\', array(
\'type\' => \'text\',
\'class\' => array(\'my-field-class form-row-wide\'),
\'label\' => __(\'Fill in this field\'),
\'placeholder\' => __(\'Enter something\'),
), $checkout->get_value( \'route_number\' ));
echo \'</div>\';
}
add_action( \'woocommerce_checkout_update_order_meta\', \'my_custom_checkout_field_update_order_meta\' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
$shipping_methods = WC()->shipping->get_shipping_methods();
$shipping_method = @array_shift($order->get_shipping_methods());
$shipping_method_route_number = $shipping_method[\'Route Number\'];
update_post_meta( $order_id, \'Route Number\', sanitize_text_field( $shipping_method_route_number )
}
欢迎任何帮助
SO网友:uk101man
下面是我最后用来解决问题的方法:
add_action(\'woocommerce_init\', \'shipping_instance_form_fields_filters\');
function shipping_instance_form_fields_filters()
{
$shipping_methods = WC()->shipping->get_shipping_methods();
foreach($shipping_methods as $shipping_method) {
add_filter(\'woocommerce_shipping_instance_form_fields_\' . $shipping_method->id, \'shipping_instance_form_add_extra_fields\');
}
}
function shipping_instance_form_add_extra_fields($settings)
{
$settings[\'route_number\'] = [
\'title\' => \'Route Number\',
\'type\' => \'text\',
\'placeholder\' => \'1,2,3...\',
\'description\' => \'Enter route number\'
];
return $settings;
}
add_action( \'woocommerce_checkout_update_order_meta\', \'my_custom_checkout_field_update_order_meta\' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
$chosen_methods = WC()->session->get( \'chosen_shipping_methods\' );
$chosen_shipping = substr( $chosen_methods[0],-1);
$free_shipping = get_option( \'woocommerce_free_shipping_\'.$chosen_shipping.\'_settings\' );
$route_number = $free_shipping[\'route_number\'];
update_post_meta( $order_id, \'Route Number\', sanitize_text_field( $route_number ) );
}