7

I'm currently configuring my transactional emails for Magento 2 and since I am in germany and want to ship some parts to a partner inside the EU i can place those orders without calculating taxes for that.

Therefore my EU partners have an own customer group without any tax display.

So make everything clear and don't get in trouble with the Tax institute here in germany i have to declare those orders with an special text inside the email and invoice.pdf.

Is there any way to implement an if-statement to call something like:

if customergroup == EU_Partner then display TEXT ?

Thanks in advance


EDIT

I found something while searching for a solution.

{{depend order.customer_group_id}} TEXT {{/depend}}

Does this work on Magento 2 and if so, how do i declare the customer_group_id ?

2
  • are you talking about in email template in backend? Feb 21, 2017 at 13:04
  • yes. I want to change the template with a custom variable or block which gets displayed if the customer got a specific group.
    – Marcel H.
    Feb 21, 2017 at 13:09

2 Answers 2

21

We cannot use the if condition like:

{{if order.customer_group_id == 1}}
     <p>Bla Bla Bla</p>
{{/if}}

We can find the customer group id in sales_order table. I also notice that the customer group id is a int type.

  1. In your case, we can use if and depend directives. However, we need to set a "flag" for our special case to order Object (for example: Magento\Checkout\Model\Type\Onepage::saveOrder()).

For example:

//We need to check if the order has EU customer
$order->setData('eu_customer');

In your email, we can check property directly:

{{if order.eu_customer}}
     <p>Bla Bla Bla</p>
{{/if}}
  1. We can use block technique. For example, insert this block in your template:

    {{block class='Magento\Framework\View\Element\Template' area='frontend' template='Vendor_Module::email/specificcustomer/info.phtml' order=$order}}

In your custom template, we can check whatever you want.

<?php if($this->getData('order')->getCustomerGroupId()==1) {
        echo "<p>Bla Bla Bla</p>"; 
      }
?>

See more here: vendor/magento/module-sales/view/frontend/email/shipment_new.html

6
  • You are my hero. The Block-technique work perfect, thanks alot :)
    – Marcel H.
    Feb 21, 2017 at 18:18
  • The option #1 worked for me. Many thanks for the input.
    – medina
    Aug 6, 2019 at 6:25
  • Magento 1 Like same how can i add website filter in email template like if website id == 1 first condition else second condition thank you @Khoa TruongDinh
    – zus
    Mar 18, 2020 at 4:52
  • could you please check this stackoverflow.com/questions/62694678/… Jul 6, 2020 at 7:04
  • Could you show me where can I add the custom info.phtml?
    –  coinhndp
    Oct 29, 2020 at 15:47
4

Following are the solutions for new version

Solution 1: use depend tag which check whether that variable has some value or not. You will need to add following code in your email template

{{depend shipping_company}}
  <tr>
    <td>
      <table>
        <tr>
           <td>
               <h1>Your shipment is on its way.</h1>
           </td>
           <td>
             <img src="{{media url=wysiwyg/logo-dpd.png}}" alt="" />
           </td>
         </tr>
        </table>
     </td>
   </tr>
 {{/depend}}

Solution 2: Block technique for advance conditioning

{{block class='Magento\\Framework\\View\\Element\\Template' 
               area='frontend' template='Codedecorator_Learn::email/shipping_company.phtml' shipping_company=$shipping_company}}

Add above block code in your email template and create new phtml template in your plugin like following:

  <?php 
            if($block->getData('shipping_company') !='dpd') {
                    echo $block->getData('shipping_company'); 
            

}?>

for more detail you can visit following blog

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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