custom fields not working
-
Hello, I am trying to make the plugin work using custom fields. I added them following the instructions mentioned in https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/ as detailed in the details page of your product.
The field I am adding is RUC, which is required in the invoices in my country, is like a VAT ID.
I am also willing to dissapear postal code, for this is not used in my coutry.
So I have to add a field from billing address, and remove postal code from all addresses.
I have edited the functions.php file and included these lines for the additional field ‘RUC’:
/**
* Add the field to the checkout
*/
add_action( ‘woocommerce_before_checkout_billing_form’, ‘my_custom_checkout_field’ );function my_custom_checkout_field( $checkout ) {
//echo ‘<div id=”custom_ruc”><h2>’ . __(‘RUCo’) . ‘</h2>’;//encabezado de campo
woocommerce_form_field( ‘billing_ruc’, array(
‘type’ => ‘text’,
‘class’ => array(‘my-field-class form-row-wide’),
‘label’ => __(‘RUC’),
‘required’ => ‘true’,
‘placeholder’ => __(‘Ingrese los 13 dígitos del RUC’),
), $checkout->get_value( ‘billing_ruc’ ));echo ‘</div>’;
}
// Custom validation for RUC checkout field
add_action(‘woocommerce_checkout_process’, ‘custom_validate_ruc’);
function custom_validate_ruc() {
$is_correct = preg_match(‘/^[0-9]{13}$/’, $_POST[‘billing_ruc’]);
if ( $_POST[‘billing_ruc’] && !$is_correct) {
wc_add_notice( __( ‘El RUC debe tener 13 dígitos.’ ), ‘error’ );
}
}/**
* Process the checkout
*/
add_action(‘woocommerce_checkout_process’, ‘my_custom_checkout_field_process’);function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST[‘billing_ruc’] )
wc_add_notice( __( ‘Por favor ingresar el RUC (13 dígitos).’ ), ‘error’ );
}
/**
* Update the order meta with field value
*/
add_action( ‘woocommerce_checkout_update_order_meta’, ‘my_custom_checkout_field_update_order_meta’ );function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST[‘billing_ruc’] ) ) {
update_post_meta( $order_id, ‘RUC’, sanitize_text_field( $_POST[‘billing_ruc’] ) );
}
}
/**
* Display field value on the order edit page
*/
add_action( ‘woocommerce_admin_order_data_before_billing_address’, ‘my_custom_checkout_field_display_admin_order_meta’, 10, 1 );function my_custom_checkout_field_display_admin_order_meta($order){
echo ‘<p>‘.__(‘RUC’).’: ‘ . get_post_meta( $order->id, ‘RUC’, true ) . ‘</p>’;
}To remove the postal code, I added the following code to the functions.php file:
/*remove postcode*/
add_filter( ‘woocommerce_checkout_fields’ , ‘remove_billing_postcode_checkout’ );
function remove_billing_postcode_checkout( $fields ) {
unset($fields[‘billing’][‘billing_postcode’]);
return $fields;
}add_filter( ‘woocommerce_checkout_fields’ , ‘remove_shipping_postcode_checkout’ );
function remove_shipping_postcode_checkout( $fields ) {
unset($fields[‘shipping’][‘shipping_postcode’]);
return $fields;
}// Disable Postcode in Shipping Calculator
add_filter( ‘woocommerce_shipping_calculator_enable_postcode’, ‘__return_false’ );——————————————————–
Any clue on how to make this work using your plugin?The page I need help with: [log in to see the link]
The topic ‘custom fields not working’ is closed to new replies.