M-a-d-m-a-n
M-a-d-m-a-n

Reputation: 25

Magento: Send notification when specific item has been ordered

We have been using a third party to pack and ship our packages. Therefore we don't see all orders that pass by anymore.

But there are several products that we have to supply manually ourselves, such as a digital gift card. Is there a possibility to have Magento send us an email when a specific SKU has been ordered by a customer? For instance to inform us that we need to create a gift card for a customer?

I don't want to see every order in our emailbox, just with some specific SKU's in it.

Thanks, Menno

Upvotes: 1

Views: 1068

Answers (2)

Alex Ivanov
Alex Ivanov

Reputation: 436

You might create a custom module for this functionality. So in the new module, you have to hook Observer event: checkout_onepage_controller_success_action. You can do like following as :

       <checkout_onepage_controller_success_action>
            <observers>
               <xxx_checkout_success>
                   <type>singleton</type>
                   <class>[Your Module Name]/observer</class>
                   <method>sendEmailToCustomerForSales</method>
               </xxx_checkout_success>
            </observers>
        </checkout_onepage_controller_success_action>

And, in this sendEmailToCustomerForSales() method, it can send an email to the customer according to a specific SKU.

Please refer this code:

public function sendEmailToCustomerForSales($observer) {
    $orderId = (int)current($observer->getEvent()->getOrderIds());
    $order = Mage::getModel('sales/order')->load($orderId);
    $itemCollection = $order->getItemsCollection();
    foreach($itemCollection as $item) {
        $_product = Mage::getModel('catalog/product')->load($item->getProductId());
        if($_product->getSku() == '[Your specific sku]') {
            /*send an email to the customer*/
        }
    }
}

Upvotes: 1

PixieMedia
PixieMedia

Reputation: 1548

Yes this can be achieved with a custom module. Create your module and add an event observer to it's config.xml;

<events>
        <checkout_onepage_controller_success_action>
            <observers>
                <copymein>
                    <type>singleton</type>
                    <class>dispatcher/observer</class>
                    <method>ccMyEmail</method>
                </copymein>
            </observers>
        </checkout_onepage_controller_success_action>
    </events>

Then in Model/Observer.php declare your function;

public function ccMyEmai($observer) {

    $order_ids = $observer->getData('order_ids');

    if(isset($order_ids)) {
        foreach ($order_ids as $order_id) :

        $sendToMe = false;
        $order = Mage::getModel('sales/order')->load($order_id);
            if (isset($order)) {

                 $orderItems = $order->getAllItems();
                 foreach ($orderItems as $_item) {

                     $product = Mage::getModel('catalog/product')->load($item->getData('product_id'));
                     if($product->getSku() == ('123' || '234' || '345')) { // Your SKUs
                              $sendToMe = true;
                     }

                 }
             }

         if($sendToMe) {
         $mail = Mage::getModel('core/email');
         $mail->setToName('Your name');
         $mail->setToEmail('[email protected]');
         $mail->setBody('Order number '.$order->getIncrementId().' has items that need action');
         $mail->setSubject('Order '.$order->getIncrementId().' needs attention');
         $mail->setFromName('Your from name');
         $mail->setFromEmail('[email protected]');
         $mail->setType('text');

                try {
                    $mail->send();
                } catch (Exception $e) {
                    Mage::log($e);
                }
          }



         endforeach;
    }
 }

Just a note that it would be more efficient to create a product attribute that isnt visible on the frontend that defines whether a product requires your attention - something along the lines of needs_attention Yes/No, then scan the ordered products for a yes value in that attribute. Much more manageable than hard coding the SKU's you are looking for ;)

Upvotes: 1

Related Questions