Building this from scratch shouldn't be to difficult. Basically you need a few things
- An observer that observes some event which is triggered when an order is completed
- Some email functionality
- A field per product indicating who to email to
If you're not familiar with creating extensions take a look at this Smashing Magazine tutorial or Alan Storm's Hello World tutorial. They both cover a good portion of the different kinds of elements an extension can have tho you will probably only need a Helper and a Model.
First we will start with an observer
. In this case I would recommend the sales_order_invoice_save_after
event. This is triggered whenever an invoice is created so you can be fairly sure the items you're emailing to the manufacturer are paid for by the customer.
We will need a config.xml
that contains, amongst some other stuff, the observer
<?xml version="1.0"?>
<config>
<global>
<models>
<[module]>
<class>[Namespace]_[Module]_Model</class>
</[module]>
</models>
<events>
<sales_order_invoice_save_after>
<observers>
<[module]_invoice_email>
<type>singleton</type>
<class>[module]/observer</class>
<method>sendEmail</method>
</[module]_invoice_email>
</observers>
</sales_order_invoice_save_after>
</events>
</global>
</config>
Here we've added a models node so to have access to the models class in our module and an observer that listens to the sales_order_invoice_save_after
event calling the Observer model, method sendMail
. You can change the names of the observer and method if you like.
Next up the Observer
model
class [Namespace]_[Module]_Model_Observer
{
/**
* Send email to appropriate manufacturer
* @param Varien_Event_Observer $observer
* @return this
*/
public function sendMail(Varien_Event_Observer $observer)
{
$order = $observer->getOrder();
$email_stacks = array();
foreach ($order->getAllItems() as $item)
{
// only add simple products to email, not a configurable product for example
if ($item->getData('product_type')!=Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)
continue;
$manufacturer = $item->getProduct()->getData('manufacturer_email');
if (!isset($email_stacks[$manufacturer])) $email_stacks[$manufacturer] = "";
$email_stacks[$manufacturer] .= "{$item->getData('sku')} {$item->getData('qty_ordered')}x<br/>\n";
}
// here goes the email part
return $this;
}
}
So what happens here. We get the order from the observer data, extract the items and loop through them only processing the simple products since a configurable product is no use to the manufacturer.
For each simple product we get the manufacturer_email
attribute (which you should add manually to each product) and use it as an index key adding a string with SKU and quantity along with it.
This leaves us with an array that has one or more indexes which represents each manufacturer to be mailed and per key a string with the items for that manufacturer.
Now onwards to email that array! Inchoo has a great article on custom emails in Magento so read that to edit your config.xml
. I will just add the code part for the model and the email template you could use. Earlier I talked about a Helper class you would need. I think your email template won't work unless you have a basic helper in your method so just to be sure and go ahead and create it.
app/code/.../[Namespace]/[Module]/Helper/Data.php
class [Namespace]_[Module]_Helper_Data extends Mage_Core_Helper_Abstract
{
}
Your Observer
model, remember the // here goes the email part
part? Below code goes there.
foreach ($email_stacks as $emailaddress => $items_string)
{
$template = Mage::getModel('core/email_template')->loadDefault('[the_name_of_your_template]');
$template->send($emailaddress, null, array(
'orderid' => $order->getIncrementId(),
'items' => $items_string
));
}
this will send an email per manufacturer loading the email template with the orderid as reference and the items string we've created earlier. Any other information you might want in your email you can retrieve from the order and pass along in the array. The name of the key you assign it to will correspond to {{var thekeyname}}
in the email template later on.
And for the email template, something like this should do it.
<!--@subject New items order @-->
<div>
<h1>Order {{var orderid}}</h1>
<p>{{var items}}</p>
</div>
And that should be it!
Just remember this is untested code I pieced together so make sure to test it thoroughly before using it in an production environment. It will probably need a tweak here or there.