Multiple email addresses

  • Posts: 11
  • Thank you received: 0
  • Hikashop Business
1 week 2 days ago #371810

-- HikaShop version -- : 6.4.1
-- Joomla version -- : 3.10.12
-- PHP version -- : 7.4.33

Hi,
I have a question regarding order confirmation emails in HikaShop.

I want to keep the current order confirmation emails unchanged, but also send a copy of the order confirmation to an additional email address for specific locations only.

For example:
Essex
Norfolk
Newport News

Orders placed from these three locations should also send a copy to another email.
Is there a built-in way in HikaShop to conditionally add a CC/BCC recipient based on the selected location or customer group?

Please Log in or Create an account to join the conversation.

  • Posts: 85609
  • Thank you received: 14016
  • MODERATOR
1 week 1 day ago #371814

Hello,

The cleanest way to do this is a small custom HikaShop plugin (group "hikashop") that listens to the onBeforeMailSend event. That event fires for every email HikaShop sends out, gives you the full order context, and lets you add a CC or BCC on the fly without modifying any email template. The existing confirmation emails stay exactly as they are; you only ADD extra recipients on the orders that match your locations.

Plugin file (extra_recipients.php):

<?php
defined('_JEXEC') or die('Restricted access');

class plgHikashopExtra_recipients extends JPlugin {

    public function onBeforeMailSend(&$mail, &$mailer) {

        // Only act on order creation notifications. This skips status-change
        // emails, account-creation emails, password resets, etc.
        if (empty($mail->mail_name)) return;
        if (!in_array($mail->mail_name, array(
                'order_creation_notification',
                'order_admin_notification'))) {
            return;
        }

        // The order object is in $mail->data for order-related emails.
        $order = isset($mail->data) ? $mail->data : null;
        if (empty($order) || empty($order->shipping_address)) return;

        // Match on the shipping address. address_state usually holds the
        // state name or code, but you can also test address_city or
        // address_post_code if your "location" lives there.
        $state = isset($order->shipping_address->address_state)
            ? trim($order->shipping_address->address_state) : '';
        $city  = isset($order->shipping_address->address_city)
            ? trim($order->shipping_address->address_city) : '';

        $locations = array('Essex', 'Norfolk');

        $match = false;
        foreach ($locations as $loc) {
            if (strcasecmp($state, $loc) === 0 ||
                strcasecmp($city,  $loc) === 0) {
                $match = true;
                break;
            }
        }
        if (!$match) return;

        // Add the extra recipient. Replace with the Newport News address.
        // Use addBCC if you don't want the customer to see it in the
        // headers; use addCC if you do.
        $mailer->addBCC('newport.news@example.com', 'Newport News');
    }
}
The matching plugin manifest (extra_recipients.xml) is a standard Joomla plugin descriptor with <extension type="plugin" group="hikashop">. Zip the two files together and install it from Extensions > Manage > Install, then enable the plugin in Extensions > Plugins.

A few notes:

If your "location" really lives in a different field, like a postcode prefix, the country code, a custom address field, etc you need to replace the $state / $city test with whatever you need. Every column of the address row is available on $order->shipping_address.

If you want different recipients per location (Essex -> one address, Norfolk -> another), turn the locations array into a map and pick the recipient per match instead of using a single addBCC at the end.

For the customer's copy (mail_name == 'order_creation_notification') vs the merchant's copy ('order_admin_notification'), you can decide which one(s) you want the extra recipient added to by tightening the in_array() above.

Please Log in or Create an account to join the conversation.

Time to create page: 0.055 seconds
Powered by Kunena Forum