Search code examples
phpmysqlmagentocontrollerobservers

Showing error on my custom module in magento


I working now in magento for developing a module to check the voucher code used or not. The details are stored in a new table. In my config.xml, I specified the observer page for fetching the details from db table. But I don't know the exact use of observer page in magento. Can I use observer page for this usage.

But it proceed to an error I checked the log file:

a:5:{i:0;s:203:"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=' at line 1";i:1;s:1677:"#0 C:\wamp\www\Mymagento\lib\Varien\Db\Statement\Pdo\Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)

My observer.php file is also shown below

class Module_Voucher_Model_Observer {
    public function __contruct() {
        $coupon_code = trim(Mage::getSingleton("core/session")->getData("coupon_code"));
    }

    public function getresultofVoucher($coupon_code) {
        $resource = Mage::getSingleton('core/resource');
        $readConnection = $resource->getConnection('core_read');
        $table = "voucher_code_status_table";
        $query = 'SELECT * FROM ' . $table. 'WHERE value='.$coupon_code;
        $results = $readConnection->fetchAll($query);

        return $results;
    }
}

and my Indexcontroller.php shown below:

class Module_Voucher_IndexController extends Mage_Core_Controller_Front_Action {
    /**
    * Coupon code checking
    **/
    public function indexAction() {
        $this->loadLayout();
        $block = $this->getLayout()->createBlock('Mage_Core_Block_Template','vouchercode',
        array('template' => 'voucher/vouchercode.phtml')
        );
        $this->getLayout()->getBlock('left')->append($block);
        $this->renderLayout();
    }

    public function CheckAction() {
        $coupon_code = $this->getRequest()->getParam('coupon_code');
        //$coupon_code ='63663';
        if ($coupon_code != '') {
            Mage::getSingleton("core/session")->setData("coupon_code",$coupon_code);     //("checkout    /session")->
        }
        else {
            //
            //echo 'error : Voucher code issue';
        }
        if ($this->getRequest()->getParam('url')) {
            header('HTTP/1.1 301 Moved Permanently');
            $gclid = $this->getRequest()->getParam('');
            $url = $this->getRequest()->getParam('url');
            header('Location: /' . $url . '?voucherbox=' . $gclid);
            die();
        }
        else {
            $this->_redirect("/");
        }
    }
}

I think there is no option to call the observer page function in controller page.


Solution

  • @SIBI A, I am just trying to answer about use of observers.

    Observers are mainly used to add more functionalities/Change some existing behaviours at the time of a particular event happens in magento application flow.

    Prior to concentrating on observer, we should have an understanding on various events which are observable in magento. For example "sales_order_place_after" is an event fired by magento while it places a new order.

    If you search for a string "Mage::dispatchEvent" inside a magento root folder, you can see a lot of statements which fire various kinds of events on different occassions.

    Let's consider the following scenario. Its just an example for understanding the basic concept.

    Scenario: We need to update the "customer_note" field in sales_flat_order table while a customer places a new order.(NB:sales_flat_order is the table which saves basic information about each orders)

    Even though there may exist a lot of conventional approaches to accomplish this functionality, we can do this by means of a new observer also. Let me write some basic steps of the process

    1. Create a module
    2. Create a model class
    3. Add an observer method, which listens to the event "order_place_after"
    4. Define the observer method, access the $order object inside this method
    5. Update the "customer_note" field in $order object and save.

    If the above steps are done and working fine, when we place a new order we can see that "customer_note" column(in sales_flat_order table) have the value updated via our new observer method.

    If you check the function place() inside Mage_Sales_Model_Order class, you can see there magento fires the event "sales_order_place_after" to which we are listening. It is passing the current $order object to the event as parameter. We will get this $order object in our observer method as parameter because we are listening to this particular event.

    I am giving here the files which might be used to create the module to accomplish the above.

    1) app/etc/modules/Research_OrderCustomerNoteModify.xml

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

    2) app/code/local/Research/OrderCustomerNoteModify/etc/config.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <Research_OrderCustomerNoteModify>
                <version>0.1.0</version>
            </Research_OrderCustomerNoteModify>
        </modules>
        <global>
            <models>
                <research_ordercustomernotemodify>
                    <class>Research_OrderCustomerNoteModify_Model</class>
                </research_ordercustomernotemodify>
            </models>
            <events>
                <sales_order_place_after>
                    <observers>
                        <research_ordercustomernotemodify_update_customernote_field>
                            <class>research_ordercustomernotemodify/observer</class>
                            <method>updateCustomernoteField</method>
                        </research_ordercustomernotemodify_update_customernote_field>
                    </observers>
                </sales_order_place_after>
            </events>
        </global>
    </config>
    

    3) app/code/local/Research/OrderCustomerNoteModify/Model/Observer.php

    <?php
    class Research_OrderCustomerNoteModify_Model_Observer
    {
        public function updateCustomernoteField($observer)
        {
            $order = $observer -> getEvent() -> getOrder();
            $currentNote = $order->getCustomerNote();
            $order->setCustomerNote('Customer has quoted this'.$currentNote);
            $order->save();
            return;
        }
    }