Override notify mail address when certain products are ordered

  • Posts: 51
  • Thank you received: 3
  • Hikashop Business
2 weeks 6 days ago #371920

In the general Hikashop settings you can set an admin email address that receives the notification when a product is ordered. For certain products I want to use a different email address. Is it possible to override this setting?

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

  • Posts: 85686
  • Thank you received: 14047
  • MODERATOR
2 weeks 6 days ago #371922

Hi,

The proper way to do it would be via HikaMarket Multivendor:
www.hikashop.com/hikamarket-multi-vendor.html
You can configure one vendor per email address, and then select the vendor in each product.
Vendors will then be notified automatically by HikaMarket when an order is made with products linked to them and they'll only see their products in the order.
This is great so that you can have users purchase different products from different vendors in the same order and keep everything tidy.
And the main merchant can still receive his notification of orders with all the products in case this is necessary.

But it is also possible to override the receiver email address dynamically. For that, you want to edit the order admin notification ( sent to the admin when the order is created) and / or the payment notification (sent to the admin when the order is confirmed automatically by a payment plugin) via the menu System>Emails. This will then require custom PHP coding in the preload section at the bottom of the email edit form.

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

  • Posts: 51
  • Thank you received: 3
  • Hikashop Business
2 weeks 1 day ago #371982

Hello Nicolas,
I found the Order administrator notification email and searched in the preload version, but can't find the variable that holds the to-address. The variable $mail is used for the greeting at the top of the message.
Where in the file can I override the mail address of the receiver?

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

  • Posts: 85686
  • Thank you received: 14047
  • MODERATOR
2 weeks 20 hours ago #371985

Hello,

The variable you are looking for is hidden one layer up: inside the preload, the recipient is not on $mail yet, it is built from $data->customer->user_email after the preload returns. The order code does, in sequence:

1. $order->customer->user_email is replaced with the configured admin notification emails (the "Notification email" setting from Configuration > Emails).
2. Your preload runs with $data = the order (so $data->customer->user_email is currently those admin emails).
3. Just after the preload, HikaShop sets $order->mail->dst_email = $order->customer->user_email and sends.

So in the preload you do not touch $mail->dst_email (it would be overwritten); you change $data->customer->user_email and let HikaShop propagate it.

In HikaShop > System > Emails, edit "Order administrator notification" and paste this in the Preload section at the bottom of the form:

<?php
// Map a product (or its parent product, for variants) to the
// recipient email address that should receive the admin notification
// when that product is in the order. Replace the example ids and
// addresses with yours, or add as many rows as you need.
$override_map = array(
    42 => 'sales-team-a@example.com',
    99 => 'sales-team-b@example.com',
);

if(!empty($data) && !empty($data->cart->products) && !empty($data->customer)) {
    foreach($data->cart->products as $p) {
        $pid     = isset($p->product_id) ? (int)$p->product_id : 0;
        $ppid    = isset($p->product_parent_id) ? (int)$p->product_parent_id : 0;
        $to      = isset($override_map[$pid]) ? $override_map[$pid]
                 : (($ppid && isset($override_map[$ppid])) ? $override_map[$ppid] : null);
        if($to !== null) {
            // Full replacement of the recipient. If you would rather
            // ADD to the configured admin emails instead of replacing
            // them, do:
            //   $current = (array)$data->customer->user_email;
            //   $current[] = $to;
            //   $data->customer->user_email = $current;
            $data->customer->user_email = $to;
            break;
        }
    }
}
?>
A few notes:

- The match looks at both $p->product_id (so a variant id) and $p->product_parent_id (the main product), so listing the parent's id in the map is enough to cover all its variants. Add the variant id to the map only if you want a per-variant override.
- If you would rather match by category instead of by product, replace the inner block with a check on the product's categories. The simplest way is to do one DB query in the preload to grab the categories for the order's products and match against a category id whitelist. Happy to provide a snippet for that variant if you want.
- If multiple products in the same order match different recipients and you want all of them to be notified, build an array of addresses across the loop (do not break on the first match) and assign it to $data->customer->user_email at the end.

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

  • Posts: 51
  • Thank you received: 3
  • Hikashop Business
2 weeks 2 hours ago #371994

Thank you for the example Nicolas. It makes much more sense now.
If you can make an example based on category 41 that would be much appreciated.

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

  • Posts: 85686
  • Thank you received: 14047
  • MODERATOR
1 week 6 days ago #371995

Hello,

Sure, here is the category-based version. Same idea, but instead of matching on the product id we look up the categories of the products in the order and check whether at least one of them is the special category.

Replace the previous preload with this one:

<?php
// Map a category id to the recipient email address that should
// receive the admin notification when any product in that category
// is in the order. Add more rows here if you want to handle several
// categories with different recipients.
$category_override_map = array(
    41 => 'category-41-team@example.com',
);

if(!empty($data) && !empty($data->cart->products) && !empty($data->customer)) {
    // Collect both the variant id and its parent product id, since
    // categories in HikaShop are typically assigned on the main
    // product, not on each variant.
    $product_ids = array();
    foreach($data->cart->products as $p) {
        if(!empty($p->product_id))
            $product_ids[(int)$p->product_id] = (int)$p->product_id;
        if(!empty($p->product_parent_id))
            $product_ids[(int)$p->product_parent_id] = (int)$p->product_parent_id;
    }

    if(!empty($product_ids)) {
        $db = JFactory::getDBO();
        $db->setQuery(
            'SELECT DISTINCT category_id FROM ' . hikashop_table('product_category') .
            ' WHERE product_id IN (' . implode(',', $product_ids) . ')'
        );
        $order_categories = array_map('intval', (array)$db->loadColumn());

        foreach($category_override_map as $catId => $to) {
            if(in_array((int)$catId, $order_categories, true)) {
                // Full replacement of the recipient. If you would rather ADD
                // to the configured admin emails instead of replacing them, do:
                //   $current = (array)$data->customer->user_email;
                //   $current[] = $to;
                //   $data->customer->user_email = $current;
                $data->customer->user_email = $to;
                break;
            }
        }
    }
}
?>

Quick notes:

- The lookup goes through the product_category table, so it picks up every category a product is assigned to, not only the primary one. That way an order containing a product that is in category 41 plus other categories still routes to This email address is being protected from spambots. You need JavaScript enabled to view it..
- The "break" means the first matching category in the map wins. If you would rather notify several recipients when an order spans multiple mapped categories, collect them in an array as shown in the comment instead of breaking out.

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

  • Posts: 51
  • Thank you received: 3
  • Hikashop Business
1 week 1 day ago #372049

Thank you very much for you help Nicolas.
It's in the live webshop now and I will keep an eye on it in the next days.

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

Time to create page: 0.069 seconds
Powered by Kunena Forum