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?

2 Answers
2

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_address_2 ) );
    }
}

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.

Leave a Reply

Your email address will not be published. Required fields are marked *