Map Link for the Shipping Address in New Order Emails
Adds an "Open in Maps" link under the shipping address in the shop's new order email, handy for local delivery drivers.
Opens WPIDE on
Code
add_action( 'woocommerce_email_customer_details', function ( $order, $sent_to_admin, $plain_text ) {
if ( ! $sent_to_admin || ! $order instanceof WC_Order || ! $order->has_shipping_address() ) {
return;
}
$address = implode( ', ', array_filter( array( $order->get_shipping_address_1(), $order->get_shipping_address_2(), $order->get_shipping_city(), $order->get_shipping_postcode(), $order->get_shipping_country() ) ) );
$url = 'https://www.google.com/maps/search/?api=1&query=' . rawurlencode( $address );
if ( $plain_text ) {
echo "\n" . esc_html( 'Map: ' ) . esc_url_raw( $url ) . "\n";
return;
}
printf( '<p><a href="%s">%s</a></p>', esc_url( $url ), esc_html( 'Open the delivery address in Maps' ) );
}, 25, 3 );
Notes
Only added to emails sent to the shop, and nothing is requested from Google until someone clicks the link.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.