1

I am trying to send an email when order is canceled. I followed this tutorial:

http://www.atwix.com/magento/emails-for-custom-events/

Here is my code i need help setting it up becouse now it is not working:

Local.xml:

1.0.0

<global>
    <models>
        <cancelemail>
            <class>Ylli_Cancelemail_Model</class>
        </cancelemail>
    </models>
    <events>
        <sales_order_save_commit_after>
            <observers>
                <Ylli_Cancelemail_Model_Observer>
                    <type>singleton</type>
                    <class>Ylli_Cancelemail_Model_Observer</class>
                    <method>invoicedStatusChange</method>
                </Ylli_Cancelemail_Model_Observer>
            </observers>
        </sales_order_save_commit_after>
    </events>
    <template>
        <email>
            <custom_order_tpl module="Ylli_Cancelemail">
                <label>Status Mail Invoice</label>
                <file>statusmail_canceled.html</file>
                <type>html</type>
            </custom_order_tpl>
        </email>
    </template>
</global>

Observer.php

class Ylli_Cancelemail_Model_Observer {
    public function invoicedStatusChange($event)
    {
        $order = $event->getOrder();
        var_dump($order);
        Mage::log('worked', null, 'cancelemail.log');
        $orderStatus = $order->getStatus();
        if ($order->getState() == Mage_Sales_Model_Order::STATE_CANCELED)
            $this->_sendStatusMail($order);
    }


private  function _sendStatusMail($order)
{
    $emailTemplate  = Mage::getModel('core/email_template');

    $emailTemplate->loadDefault('custom_order_tpl');
    $emailTemplate->setTemplateSubject('Your order was holded');

    // Get General email address (Admin->Configuration->General->Store Email Addresses)
    $salesData['email'] = Mage::getStoreConfig('trans_email/ident_general/email');
    $salesData['name'] = Mage::getStoreConfig('trans_email/ident_general/name');

    $emailTemplate->setSenderName($salesData['name']);
    $emailTemplate->setSenderEmail($salesData['email']);

    $emailTemplateVariables['username']  = $order->getCustomerFirstname() . ' ' . $order->getCustomerLastname();
    $emailTemplateVariables['order_id'] = $order->getIncrementId();
    $emailTemplateVariables['store_name'] = $order->getStoreName();
    $emailTemplateVariables['store_url'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
    $emailTemplate->send($order->getCustomerEmail(), $order->getStoreName(), $emailTemplateVariables);
}

Any help is appreciated. thanks in advace

  • can you please tell is event is fire ya email is nit fired – Amit Bera Jan 16 '15 at 16:52
  • No the event is not fired, i have tried to make a log file but it does not create it. so i think it is an event problem. can you give me some debugging help ? – Ylli Jan 17 '15 at 8:45
5

I want to have a cms page where I will list all products that have special_offer dropdown attribute with yes value. I found something here but this is work only with the Yes/No attribute. I need to use dropdown attribute because I want to use this attribute in the layered navigation too.

So how I can display dropdown special_offer attribute where options are Yes and No?

This is the public function where the products are sorted:

public function getFeaturedProducts()
{
    $resource = Mage::getSingleton('core/resource');
    $read = $resource->getConnection('catalog_read');
    $productEntityIntTable = (string) Mage::getConfig()->getTablePrefix() . 'catalog_product_entity_int';
    $eavAttributeTable = $resource->getTableName('eav/attribute');
    $categoryProductTable = $resource->getTableName('catalog/category_product');

    $select = $read->select()
        ->distinct(true)
        ->from(array('cp'=>$categoryProductTable), 'product_id')
        ->join(array('pei'=>$productEntityIntTable), 'pei.entity_id=cp.product_id', array())
        ->joinNatural(array('ea'=>$eavAttributeTable))
        ->where('pei.value=1')
        ->where('ea.attribute_code="special_offer"');

    $res = $read->fetchAll($select);

    return $res;
}

There issue with fetch data from event.you can get your passing event data using $observer->getEvent()->getData('data_object') for getting the data of event data.

According magento,**_save_commit_after event on dispatch on below code at class Mage_Core_Model_Abstract

Mage::dispatchEvent($this->_eventPrefix.'_save_commit_after', $this->_getEventData());

According to this event it pass data using $this->_getEventData() for getting data you need to use $observer->getEvent()->getData('data_object') at Observer file.

For getting old data use getOrigData()

Example:

$Olddata= $observer->getEvent()->getData('data_object')->getOrigData()

and for getting current change data getData()

Example:

$new_data = $observer->getEvent()->getData('data_object')->getData();

This is provide current change data in array format;

public function invoicedStatusChange(Varien_Event_Observer $observer)
{
    $original_data = $observer->getEvent()->getData('data_object')->getOrigData();
    $new_data = $observer->getEvent()->getData('data_object')->getData();
    Mage::log($original_data);
    Mage::log($new_data);
    if (($original_data['state'] !== $new_data['state']) && ($new_data['state'] == Mage_Sales_Model_Order::STATE_CANCELED)) {
        Mage::log('Yes+cancel');
        /**
        * This is where you can place your process code
        */
    }
}

Previous data: print in log file:

2015-01-18T12:48:31+00:00 DEBUG (7):
 Array
(
    [entity_id] => 192 //order id
    [state] => new
    [status] => pending
...
[subtotal_incl_tax] => 372.3800
    ....
    [base_shipping_hidden_tax_amount] => 0.0000
)

Current change data:

2015-01-18T12:48:31+00:00 DEBUG (7):
Array
(
    [entity_id] => 192 //order id
    [state] => canceled
    [status] => canceled

    ...
    [subtotal_incl_tax] => 372.3800
    .......
    [base_shipping_hidden_tax_amount] => 0.0000
)

Full Module:

app\code\local\Ylli\Cancelemail\etc\config.xml

<?xml version="1.0"?>
    <config>
        <modules>
            <Ylli_Cancelemail>
                <version>0.1.0</version>
            </Ylli_Cancelemail>
        </modules>
    <global>
        <helpers>
            <cancelemail>
                <class>Ylli_Cancelemail_Helper</class>
            </cancelemail>
        </helpers>
        <models>
            <cancelemail>
                <class>Ylli_Cancelemail_Model</class>
                <resourceModel>cancelemail_mysql4</resourceModel>
            </cancelemail>
        </models>
        <events>
            <sales_order_save_commit_after> <!-- identifier of the event we want to catch -->
                <observers>
                    <sales_order_save_commit_after_handler> <!-- identifier of the event handler -->
                        <type>model</type> <!-- class method call type; valid are model, object and singleton -->
                        <class>cancelemail/observer</class> <!-- observers class alias -->
                        <method>invoicedStatusChange</method>  <!-- observer's method to be called -->
                        <args></args> <!-- additional arguments passed to observer -->
                    </sales_order_save_commit_after_handler>
                </observers>
            </sales_order_save_commit_after>
        </events>
    </global>
</config>

Ylli\Cancelemail\Model\Observer.php

<?php
class Ylli_Cancelemail_Model_Observer
{
    public function invoicedStatusChange(Varien_Event_Observer $observer)
    {
        $original_data = $observer->getEvent()->getData('data_object')->getOrigData();
        $new_data = $observer->getEvent()->getData('data_object')->getData();
        Mage::log($original_data);
        Mage::log($new_data);
        if (($original_data['state'] !== $new_data['state']) && ($new_data['state'] == Mage_Sales_Model_Order::STATE_CANCELED)) {
            Mage::log('Yes+cancel');
            /**
            * This is where you can place your process code
            */
        }
    }
}

app/code/local/Ylli/Cancelemail/Helper/Data.php

<?php
class Ylli_Cancelemail_Helper_Data extends Mage_Core_Helper_Abstract
{
}

app/etc/modules/Ylli_Cancelemail.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Ylli_Cancelemail>
            <codePool>local</codePool>
            <active>true</active>
        </Ylli_Cancelemail>
    </modules>
</config>

Thank you

  • Thanks for the code, when i try it in localhost it works great but when i try it on my live site it does not work, the event is not fired at all. – Ylli Jan 19 '15 at 9:26
  • it may be your live system cache issue – Amit Bera Jan 19 '15 at 9:32
  • 1
    I tried flushing all cache and reindexing but nothing worksn any other sugestion? – Ylli Jan 19 '15 at 10:44
  • no ,i have test it on live server , it is worked perfectly – Amit Bera Jan 19 '15 at 10:50
  • Can you please send me the module to make a comparison with my module. – Ylli Jan 19 '15 at 10:58

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.