0

I am using Magento 1.9.

When I want to destroy a specific session in Magento I run this code:

Mage::getSingleton('core/session')->unsPcPayment();

But I want to execute this code when an order is completed.

How can I do this? Where should I put this code?

NOTE: I do not want to destroy this session when the order is paid, I just want to destroy the session when the order is simply received.

2 Answers 2

1

You can try to create an observer in a module, preferrable your own module in the local code pool:

<!-- app/code/local/Mymodule/etc/config.xml -->
...
<global>
    <events>
        ...
        <sales_order_place_after>
            <observers>
                <clear_session>
                    <class>Mymodule_Model_Observer</class>
                    <method>clearCheckoutSession</method>
                </clear_session>
            </observers>
        </sales_order_place_after>
    </events>
</global>
...

Don't know how this works? See here:

http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method

Once you declare the observer, build it out:

<?php

// app/code/local/Mymodule/Model/Observer.php

class Mymodule_Model_Observer 
    extends Varien_Event_Observer
{

    public function clearCheckoutSession()
    {
        Mage::getSingleton('core/session')->clear(); // Or whatever you want to clear
    }

}

You'll want to customize this to your needs, obviously. And put some verification checks in there as well.

EDIT: As per laketuna's suggestion, I changed the target event to sales_order_place_after, as it seems to be the most reliable one to observe.

6
  • I have my own module like /app/code/core/local/Vivas/PercentPayment so how it will look like in your answer ? Also, does <method>clearCheckoutSession</method> means that the xml is "telling" to run the public function clearCheckoutSession() ? Thanks for your answer dude! Commented Nov 25, 2014 at 21:26
  • yep, you got it. Read the article referenced for the full details on that part. In your module, translate all the references from Mymodule to PercentPayment, and place the config XML in app/code/core/local/Vivas/PercentPayment/etc/config.xml as part of the global node. Commented Nov 25, 2014 at 21:45
  • 1
    I strongly suggest hooking onto the sales_order_place_after event instead of sales_model_service_quote_submit_after to ensure the order is completed successfully.
    – laketuna
    Commented Nov 26, 2014 at 23:15
  • Good call, @laketuna. I'm updating my answer to reflect that. Commented Nov 27, 2014 at 3:22
  • It seems it's not working for me. I've placed Mage::getSingleton('core/session')->unsPcPayment(); instead of Mage::getSingleton('core/session')->clear(); but it's not destroying this session. Commented Dec 2, 2014 at 12:07
0

you can try to put an observer in checkout_onepage_controller_success_action (or some event after the order is placed).

<?xml version="1.0"?>
<config>
    <modules>
        <GP_Savesku>
            <version>0.0.1</version>
        </GP_Savesku>
    </modules>
    <global>
        <models>
            <gp_savesku>
                <class>GP_Savesku_Model</class>
            </gp_savesku>
        </models>
        <events>
            <checkout_onepage_controller_success_action>
                <observers>
                    <gp_savesku>
                        <type>singleton</type>
                        <class>gp_savesku/observer</class>
                        <method>saveSku</method>
                    </gp_savesku>
                </observers>
            </checkout_onepage_controller_success_action>
        </events>
    </global>
</config>

Observer.php

class GP_Savesku_Model_Observer {
 public function saveSku($observer){
    if(Mage::getSingleton('customer/session')->getDummy()){
      Mage::getSingleton('customer/session')->unsDummy();
    }
 }
}

Is working on a production enviroment right now :)

1
  • You should probably not just copy and paste your code - if you're going to go to the trouble of creating the module you should change it's name to at least hide your copy and pasting :) Besides that, you're missed the app/etc/modules entry, and it'd be handy to specify the file paths for each file incase the OP doesn't know where the Observer model should go
    – scrowler
    Commented May 30, 2016 at 2:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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