Search code examples
phpwordpresswoocommercehook-woocommerceemail-notifications

Custom formatted adresses section in WooCommerce email notifications sent to admin


i've been searching around for a long time now and for some reason i cant get it to work.

i need to hide from admin email only, but for some reason it also apply to customer email. i want to hide, shipping from the other, and in client details, phone and email adress.

  <?php  
        add_filter( 'woocommerce_get_order_item_totals', 'custom_woocommerce_get_order_item_totals' );

 function custom_woocommerce_get_order_item_totals( $totals ) {
  unset( $totals['shipping'] );
      return $totals;
  }
  ///// so far is what i got to hide everything and not just the specific fields i wanted
  function removing_customer_details_in_emails( $order, $sent_to_admin, $plain_text, $email ){
$wmail = WC()->mailer();
if($sent_to_admin)
remove_action( 'woocommerce_email_customer_details', array( $wmail, 'email_addresses' ), 20, 3 );
 }
 add_action( 'woocommerce_email_customer_details', 'removing_customer_details_in_emails', 5, 4 );

as mentioned above it hides the fields from the customer email as well and i need just for the admin email, and for some reason all i found did not work,


Solution

  • To hide billing phone, billing email and shipping address on email notifications sent to the admin, use the following instead:

    add_action( 'woocommerce_email_customer_details', 'sent_to_admin_custom_email_addresses', 5, 4 );
    function sent_to_admin_custom_email_addresses( $order, $sent_to_admin, $plain_text, $email ){
        if( in_array( $email->id, array( 'new_order', 'cancelled_order', 'failed_order' ) ) ) {
            $mailer = WC()->mailer();
            remove_action( 'woocommerce_email_customer_details', array( $mailer, 'email_addresses' ), 20 );
            add_action( 'woocommerce_email_customer_details', 'custom_email_addresses', 20, 1 );
        }
    }
    
    function custom_email_addresses( $order ) {
        if ( is_a( $order, 'WC_Order' ) ) :
    
        $text_align = is_rtl() ? 'right' : 'left';
        $address    = $order->get_formatted_billing_address();
    
        ?><table id="addresses" cellspacing="0" cellpadding="0" style="width: 100%; vertical-align: top; margin-bottom: 40px; padding:0;" border="0">
            <tr>
                <td style="text-align:<?php echo esc_attr( $text_align ); ?>; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; border:0; padding:0;" valign="top" width="50%">
                    <h2><?php esc_html_e( 'Billing address', 'woocommerce' ); ?></h2>
                    <address class="address">
                        <?php echo wp_kses_post( $address ? $address : esc_html__( 'N/A', 'woocommerce' ) ); ?>
                    </address>
                </td>
            </tr>
        </table>
        <?php
        endif;
    }
    

    Code goes in functions.php file of the active child theme (or active theme). It should works.

    Related: Removing customer details and addresses from email notification templates