How to Customize WooCommerce Order Emails Based on Product Attributes

In a recent Business Bloomer Club Slack thread, a WooCommerce user wanted to customize order confirmation emails to display specific text based on whether an order was for pickup or delivery.

These options were set as product attributes, and the customization needed to change the “Thanks for shopping with us” message that appears after the Billing and Shipping details.

Here’s how to achieve this using WooCommerce filters to dynamically tailor the message in completed order emails.

Step 1: Use the woocommerce_email_additional_content_customer_completed_order Filter

WooCommerce includes a filter, woocommerce_email_additional_content_customer_completed_order, that allows you to modify the “Additional content” section in completed order emails. This filter provides access to the order object, so you can check product attributes and tailor the message based on the pickup or delivery option.

Step 2: Check Product Attributes and Customize the Message

Here’s a code snippet to inspect each product’s attributes and update the “Additional content” text accordingly:

add_filter( 'woocommerce_email_additional_content_customer_completed_order', 'customize_email_based_on_product_attributes', 10, 2 );

function customize_email_based_on_product_attributes( $additional_content, $order ) {
    $is_pickup = false;
    $is_delivery = false;

    // Loop through each product in the order
    foreach ( $order->get_items() as $item ) {
        $product = $item->get_product();
        if ( $product ) {
            // Get product attributes
            $attributes = $product->get_attributes();

            // Check for "pickup" or "delivery" attribute
            if ( isset( $attributes['pickup'] ) ) {
                $is_pickup = true;
            }
            if ( isset( $attributes['delivery'] ) ) {
                $is_delivery = true;
            }
        }
    }

    // Customize the additional content based on attributes
    if ( $is_pickup && !$is_delivery ) {
        $additional_content = "Thank you for choosing pickup! Your order will be ready at our designated pickup location.";
    } elseif ( $is_delivery && !$is_pickup ) {
        $additional_content = "Thank you for choosing delivery! Your order will be delivered to your specified address.";
    } else {
        $additional_content = "Thank you for shopping with us!";
    }

    return $additional_content;
}

Explanation of the Code

  1. Loop through Order Items: The code loops through each item in the order to access product attributes.
  2. Check for Pickup or Delivery Attribute: It inspects each product’s attributes to see if they include “pickup” or “delivery.”
  3. Set Custom Message Based on Attribute: Based on the detected attribute, it customizes the email message for pickup or delivery.

Step 3: Test the Customization

To ensure that the message displays correctly:

  1. Place a Test Order: Test both pickup and delivery scenarios to confirm that the correct message appears.
  2. Check the Email: Verify the content in the order confirmation email to ensure it accurately reflects the selected order type.

Conclusion

By using the woocommerce_email_additional_content_customer_completed_order filter, you can tailor WooCommerce order emails based on product attributes, ensuring that customers receive relevant information for pickup or delivery. This dynamic customization improves the customer experience by providing clear, attribute-based communication.

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Reply

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