• Resolved socorropc

    (@socorropc)


    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]

Viewing 1 replies (of 1 total)
  • Plugin Author Matt Harrison

    (@matt-h)

    This is more of a WooCommerce question in general and not really related to the plugin itself.

    I took a look at your code and you added the Billing RUC field with the “Adding a Custom Special Field” instructions instead of the instructions to add shipping or billing fields. So it is just a field on your checkout and not actually part of a Shipping Address or Billing Address. So, it would also not show on the Edit Address section of WooCommerce even without this plugin.

    It should be added into the Billing or Shipping Address directly. You’ll probably want to start with something like this:

    // Hook in
    add_filter( 'woocommerce_billing_fields' , 'custom_override_billing_fields' );
    
    // Our hooked in function - $fields is passed via the filter!
    function custom_override_billing_fields( $fields ) {
        $fields = array_merge(
            array(
                'billing_ruc' => array(
                    'label'       => __('RUC', 'woocommerce'),
                    'placeholder' => _x('RUC', 'placeholder', 'woocommerce'),
                    'required'    => true,
                    'class'       => array('form-row-wide'),
                    'clear'       => true
                )
            ),
            $fields
        );
    
        return $fields;
    }
    
    /**
     * Display field value on the order edit page
     */
     
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
    
    function my_custom_checkout_field_display_admin_order_meta($order){
        echo '<p><strong>'.__('Billing RUC').':</strong> ' . get_post_meta( $order->get_id(), '_billing_ruc', true ) . '</p>';
    }
Viewing 1 replies (of 1 total)

The topic ‘custom fields not working’ is closed to new replies.