Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

php - Save checkout custom field value and display it in WooCommerce admin orders

I have some additional fields in my checkout for asking questions from the customer about certain things regarding their order. Most of these are text input fields but there's one Select menu asking them how they heard about my clients site. I've been unable to figure out how to display the value in the meta area of the order for the selected option. Here's how I'm generating that select drop down in my functions.php file from my theme.

woocommerce_form_field( 'aba_hear', array(
    'type'     => 'select',
    'required' => 'true',
    'class'    => array('hear-class form-row-wide'),
    'label'    => __('How did You Hear About Us?'),
    'options'  => array( // options for <select> or <input type="radio" />
        ''          => 'Please select', // empty values means that field is not selected
        'Instagram' => 'Instagram',
        'Facebook'  => 'Facebook',
        'Yelp'  => 'Yelp',
        'Other' => 'Other',
    )

), $checkout->get_value( 'aba_hear' ) );

Now I go add a function to update the order meta values:

add_action( 'woocommerce_checkout_update_order_meta', 'aba_checkout_field_update_order_meta' );

function aba_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['aba_hear'] ) ) {
        update_post_meta( $order_id, 'How did You Hear About Us&#63;', sanitize_text_field( $_POST['aba_hear'] ) );
    }
}

And finally display the value on the order page:

add_action( 'woocommerce_admin_order_data_after_billing_address', 'aba_checkout_field_display_admin_order_meta', 10, 1 );
function aba_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('How did You Hear About Us&#63;').':</strong> ' . get_post_meta( $order->id, 'Is this a Gift&#63;', true ) . '</p>';
}

Lastly here's how the select menu's code appears at checkout:

<p class="form-row hear-class form-row-wide validate-required" id="aba_hear_field" data-priority="">
    <label for="aba_hear" class="">How did You Hear About Us&#063;&nbsp;<abbr class="required" title="required">*</abbr></label>
    <span class="woocommerce-input-wrapper">
        <select name="aba_hear" id="aba_hear" class="select " data-allow_clear="true" data-placeholder="Please select">
            <option value=""  selected='selected'>Please select</option>
            <option value="Instagram" >Instagram</option>
            <option value="Facebook" >Facebook</option>
            <option value="Yelp" >Yelp</option>
            <option value="Other" >Other</option>
        </select>
    </span>
</p>

Now, this works fine for text input fields but not select menus. How can I change this to make it work so I can display the resulting data?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are some mistakes in some of your functions… You need to use the same checkout field key as meta key in the following functions:

In the 2nd function, you use another hook and save custom field as user meta data too:

add_action( 'woocommerce_checkout_create_order', 'aba_checkout_field_update_order_meta' );

function aba_checkout_field_update_order_meta( $order ) {
    if ( isset($_POST['aba_hear']) && ! empty($_POST['aba_hear']) ) {
        $order->update_meta_data( '_aba_hear', sanitize_text_field( $_POST['aba_hear'] ) );

        // Update user data
        if( $order->get_user_id() > 0 ) {
            update_user_meta( $order->get_user_id(), 'aba_hear', true );
        }
    }
}

In the 3rd function use this:

add_action( 'woocommerce_admin_order_data_after_billing_address', 'aba_checkout_field_display_admin_order_meta', 10, 1 );
function aba_checkout_field_display_admin_order_meta( $order ){
    $value = $order->get_meta( '_aba_hear' );

    if ( ! empty($value) ) {
        echo '<p><strong>'.__('How did You Hear About Us&#63;').':</strong> ' . $value . '</p>';
    }
}

It should better work now.


Notes:

Why saving this checkout field also as custom user meta data?

Because in your first function you have $checkout->get_value( 'aba_hear' ) that will display the selected value on from customer last order in this custom checkout field. The value is read from user meta 'aba_hear'.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...