2

I am making an extension that filters each order and send out an e-mail to each drop shipper in order.

Example

Order with 3 products. 2 products from drop shippers A and 1 product from drop shipper B.

Dropshipper A with should receive email containing only their 2 products and drop shipper B should receive email containing just their 1 product.

I want to maintain default Magento e-mail template functionality, so email is easy to update.

This is what i have so far.

Observer model that listen for <sales_order_place_after>

<events>
    <sales_order_place_after>
        <observers>
            <sales_order_place_after_handler>
                <type>model</type>
                <class>dropshipperemail/observer</class>
                <method>sendEmail</method>
            </sales_order_place_after_handler>
        </observers>
    </sales_order_place_after>
</events>

A method in observer that filter products and build array containing correct data.

public function sendEmail(Varien_Event_Observer $observer)
{
    $order = $observer->getOrder();     
    $orderItems = $order->getAllVisibleItems(); 

    foreach($orderItems as $item):          
        $product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($item->getProductId());
        if($dropshipper = $product->getAttributeText('dropshipper')):
            $list[$dropshipper][$product->getSku()] = array(
                'name' => $product->getName(),
                'sku' => $product->getSku(),
                'qty' => $item->getData('qty_ordered'),
                'price' => $item->getPrice()
            );
        endif;
    endforeach;
}

I can pass variables to transactional email and send fine.

But what i would like to do and i think is more elegant solution is to somehow rewrite queueNewOrderEmail and pass params with it (recipient email and product attribute to filter) like queueNewOrderEmail(false, $dropshipper, $recipient). Then i can do the actual filtering in render file

/app/design/frontend/base/default/template/email/order/items/order/default.phtml

Will this be working solution or am i missing something important?

0

Answering my own question, if other need similar solution. The suggested approach is working.

/app/code/local/DM/DropshipperEmail/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
        <DM_DropshipperEmail>
            <version>0.1.0</version>
        </DM_DropshipperEmail>
    </modules>
  <global>
        <helpers>
            <dropshipperemail>
                <class>DM_DropshipperEmail_Helper</class>
            </dropshipperemail>
        </helpers>
        <models>
            <dropshipperemail>
                <class>DM_DropshipperEmail_Model</class>
                <resourceModel>dropshipperemail_mysql4</resourceModel>
            </dropshipperemail>
        </models>
        <events>
            <sales_order_place_after>
                <observers>
                    <sales_order_place_after_handler>
                        <type>model</type>
                        <class>dropshipperemail/observer</class>
                        <method>sendEmail</method>
                    </sales_order_place_after_handler>
                </observers>
            </sales_order_place_after>
        </events>
    </global>
</config> 

/app/code/local/DM/DropshipperEmail/Model/Observer.php

class DM_DropshipperEmail_Model_Observer
{

    public function sendEmail(Varien_Event_Observer $observer)
        {
            $order = $observer->getOrder();     
            $orderItems = $order->getAllVisibleItems();
            $storeId = 6;   // lagersalg

            $recipients = array(
                'dropshipperA'      =>  'a@dropshipper.com',
                'dropshipperB'      =>  'b@dropshipper.com',
                'dropshipperC'      =>  'c@dropshipper.com',
            );

            foreach($orderItems as $item)
            {       
                $product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($item->getProductId());
                if($_dropshipper = $product->getAttributeText('dropshipper'))
                {
                    $list[$_dropshipper][$product->getSku()] = array(
                            'name' => $product->getName(),
                            'sku' => $product->getSku(),
                            'qty' => $item->getData('qty_ordered'),
                            'price' => $item->getPrice()
                    );
                }
            }

            if(count($list))
            {
                foreach($list as $dropshipper => $data)
                {
                    $order->queueNewOrderEmail(false, $dropshipper, $recipients[$dropshipper]);
                    Mage::log('Email sent to dropshipper ' . $dropshipper . ' [' . $recipients[$dropshipper] . '] for order #' . $order->getIncrementId(), null, 'dropshipper.log');
                }
            }

            return $this;

        }

}

I then modified queueNewOrderEmail in /app/code/local/Mage/Sales/Model/Order.php to get variable $dropshipper and $recipient.

I updated dropshipper e-mail with:

    if($recipient != ''):
        $emailInfo->addTo($recipient, $customerName);
    else:
        $emailInfo->addTo($this->getCustomerEmail(), $customerName);
    endif;

and changed e-mail template with:

    if($dropshipper != ''){
        $templateId = 30;
        $customerName = $dropshipper;   
    } elseif ($this->getCustomerIsGuest()) {
        $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId);
        $customerName = $this->getBillingAddress()->getName();
    } else {
        $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $storeId);
        $customerName = $this->getCustomerName();
    }

In email template i added the dropshipper variables with

{{layout handle="sales_email_order_items" order=$order dropshipper=$dropshipper}}

Then in /app/design/frontend/hvidtogfrit/lagersalg/template/email/order/items.phtml i am now able to filter products with $dropshipper = $this->getDropshipper() and compare this to product attribute.

Please note you also need to modify $this->getChildHtml('order_totals') to get the numbers to match the products, as you are now removing products from the order e-mail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.