Woocommerce custom field not saving

2019-07-16 20:49发布

I have 3 custom fields in woocommerce:

Matter reference number, invoice number and soliciter dealing with case (code next, if you dont want to see it just scroll down, its not that important)

add_action('woocommerce_after_order_notes', 'matter_reference_number_func');

function matter_reference_number_func( $checkout ) {

    echo '<div id="matter_ref"><h3>'.__('Matter reference number').'</h3>';

    woocommerce_form_field( 'matter_reference_number', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'placeholder'       => __('Matter reference number'),
        ), $checkout->get_value( 'matter_ref_num' ));

    echo '</div>';

}

add_action('woocommerce_after_order_notes', 'invoice_number_func');

function invoice_number_func( $checkout ) {

    echo '<div id="inv_num"><h3>'.__('Invoice number').'</h3>';

    woocommerce_form_field( 'invoice_number', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'placeholder'       => __('Invoice number'),
        ), $checkout->get_value( 'invoice_num' ));

    echo '</div>';

}

add_action('woocommerce_after_order_notes', 'sol_deal_func');

function sol_deal_func( $checkout ) {

    echo '<div id="sol_deal"><h3>'.__('Solicitor dealing with matter').'</h3>';

    woocommerce_form_field( 'matter_reference_number', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'placeholder'       => __('Solicitor dealing with matter'),
        ), $checkout->get_value( 'sol_deal' ));

    echo '</div>';

}

Now however when I try to save the values they do not appear in the email to client nor in the admin, or even on confirm page. According the the docs this should be sufficient to do that; but it isnt.

/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'matter_ref_num_checkout_field_update_order_meta');

function matter_ref_num_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['matter_ref_num']) update_post_meta( $order_id, 'Matter Reference Number', esc_attr($_POST['matter_ref_num']));
}

/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'invoice_num_checkout_field_update_order_meta');

function invoice_num_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['invoice_num']) update_post_meta( $order_id, 'Invoice Number', esc_attr($_POST['invoice_num']));
}

/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'sol_deal_checkout_field_update_order_meta');

function sol_deal_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['sol_deal']) update_post_meta( $order_id, 'Solicitor Dealing With Matter', esc_attr($_POST['sol_deal']));
}

标签: php wordpress
1条回答
Luminary・发光体
2楼-- · 2019-07-16 21:18

You are using the hook to update the post meta (being the post id the order id). If you check your database you should be able to see those fields under the appropriate post id meta.

If you want to add them to the emails you'll need to use another hook too:

woocommerce_email_order_meta_keys

Here is a reference from woocommerce documentation:

/**
 * Add the field to order emails
 **/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');

function my_custom_checkout_field_order_meta_keys( $keys ) {
    $keys[] = 'My Field';
    return $keys;
}

You really only need to use one hook per action and place your code there, eg:

/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta');

function custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['matter_ref_num']) update_post_meta( $order_id, 'Matter Reference Number', esc_attr($_POST['matter_ref_num']));
    if ($_POST['invoice_num']) update_post_meta( $order_id, 'Invoice Number', esc_attr($_POST['invoice_num']));
    if ($_POST['sol_deal']) update_post_meta( $order_id, 'Solicitor Dealing With Matter', esc_attr($_POST['sol_deal']));
}

Hope this helps.

查看更多
登录 后发表回答