0

In my store there are two payment methods enabled. One of these is the "Bank Transfer". I would like to be able to add the bank details in the "New order email template" only when the customer has choosen the "Bank Transfer" payment method.

Is there a way to check this information inside the template with something like

{{if payment == 'banktransfer'}}
 Bank Details
....
....
{{endif}}

or is there a way to use multiple template based on the payment method specified by the customer?

1

I managed to do it by including a block into the template. In the middle of my template I inserted this:

{{block type='core/template' area='frontend' template='email/order/bankdetails.phtml' order=$order store=$store}}

insid the bankdetails.phtml file I perform this check to decide whether to include bank details or not:

<?php $_order = $this->getOrder(); ?>
<?php if(!is_null($_order) && $_order->getPayment()->getMethodInstance()->getCode() == 'banktransfer') : ?>

....
bank details
....

<?php endif; ?>
1

I had the same problem as you. In my solution:

I have two payment methods: 'banktransfer' and 'cashondelivery'. These are the codes of the methods. In email template files getting payment method code you have to insert this:

{{var order.getPayment().getMethodInstance().getCode()}}

In my order_new.html template file the block of the "Bank Details" was:

<table>
  ...
</table>

I inserted a class attribute into table element:

<table class="{{var order.getPayment().getMethodInstance().getCode()}}-email">
  ...
</table>

My template file uses inline CSS file (at the begining):

{{inlinecss file="email-inline.css"}}

In this email skin/frontend/base/default/css/email-inline.css I inserted this:

.cashondelivery-email
{
   display: none;
} 

It results if the code of the payment method is 'cashondelivery', the block of the "Bank Details" won't be displayed.

If later there will be more (not banktransfer-like) method, I will only add some class selectors to CSS file.

If in your shop 'Guest Checkout' is allowed, it is worth repeatig the above changes in order_guest_new.html template files.

0

I'm just going to leave this answer here for Magento 2...

I have set up a custom module which includes the ability to add bank details to the email. There are many resources online which detail how to set up a custom module so I am just going to add the specific files related to the question here:

In the email template add the following:

{{layout area="frontend" handle="banktransfer_email_template_message" order=$order }}

Then in your module add the following file:

app/code/Vendor/ModuleName/view/frontend/layout/banktransfer_email_template_message.xml

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"
  label="Bank Transfer Email Template Message"
  design_abstraction="custom">
<body>
    <block class="Vendor\ModuleName\Block\View"
           name="vendor.modulename.email.template.message"
           template="Vendor_ModuleName::email/banktransfer/message.phtml">
    </block>
</body>

Then add the View class:

app/code/Vendor/ModuleName/Block/View.php

<?php

namespace Vendor\ModuleName\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;

class View extends Template
{
    /**
     * @var ScopeConfigInterface
     * */
    private $scopeConfig;

    public function __construct(
        Context $context,
        array $data = []
    ) {
        parent::__construct(
            $context,
            $data
        );
        $this->scopeConfig = $context->getScopeConfig();
    }

    /**
     * @return mixed
     */
    public function getAdditionalInfoMessageFromConfig() {
    /** I have added a custom field to admin where I retrieve the 
    * bank details but you can enter your bank details in the text 
    * field already given in 
    * Stores/Configuration/Sales/Payment Methods/Bank Transfer/Instructions text field */
        return $this->scopeConfig->getValue('payment/banktransfer/instructions', ScopeInterface::SCOPE_STORE);
    }

}

Add the template file:

app/code/Vendor/ModuleName/view/frontend/templates/email/banktransfer/message.phtml

<?php
/** @var \Vendor\ModuleName\Block\View $block */
?>
<div class="bank-transfer-email-message">

    <?php
    $var = $block->getData('order')->getPayment()->getMethodInstance()->getCode();

    if ($var == 'banktransfer') {
        echo $block->getAdditionalInfoMessageFromConfig();
    }
    ?>

</div>

Hope this helps someone in the future!

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.