最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - New field on checkout is shown but it's not saved on the order details

programmeradmin1浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

I am having a problem adding a custom field on the checkout of WooCommerce, I am using this code:

add_filter('woocommerce_checkout_fields','custom_override_checkout_fields');
function custom_override_checkout_fields($fields) {
    $fields['billing']['billing_colonia'] = array(
        'label' => __('Colonia', 'woocommerce'),
        'placeholder' => _x('', 'placeholder', 'woocommerce'),
        'required' => true,
        'class' => array('form-row-wide'), 
        'clear' => true
    );
    
    $fields['shipping']['shipping_colonia'] = array(
        'label' => __('Colonia', 'woocommerce'),
        'placeholder' => _x('', 'placeholder', 'woocommerce'),
        'required' => true,
        'class' => array('form-row-wide'), 
        'clear' => true
    );
    return $fields;
}

It indeed shows the field on the checkout form, but I fill it and I complete an order and nothing about this new field is saved on any order at /wp-admin/edit.php?post_type=shop_order

Should I do something to save that info? What is the correct way to add a custom field there?

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

I am having a problem adding a custom field on the checkout of WooCommerce, I am using this code:

add_filter('woocommerce_checkout_fields','custom_override_checkout_fields');
function custom_override_checkout_fields($fields) {
    $fields['billing']['billing_colonia'] = array(
        'label' => __('Colonia', 'woocommerce'),
        'placeholder' => _x('', 'placeholder', 'woocommerce'),
        'required' => true,
        'class' => array('form-row-wide'), 
        'clear' => true
    );
    
    $fields['shipping']['shipping_colonia'] = array(
        'label' => __('Colonia', 'woocommerce'),
        'placeholder' => _x('', 'placeholder', 'woocommerce'),
        'required' => true,
        'class' => array('form-row-wide'), 
        'clear' => true
    );
    return $fields;
}

It indeed shows the field on the checkout form, but I fill it and I complete an order and nothing about this new field is saved on any order at /wp-admin/edit.php?post_type=shop_order

Should I do something to save that info? What is the correct way to add a custom field there?

Share Improve this question asked Oct 9, 2020 at 12:54 CastiblancoCastiblanco 2,1947 gold badges20 silver badges26 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

As far as I remember you need to manually save your custom fields values via woocommerce_checkout_update_order_meta hook. Try

add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_fields_update_order_meta' );
function custom_checkout_fields_update_order_meta( $order_id ) {
    update_post_meta( $order_id, 'billing_colonia', sanitize_text_field( $_POST['billing_colonia'] ) );
    update_post_meta( $order_id, 'shipping_colonia', sanitize_text_field( $_POST['shipping_colonia'] ) );
}
发布评论

评论列表(0)

  1. 暂无评论