将自定义数据保存到客户,并在下一个订单时预先填充值

时间:2019-01-09 11:44:14

标签: wordpress woocommerce

WooCommerce插件从结帐中提供自定义订单数据(联邦快递编号),因此客户不必支付运输费用。我需要将此值(联邦快递编号)保存到客户,因此下次在后端创建订单时,联邦快递编号将预先填写。

我在WooCommerce Order admin中创建了一个显示该值的元框,还在“帐单明细”下创建了一个显示该值的字段。

在后端创建新订单时,我不知道如何获得预先填充的值。

这是我到目前为止所得到的:

//* Display field value on the order edit page *//

add_action( 'woocommerce_admin_order_data_after_billing_address', 
'my_fedex_checkout_field_display_admin_order_meta', 10, 1 );

function my_fedex_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('FedEx number Client').':</strong> ' . 
get_post_meta( $order->id, 'FedEx_number', true ) . '</p>';
}

//* Adding Meta container admin shop_order pages *//

add_action( 'add_meta_boxes', 'fedex_add_meta_boxes' );
if ( ! function_exists( 'fedex_add_meta_boxes' ) )
{
function fedex_add_meta_boxes()
{
    global $woocommerce, $order, $post;

    add_meta_box( 'fedex_other_fields', __('FedEx number 
Client','woocommerce'), 'fedex_add_other_fields_for_orders', 
'shop_order', 'side', 'core' );
}
}


//* adding Meta field in the meta container admin shop_order pages 
//*

if ( ! function_exists( 'fedex_save_wc_order_other_fields' ) )
{
function fedex_add_other_fields_for_orders()
{
    global $woocommerce, $order, $post;

    $meta_field_data = get_post_meta( $post->ID, 'FedEx_number', 
true ); //? get_post_meta( $post->ID, 'FedEx_number', true ) : '';

    echo '<input type="hidden" name="fedex_other_meta_field_nonce" 
value="' . wp_create_nonce() . '">
    <p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
        <input type="text" style="width:250px;";" 
name="FedEx_number" placeholder="' . $meta_field_data . '" value="' 
. $meta_field_data . '"></p>';

}
}

//* Save Fedex number to Customer //*

add_action('save_post_shop_order', 'customer_fedex_save', 50, 3 );
function customer_fedex_save( $post_id, $post, $update ) {

// Checking that is not an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    return $post_id;

// Check the user’s permissions (for 'shop_manager' and 
'administrator' user roles)
if ( ! current_user_can( 'edit_shop_order', $post_id ) )
    return $post_id;

if( isset($_POST['FedEx_number']) ) {
    $order = wc_get_order( $post_id );

    // Update user meta data
    update_user_meta( $order->get_customer_id(), 'FedEx_number', 
sanitize_text_field( $_POST['FedEx_number'] ) );
}
}

1 个答案:

答案 0 :(得分:0)

我对您的代码进行了一些更改,还添加了一些其他功能。由于我不使用您的Fedex插件,因此我已经对其进行了部分测试,并且我不确定它是否会按您的意愿工作。

代码:

// On Order submit: Save 'FedEx_number' as user meta data if it doesn't exits
add_action( 'woocommerce_checkout_update_order_meta', 'sync_fedex_checkout_field_to_order', 100, 1 );
function sync_fedex_checkout_field_to_order( $order_id, $data ){
    $meta_key   = 'FedEx_number';
    $user_id    = (int) get_post_meta( $order_id, '_customer_user', true );
    if( $user_id > 0 ) {
        $meta_value = get_user_meta( $user_id, $meta_key, true );
    }

    // Sync 'FedEx_number' as user meta data if it doesn't exist yet
    if( $user_id > 0 && ! $meta_value ) {
        // Get the 'FedEx_number' value and update user data
        if( $meta_value = get_post_meta( $order_id, $meta_key, true ) ) 
            update_user_meta( $user_id, $meta_key, esc_attr( $meta_value ) );
    }
}

// Display the fedex field value under admin order billing address section
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_fedex_checkout_field_display_admin_order_meta', 10, 1 );
function my_fedex_checkout_field_display_admin_order_meta( $order ){
    $meta_key = 'FedEx_number';
    $user_id  = $order->get_customer_id();

    if( $user_id > 0 ) {
        $meta_value = get_user_meta( $user_id, $meta_key, true );
    }
    if( ! isset($meta_value) ) {
        $meta_value = $order->get_meta( $meta_key );
    }
    if( isset($meta_value) ) {
        echo '<p><strong>'.__("FedEx number Client", "woocommerce").':</strong> ' . $meta_value . '</p>';
    }
}

// Add fedex order metabox
add_action( 'add_meta_boxes', 'add_fedex_meta_box' );

function add_fedex_meta_box() {
    global $post;

    add_meta_box( 'fedex_meta_box',
    __("FedEx number Client", "woocommerce'"),
    'fedex_meta_box_content',
    'shop_order', 'side', 'core' );
}

// The fedex metabox content
function fedex_meta_box_content() {
    global $post;

    $meta_key = 'FedEx_number';
    $user_id  = get_post_meta( $post->ID, '_customer_user', true );

    if( $user_id > 0 ) {
        $meta_value = get_user_meta( $user_id, $meta_key, true );
    } else {
        $meta_value = get_post_meta( $post->ID, $meta_key, true );
    }

    echo '<p style="border-bottom:solid 1px #eee;padding-bottom:13px;">
    <input type="text" style="width:250px;"  name="'.$meta_key.'" value="'.$meta_value.'">
    <input type="hidden" name="fedex_field_nonce" value="' . wp_create_nonce() . '"></p>';
}

add_action('save_post_shop_order', 'customer_fedex_save', 100, 1 );
function customer_fedex_save( $post_id ) {
    // Security check
    if ( ! isset( $_POST[ 'fedex_field_nonce' ] ) ) {
        return $post_id;
    }

    //Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST[ 'fedex_field_nonce' ] ) ) {
        return $post_id;
    }

    // Checking that is not an autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    // Check the user’s permissions (for 'shop_manager' and 'administrator' user roles)
    if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
        return $post_id;
    }

    $meta_key = 'FedEx_number';

    if( isset($_POST[$meta_key]) ) {
        $user_id = (int) get_post_meta( $post_id, '_customer_user', true );

        // Update post meta
        update_post_meta( $post_id, $meta_key, sanitize_text_field( $_POST[$meta_key] ) );

        // Update user meta
        if( $user_id > 0)
            update_user_meta( $user_id, $meta_key, sanitize_text_field( $_POST[$meta_key] ) );
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过部分测试。