admin管理员组

文章数量:1390204

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

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

Share Improve this question asked Apr 5, 2020 at 23:33 uk101manuk101man 193 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Below is what I used in the end to solve my problem:

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

} 

本文标签: woocommerce offtopicAdding a custom Shipping methods field to an order