Passing custom data from cart items to Order meta in Woocommerce 3

帅比萌擦擦* 提交于 2019-12-04 18:14:32

Update 2 - Two steps

1) Saving data:

We will save this custom customer data as "Hidden" order "item" meta data and then as order meta data too as this is related to subscriptions with a unique order item:

// Utility function: array of custom customer key/label pairs
function custom_data_keys_labels(){
    return array(
        'name'  => __('Customer name'),     'email' => __('Customer email'),
        'sex'   => __('Customer gender'),   'age'   => __('Customer age'),
    );
}

// Add/save custom field value as custom HIDDEN order item meta data
add_action( 'woocommerce_checkout_create_order_line_item', 'custom_field_update_order_item_meta', 20, 4 );
function custom_field_update_order_item_meta( $item, $cart_item_key, $values, $order ) {
    if ( ! isset( $values['custom_data'] ) )
        return;

    $custom_data = $values['custom_data'];
    $meta_data   = array();
    $labels_keys = custom_data_keys_labels();

    foreach( $labels_keys as $key => $label ){
        if ( isset( $custom_data[$key] ) )
            $meta_data[$key] = $custom_data[$key];
    }

    if ( sizeof( $meta_data ) > 0 )
        $item->update_meta_data( __('_customer_data'), $meta_data );

    return $cart_item_data;
}

// Add/save custom fields values as custom order meta data
add_action( 'woocommerce_checkout_create_order', 'my_custom_checkout_field_update_order_meta', 20, 2 );
function my_custom_checkout_field_update_order_meta( $order, $data ) {

    $order_items = $order->get_items(); // Order itesm
    $item        = reset($order_items); // Keep only the first order item

    $item_data   = $item->get_meta( '_customer_data' ); // Get custom customer data

    if( is_array($item_data) && sizeof($item_data) > 0 ){
        foreach( $item_data as $key => $value ) {
            if ( isset( $item_data[$key] ) )
                $order->update_meta_data( '_customer_' . $key, $value );
        }
        // Mark as data saved
        $order->update_meta_data( '_customer_data_set', true );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


2) Displaying saved custom data:

The code below also use our utility function custom_data_keys_labels()

// Order pages (frontend and admin) display
add_filter( 'woocommerce_order_details_after_order_table' , 'display_admin_order_meta_cutom_data', 20, 1 ); // Front
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_admin_order_meta_cutom_data', 20, 1 ); // Admin
function display_admin_order_meta_cutom_data( $order ){
    $labels_keys = custom_data_keys_labels();
    if( $order->get_meta( '_customer_data_set' ) ){
        if( is_admin() ){ // Admin
            echo '<p>';
            foreach( $labels_keys as $key => $label ){
                if ( $order->get_meta( '_customer_' . $key ) )
                    echo '<strong>' . $label . ':</strong> ' . $order->get_meta( '_customer_' . $key ) . '<br>';
            }
            echo '</p>';
        }
        else { // Front end: order view and Order received (thankyou)
            echo '<table class="woocommerce-table"><tbody>';
            foreach( $labels_keys as $key => $label ){
                if ( $order->get_meta( '_customer_' . $key ) )
                    echo '<tr><th>' . $label . ':</th><td>' . $order->get_meta( '_customer_' . $key ) . '</td></tr>';
            }
            echo '</tbody></table>';
        }
    }
}

// Email notifications display
add_filter( 'woocommerce_email_order_meta_fields' , 'display_email_cutom_data', 20, 3 );
function display_email_cutom_data ( $fields, $sent_to_admin, $order ) {
    $labels_keys = custom_data_keys_labels();
    if( $order->get_meta( '_customer_data_set' ) ){
        foreach( $labels_keys as $key => $label ){
            $fields['customer_' . $key] = array(
                'label' => $label,
                'value' => $order->get_meta( '_customer_' . $key ),
            );
        }
    }
    return $fields;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!