Change Vendor Email in Customer Order Notifications

Hello everyone, I am using Multi-Vendor v4.16.1 and I need to change the “from” name & address in all customer notification emails. Currently, they are being sent from the vendor’s registered email address, but I want to send them from my company’s email address.

When I still had an active support plan I requested support from CS-Cart helpdesk and I received the following solution:

FILE: /app/addons/my_changes/init.php:
fn_register_hooks(
‘mailer_create_message_before’,
‘mailer_send_pre’
);

FILE: /app/addons/my_changes/func.php:
use Tygh\Registry;
use Tygh\Enum\SiteArea;

function fn_my_changes_mailer_create_message_before($mailer, &$message, $area, $lang_code, $transport, $builder)
{
$company_id = !empty($params[‘company_id’]) ? $params[‘company_id’] : 0;
if ($company_id && SiteArea::isStorefront($area) && strpos($message[‘template_code’], ‘order_notification’) !== false) {
$message[‘reply_to’] = ‘default_company_orders_department’;
if (is_array($message[‘from’])) {
$message[‘from’][‘name’] = Registry::get(‘settings.Company.company_name’);
}
}
}

function fn_my_changes_mailer_send_pre($mailer, $transport, $message, $area, $lang_code) {
$from = $message->getFrom();
$company_id = $message->getCompanyId();
$data = $message->getData();
if ($company_id && strpos($data[‘template_code’], ‘order_notification’) !== false) {
$message->setFrom(key($from), Registry::get(‘settings.Company.company_name’));
}
}

The above code does change the from address, but whenever an order is placed I am receiving the PHP error:
“Notice: Undefined index: template_code in /…/app/addons/my_changes/func.php on line 21”

How could I solve this? Thank you for your help!

Hi!

Please replace the following code:

$company_id = $message->getCompanyId();
$data = $message->getData();
if ($company_id && strpos($data[‘template_code’], ‘order_notification’) !== false) {

with this one:

if (
    $message->getCompanyId()
    && $message->getId()
    && preg_match('/order_notification/', $message->getId())
) {
1 Like

Hello, thank you very much! It worked perfectly!

1 Like

Hello @CS-Cart_team

The code provided above works for most of the emails. Almost all of them are being sent from the marketplace, with the exception of a few emails sent by a few addons, for example: RMA.

Is there a way to modify this code for all emails including addons? Or will I have to modify the code for each addon?

Hi!

If you need to cover all the cases, simply remove this one condition:

&& preg_match('/order_notification/', $message->getId())

This changes the behavior for all cases, not just when an email is sent regarding an updated order.

1 Like