Magento 1.7 and 1.8 – How to Send Email to the Manufacturer of Goods

magento-1.7magento-1.8

We are building a website which somewhat acts like a marketplace. The website sells bags which are manufactured in different countries. The website owner acts as a mediator between the customer and the manufacturer

What i need is when a product is bought an email should be fired to the manufacturer of the bag. The email includes name of the product, its buyer, shipping address and contact information of the buyer.

If more than one product is bought different email should be fired to the manufacturer of those particular product.

Is there any extension available in the market which supports such a functionality. I tried to search but all i came across was marketplaces which our client can't afford.

Another way i thought was to enter a field for each product wherein the email of manufacturer is entered. When a prouduct is bought on checkout_process_complete event the email should be fired. As i am not that pro in magento custom extension building can anyone suggest me which way should i go ?

If i go for building a custom module from where i can start from ?

Any help will be appreciated

Thanks

Best Answer

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.

Related Topic