user2730011
user2730011

Reputation: 191

Add custom text based on shipping method to certain WooCommerce email notifications

I am trying to get WooCommerce to send different emails to customers depending on if they have selected 'Collection' or any delivery method.

If the customer chose to collect, the 'Processing' email must tell them that they will get notified once ready and the 'Completed' email must tell them to come collect.

If the customer chose delivery, the 'Processing' email must tell them they will be notified when its on its way and the 'Completed' email must tell them its on the way.

Currently, I have the following code and nothing is coming up on either email.

I am not sure if what my error is, and I am also not sure if I am using the correct variable names to reference my shipping methods.

add_action ('woocommerce_email_customer_details', 'custom_email_customer_details', 15, 4);
function custom_email_customer_details( $order, $sent_to_admin, $plain_text, $email ){

  
    if ( $order->has_shipping_method('Pickup from PnP Honey Junction') && $new_status == 'processing' ){

        echo "<h2>Shipping notice</h2>
        <p>You will be notified when to collect your order</p>";
        
    }
        
        elseif($order->has_shipping_method('Pickup from PnP Honey Junction') && $new_status == 'completed'){
            
        echo "<h2>Shipping notice</h2>
        <p>Your order is ready for pickup</p>";
            
        }
    
elseif ( ! $order->has_shipping_method('Pickup from PnP Honey Junction') && $new_status == 'processing' ) {

        echo "<h2>Shipping notice</h2>
        <p>You will be notified when your order is out for delivery</p>";
    
        }
    
    
    elseif(! $order->has_shipping_method('Pickup from PnP Honey Junction') && $new_status == 'completed' ) {
        
              echo "<h2>Shipping notice</h2>
        <p>Your order is out for delivery</p>";
        
        }

}

enter image description here

Upvotes: 3

Views: 1423

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29640

$new_status is undefined and you are applying the shipping method incorrectly.

To apply this correctly, it is best to first obtain the correct method id, this can be done via this code:

function action_woocommerce_email_customer_details( $order, $sent_to_admin, $plain_text, $email ) { 
    // Get the shipping method Id
    $shipping_items = $order->get_items('shipping');
    $shipping_item = reset( $shipping_items );
    
    echo 'DEBUG INFORMATION: ' . $shipping_item->get_method_id();
}
add_action( 'woocommerce_email_customer_details', 'action_woocommerce_email_customer_details', 10, 4 );

The result will then be something like:

  • DEBUG INFORMATION: local_pickup

Then you will replace the above code with this one, to target an specific email notification you can use $email->id

Then it is a matter of applying the debug information in this answer:

function action_woocommerce_email_customer_details( $order, $sent_to_admin, $plain_text, $email ) {
    // For 'processing' & has shipping method 'REPLACE WITH THE DEBUG INFORMATION'
    if ( $email->id == 'customer_processing_order' && $order->has_shipping_method( 'local_pickup' ) ) {
        echo __( 'My processing text', 'woocommerce' );
    // For 'completed'
    } elseif( $email->id == 'customer_completed_order' && $order->has_shipping_method( 'local_pickup' ) ) {
        echo __( 'My completed text', 'woocommerce' );      
    }
}
add_action( 'woocommerce_email_customer_details', 'action_woocommerce_email_customer_details', 10, 4 );

Upvotes: 2

Related Questions