简体   繁体   中英

Add a custom checkout field value to customer order notes in WooCommerce

In short:

  1. I want to display a new field on Woocommerce Checkout 'daypart'. This is a dropdown field with 3 options.
  2. The code is working and I can see it on WooCommerce backend but I need to save the data in "Customer Notes" field but as an addition of the customer comments (if any) I'm stuck here, no clue in how to do this.

Additional information:

I need to save daypart in a core checkout field because the store synchs to another shipping service that doesn't synch order meta data.

Here's the code:

//define current user status to display things on checkout
if( current_user_can( 'rethunk_retailers' ) ){
  // Retailers text
    //* Add select field to the checkout page
add_action('woocommerce_before_order_notes', 'wps_add_select_checkout_field');
function wps_add_select_checkout_field( $checkout ) {

    echo '<h2>'.__('Partial Shipment Options').'</h2>';
    echo '<p>'.__('Option 1: details for option 1').'</p>';
    echo '<p>'.__('Option 2: details for option 2').'</p>';
    echo '<p>'.__('Option 3: details for option 3').'</p>';

    woocommerce_form_field( 'daypart', array(
        'label'         => __( 'Delivery options' ),
        'type'          => 'select',
        'class'         => array( 'wps-drop' ),
        'required' => true,
        'options'       => array(
            'blank'     => __( 'Please choose one', 'wps' ),
            'Yes I want Partial Shipment'   => __( 'Option 1: I want a partial shipment', 'wps' ),
            'Please do not ship a partiel shipment' => __( 'Option 2: Do not ship a partial shipment to me.', 'wps' ),
            'I will Wait do not refund anything'    => __( 'Option 3: Do not refund anything', 'wps' )
        )
),
        $checkout->get_value( 'daypart' ));

}

    //* Process the checkout
 add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
 function wps_select_checkout_field_process() {
    global $woocommerce;

    // Check if set, if its not set add an error.
    if ($_POST['daypart'] == "blank")
     wc_add_notice( '<strong>Please select a Partial Shipment Option </strong>', 'error' );

 }

    //* Update the order meta with field value
 add_action('woocommerce_checkout_update_order_meta', 'wps_select_checkout_field_update_order_meta');
 function wps_select_checkout_field_update_order_meta( $order_id ) {

   if ($_POST['daypart']) update_post_meta( $order_id, 'daypart', esc_attr($_POST['daypart']));
 }

} else {
   // not retailers text
}
//* Add selection field value to emails
add_filter('woocommerce_email_order_meta_keys', 'wps_select_order_meta_keys');
function wps_select_order_meta_keys( $keys ) {

    $keys['Shipping Option:'] = 'daypart';
    return $keys;

}
//* Display field value on the order edition page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'wps_select_checkout_field_display_admin_order_meta', 10, 1 );
function wps_select_checkout_field_display_admin_order_meta($order){

    echo '<p><strong>'.__('Delivery option').':</strong> ' . get_post_meta( $order->id, 'daypart', true ) . '</p>';

}

To include the "daypart" selected value to customer order notes, you will replace from your code:

// Update the order meta with field value
add_action('woocommerce_checkout_update_order_meta', 'wps_select_checkout_field_update_order_meta');
function wps_select_checkout_field_update_order_meta( $order_id ) {

    if ( $_POST['daypart']) update_post_meta( $order_id, 'daypart', esc_attr($_POST['daypart']));
}

by the following:

// Update the order meta with field value
add_action( 'woocommerce_checkout_create_order', 'wps_update_order_meta', 20, 2 );
function wps_update_order_meta( $order, $data ) {
    if ( isset($_POST['daypart']) && ! empty($_POST['daypart']) ) {
        // Add "daypart" selected value as custom order meta data
        $order->update_meta_data('daypart', esc_attr($_POST['daypart']));

        // Get customer note
        $customer_note = isset( $data['order_comments'] ) ? $data['order_comments'] . ' ' : '' );

        // add to existing customer note the "daypart" selected value
        $order->set_customer_note( $customer_note . esc_attr($_POST['daypart']) );
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM