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 questionI 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 questionI 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 badges1 Answer
Reset to default 3As 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'] ) );
}