Usama Shabbier
Usama Shabbier

Reputation: 170

Display a custom text based on product category in WooCommerce Email

I'm trying to display some "conditional" custom text on the "Order Complete" email, which is sent to the customers when an order is marked as completed. I have used the following code.

Now, I want to make this code conditional, so this code should only be active for the "Gift" Category products. I couldn't find any appropriate Woocommerce functions for this particular functionality.

Any help would be appreciated.

This is what I have for now:

add_action( 'woocommerce_email_before_order_table', 
'bbloomer_add_content_specific_email', 20, 4 );

function bbloomer_add_content_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
    if ( $email->id == 'customer_completed_order' ) {
        echo '<p class="email-text-conditional">Thank you for your order with Adventure Clues, we have sent your recipient the gift card’</p>';
    }
}

Upvotes: 1

Views: 2152

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

Try the following:

add_action( 'woocommerce_email_before_order_table', 'email_before_order_table_conditional_display', 20, 4 );
function email_before_order_table_conditional_display( $order, $sent_to_admin, $plain_text, $email ) {
    // Only for "commpeted" order status notification
    if ( 'customer_completed_order' !== $email->id ) return;

    foreach( $order->get_items() as $item ){
        if( has_term( "Gifts", "product_cat", $item->get_product_id() ) ){
            echo '<p class="email-text-conditional">Thank you for your order with Adventure Clues, we have sent your recipient the gift card.</p>';
            break;
        }
        if( has_term( "Clothes", "product_cat", $item->get_product_id() ) ){
            echo '<p class="email-text-conditional">Thank you for your order with Adventure Clues, we are managing your Clothes.</p>';
            break;
        }
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Upvotes: 4

Related Questions