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.