0

I have multiple status like

1: Pending 
2: Processing
3: cancelled
4: completed

When order is cancelled used get defualt status notification email, instead i want to use different template when order status is canclled.

Please help me with way to do same, i am bit new in magento , will observer help ? please help way out

2

1 Answer 1

1
+50

You could pass new variables via Observer to use in the email template using knockout {{if}} to show some parts of email template...

In your observer add conditions to pass the variable depending the status

 $orderData = $transport->getData('order');

I assume based in your order Data can add some conditions to pass the variable you need to use in the email Template.

if(//Here you search for the status in the order){

    $transport['processing'] = 'processing';}else{
    
    $transport['Pending'] = 'Pending';}

1.- Inside your admin Marketing > Email Templates. Create a new template. Then add the condition to show which part of the template need to display in the email.

 <!--@subject {{trans "Your %store_name order confirmation" store_name=$store.getFrontendName()}} @-->
    <!--@vars {
    "var order.getBillingAddress().getName()":"Guest Customer Name",
    "var store.getFrontendName()":"Customer Name",
    "var is_pickup_order":"Check if order is a pickup order",
    "var store_email |raw":"Defined Email Address for this type of mails",
    "var store_phone |raw":"Defined Phone Number in the Store Information",
    "var store_hours |raw":"Defined Opening Hours Phone Number in the Store Information",
    "var order.increment_id |raw":"Order Id",
    "var order.getCreatedAtFormatted(2) |raw":"Get the date of order creation",
    "var order.getEmailCustomerNote()":"Email Order Note",
    "var formattedBillingAddress|raw":"Billing Address",
    "var order.getIsNotVirtual()":"Check if shipment exists",
    "var pickupAddress|raw":"Pickup Location Address",
    "var formattedShippingAddress|raw":"Customer Shipping Address",
    "var payment_html|raw":"Payment Details",
    "var order.getShippingDescription()":"Shipping Description",
    "var shipping_msg":"Shipping message",
    "layout handle=\"sales_email_order_items\" order=$order area=\"frontend\"":"Order Items Grid"
    } @-->
{{if processing}} 
        
    
    {{template config_path="design/email/header_template"}}
    
    <table>
        <tr class="email-intro">
            <td>
                <p class="greeting">{{trans "%name," name=$order.getBillingAddress().getName()}}</p>
                <p>
                    {{trans "Thank you for your order from %store_name." store_name=$store.getFrontendName()}}
                    {{if is_pickup_order}}
                        {{trans "We will send you a notification once your items are ready for pickup."}}
                    {{else}}
                        {{trans "Once your package ships we will send an email with a link to track your order."}}
                    {{/if}}
                    {{trans 'If you have questions about your order, you can email us at <a href="mailto:%store_email">%store_email</a>' store_email=$store_email |raw}}{{depend store_phone}} {{trans ' or call us at <a href="tel:%store_phone">%store_phone</a>' store_phone=$store_phone |raw}}{{/depend}}.
                    {{depend store_hours}}
                        {{trans 'Our hours are <span class="no-link">%store_hours</span>.' store_hours=$store_hours |raw}}
                    {{/depend}}
                </p>
            </td>
        </tr>
        <tr class="email-summary">
            <td>
                <h1>{{trans 'Your Order <span class="no-link">#%increment_id</span>' increment_id=$order.increment_id |raw}}</h1>
                <p>{{trans 'Placed on <span class="no-link">%created_at</span>' created_at=$order.getCreatedAtFormatted(2) |raw}}</p>
            </td>
        </tr>
        <tr class="email-information">
            <td>
                {{depend order.getEmailCustomerNote()}}
                <table class="message-info">
                    <tr>
                        <td>
                            {{var order.getEmailCustomerNote()|escape|nl2br}}
                        </td>
                    </tr>
                </table>
                {{/depend}}
                <table class="order-details">
                    <tr>
                        <td class="address-details">
                            <h3>{{trans "Billing Info"}}</h3>
                            <p>{{var formattedBillingAddress|raw}}</p>
                        </td>
                        {{depend order.getIsNotVirtual()}}
                        <td class="address-details">
                            <h3>{{trans "Shipping Info"}}</h3>
                            {{if is_pickup_order}}
                                <h4>{{trans "Pickup Location"}}</h4>
                                <p>{{var pickupAddress|raw}}</p>
                                <h4>{{trans "Recipient Address"}}</h4>
                            {{/if}}
                            <p>{{var formattedShippingAddress|raw}}</p>
                        </td>
                        {{/depend}}
                    </tr>
                    <tr>
                        <td class="method-info">
                            <h3>{{trans "Payment Method"}}</h3>
                            {{var payment_html|raw}}
                        </td>
                        {{depend order.getIsNotVirtual()}}
                        <td class="method-info">
                            <h3>{{trans "Shipping Method"}}</h3>
                            <p>{{var order.getShippingDescription()}}</p>
                            {{if shipping_msg}}
                            <p>{{var shipping_msg}}</p>
                            {{/if}}
                        </td>
                        {{/depend}}
                    </tr>
                </table>
                {{layout handle="sales_email_order_items" order=$order}}
            </td>
        </tr>
    </table>
    
    {{template config_path="design/email/footer_template"}}

    {{/if}}

    {{if completed}} 
        /// Your custom email template and its variables here
    {{/if}}

... etc.

if the variable has text it will be considered as true an use the part inside of the email template you need... Also no need to declare the var inside @vars. your observer will pass it without problem

This could be a fast solution. Another should be create a new module EmailSystem to define the template to use depending the case...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.