1

My client's WooCommerce shop is exporting orders as comma separated files for creation of packaging labels and accounting in C5.

When users enter an address with a comma upon checkout, this screws up the CSV export. Example: "60 Brickyard Road, Lapeer". The comma after "Road" confuses C5 when importing.

How can I filter out any commas before the address value is saved to the order?

1

You can remove/replace comas from address_1 and address_2 billing and shipping fields with the following, once order is submitted (before saving data):

// Checkout/Order: Remove/replace comas from adresses fields
add_action('woocommerce_checkout_create_order', 'remove_comas_from_address_fields', 10, 2 );
function remove_comas_from_address_fields( $order, $data ) {
    $replacement = '';

    if ( $billing_address_1 = $order->get_billing_address_1() ) {
        $order->set_billing_address_1( str_replace( ',', $replacement, $billing_address_1 ) );
    }

    if ( $billing_address_2 = $order->get_billing_address_2() ) {
        $order->set_billing_address_2( str_replace( ',', $replacement, $billing_address_2 ) );
    }

    if ( $shipping_address_1 = $order->get_shipping_address_1() ) {
        $order->set_shipping_address_1( str_replace( ',', $replacement, $shipping_address_1 ) );
    }

    if ( $shipping_address_2 = $order->get_shipping_address_2() ) {
        $order->set_shipping_address_2( str_replace( ',', $replacement, $shipping_address2 ) );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

If you wish, you can also keep your answer code at the same time. This way everything will be secure in case of manipulation.

  • Thanks a lot! That's a great and simple solution. I really appreciate you took your time to make this answer! – Skovsgaard May 7 at 17:03
0

I managed to prohibit the use of commas in both address fields with the following code. Add it to your functions.php file.

add_action( 'wp_footer', 'checkout_field_name_validator_script');
function checkout_field_name_validator_script() {
    // Only on checkout page
    if( ! ( is_checkout() && ! is_wc_endpoint_url() ) ) return;
    ?>
    <script>
    jQuery(function($){
        var b = '#billing_',    s = '#shipping_',
            a = 'address_1',     c = ',';

        // Address fields
        $(b+a+c+s+a).bind('keyup blur',function(){
            $(this).val($(this).val().replace(/,/g,''));
        });

    });
    </script>
    <?php
}

Thanks and credit to user "LoicTheAztec" in this post.

Important - This works for the frontend only. To validate and filter the input see the accepted answer.

  • 2
    You should not rely on client-side code for something this important. – Jacob Peattie May 6 at 8:49
  • As Jacob pointed out, you should not rely only on client-side validation. You can use the woocommerce_checkout_process hook to check order form on server-side and display the appropriate message. Look here at the part about validation. – nmr May 6 at 9:20
  • You're absolutely right. I'll look into that, though I'm not really sure how to do it. – Skovsgaard May 6 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.