在Wocmmerce結帳頁,輸入地址欄位的時候,有些人會希望顧客不要輸入到標點符號,這邊以半型逗號為例,參考下面的語法放在functions.php即可
// 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 ) );
}
}