0

We currently have, on our site, the option to make payment either by credit card or Bank transfer. We would like a way for the admin to receive an alert email when payment is made using the Bank transfer method. We can get the system to send an email every time an order is made but we'd like to send a specific one, just for Bank transfer orders.

Is there a way to do this?

Thank you.

0

There are many different ways to do this. One way is to make an after plugin for place order like this:

app/code/StackExchange/BankTransferNotify/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"
>
    <type name="Magento\Quote\Api\CartManagementInterface">
        <plugin type="StackExchange\BankTransferNotify\Plugin\NotifyAdminOnBankTransfer" name="NotifyAdminOnBankTransfer" />
    </type>
</config>

app/code/StackExchange/BankTransferNotify/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="StackExchange_BankTransferNotify" setup_version="0.1.0">
    </module>
</config>

app/code/StackExchange/BankTransferNotify/Plugin/NotifyAdminOnBankTransfer.php

<?php
declare(strict_types=1);

namespace StackExchange\BankTransferNotify\Plugin;

use Magento\OfflinePayments\Model\Banktransfer;
use Magento\Quote\Api\CartManagementInterface;
use Magento\Sales\Api\OrderRepositoryInterface;

class NotifyAdminOnBankTransfer
{
    /**
     * @var OrderRepositoryInterface
     */
    private OrderRepositoryInterface $orderRepository;

    public function __construct(
        OrderRepositoryInterface $orderRepository
    ) {
        $this->orderRepository = $orderRepository;
    }

    public function afterPlaceOrder(CartManagementInterface $cartManagement, $orderId)
    {
        $order = $this->orderRepository->get($orderId);

        if ($order->getPayment()->getMethod() == Banktransfer::PAYMENT_METHOD_BANKTRANSFER_CODE) {
            //
            // Send Email here......
            //
        }

        return $orderId;
    }
}

app/code/StackExchange/BankTransferNotify/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'StackExchange_BankTransferNotify',
    __DIR__
);

Enable module

Run setup upgrade

Good luck!

1
  • Hello. There seems to be a parse error on line 15 of the NotifyAdminOnBankTransfer.php, the error message reads as follows reads as follows: PHP Parse error: syntax error, unexpected 'OrderRepositoryInterface' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /var/www/vhosts/powerdev.magedemo.co.uk/httpdocs/app/code/OrderAlert/BankTransferNotify/Plugin/NotifyAdminOnBankTransfer.php on line 15 – CJNotts Oct 21 '20 at 11:56

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.