Adding a custom Shipping methods field to an order

Question

I have created a custom filed in a Shipping methods as per:

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;
} 

I need to add the router number at the time of order to a order, it can change post order this is why I’m saving it to an order. I have tried the below with no joy:

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 ) 
}

Any help would be welcome

0
, uk101man 3 years 2020-04-05T20:51:02-05:00 0 Answers 97 views 0

Leave an answer

Browse
Browse