1

I set a session variable through ajax request like in checkout page

Mage::getSingleton('customer/session')->setMyValue($value_from_post_data);

The above is set in a controller of a One Page Checkout module.

I have an event observer which observes checkout_onepage_controller_success_action here, when I access the session, I don't find my_value the session variable I created.

Note: The session ids are same. No change in them. After the session value is set, I printed the whole session data to confirm, the setMyValue created my_value in the session.

So the ajax request has done its job. It has set the variable But the observer picks up old session data. Why is this happening, give me a work around

  • On which controller you have set session varibale – Amit Bera Jan 8 '15 at 9:24
  • use Mage::getSingleton('core/session') instead of Mage::getSingleton('customer/session') – Amit Bera Jan 8 '15 at 9:24
  • @AmitBera : I ve tried all the three Core , customer and checkout. I ve set it in a custom controller which handles AJAX request. The session was set through AJAX – Mohammed Ashiq Jan 8 '15 at 9:26
  • then create quote variable and order variable .On ajax request set the value at that quote variable it automatically set to order new field – Amit Bera Jan 8 '15 at 9:34
  • @AmitBera : I am kind of confused about that. can you elaborate a little bit more. I am new to magento – Mohammed Ashiq Jan 8 '15 at 9:39
1

As you have said that your form is exit in Checkout step then we can set field value at Quote Object and then you can get this quote variable value from order at success page which fill you requirement.

Step1: Create new field at Sale Quote table and Order table using installer

$installer->startSetup();

$installer->addAttribute("quote", "customfield", array("type"=>"text"));
$installer->addAttribute("order", "customfield", array("type"=>"text"));
$installer->endSetup();

Step2: On your Custom controller save this field to new quote field

    if(Mage::getSingleton('checkout/session')->getQuote()->getId()){
Mage::getSingleton('checkout/session')->getQuote()->setCustomfield($yourFieldValue)->save();
    }   

Step3: We have used copy field logic for copy Quote field value to Order field value.

<global>
        <fieldsets>
            <sales_convert_quote>                          
                <customfield><to_order>*</to_order></customfield>
            </sales_convert_quote>

            <sales_convert_order>                                             
                <customfield><to_quote>*</to_quote></customfield>
            </sales_convert_order>
        </fieldsets>
    </global>

Please Study:about this

Step4: Then you can easy get this field value from Order object

$OrderObejct->getCustomfield();

Full Module:

Step1: Create Module at app/code/local/Stackexchange/Magento51524/etc/config.xml

code is

<?xml version="1.0"?>
<config>
  <modules>
    <Stackexchange_Magento51524>
      <version>0.1.0</version>
    </Stackexchange_Magento51524>
  </modules>
  <global>
    <helpers>
      <magento51524>
        <class>Stackexchange_Magento51524_Helper</class>
      </magento51524>
    </helpers>
    <models>
      <magento51524>
        <class>Stackexchange_Magento51524_Model</class>
        <resourceModel>magento51524_mysql4</resourceModel>
      </magento51524>
    </models>
    <resources>
      <salesattribute1420712691_setup>
        <setup>
          <module>Stackexchange_Magento51524</module>
          <class>Mage_Sales_Model_Mysql4_Setup</class>
        </setup>
        <connection>
          <use>core_setup</use>
        </connection>
      </salesattribute1420712691_setup>
      <salesattribute1420712691_write>
        <connection>
          <use>core_write</use>
        </connection>
      </salesattribute1420712691_write>
      <salesattribute1420712691_read>
        <connection>
          <use>core_read</use>
        </connection>
      </salesattribute1420712691_read>
    </resources>
    <events>
      <checkout_submit_all_afte> <!-- identifier of the event we want to catch -->
        <observers>
          <checkout_submit_all_afte_handler> <!-- identifier of the event handler -->
            <type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
            <class>magento51524/observer</class> <!-- observers class alias -->
            <method>ownfieldValueset</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </checkout_submit_all_afte_handler>
        </observers>
      </checkout_submit_all_afte>
    </events>
  </global>
    <global>
        <fieldsets>
            <sales_convert_quote>                          
                <customfield><to_order>*</to_order></customfield>
            </sales_convert_quote>
            <sales_convert_order>                                             
                <customfield><to_quote>*</to_quote></customfield>
            </sales_convert_order>
        </fieldsets>
    </global>
</config> 

Step2: Create Installer file at app/code/local/Stackexchange/Magento51524/sql/salesattribute1420712691_setup/mysql4-install-0.1.0.php

And code is:

<?php
$installer = $this;
$installer->startSetup();

$installer->addAttribute("quote", "customfield", array("type"=>"text"));
$installer->addAttribute("order", "customfield", array("type"=>"text"));
$installer->endSetup();

Step3: Create Helper class at app/code/local/Stackexchange/Magento51524/Helper/Data.php

Code is

<?php
class Stackexchange_Magento51524_Helper_Data extends Mage_Core_Helper_Abstract
{
}

Step4: Observe file : at app/code/local/Stackexchange/Magento51524/Model/Observer.php

A nd code

<?php
class Stackexchange_Magento51524_Model_Observer
{

    public function ownfieldValueset(Varien_Event_Observer $observer)
    {
        //Mage::dispatchEvent('admin_session_user_login_success', array('user'=>$user));
        //$user = $observer->getEvent()->getUser();
        //$user->doSomething();
    }

}

Step5: Create Stackexchange_Magento51524.xml which used for Module config at app\etc\modules\

Code is

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

Step:On your submit controller add this code for set the field to new quote fields

  • That is definitely an awesome work around. I am going for this way. Thanks. But why is the session not working / updating. Is that a bug in magento or is there a way where I can actually ask magento to refresh the session something like for example refreshSessionObject() after an update so that I dont have to wait till the page reloads. – Mohammed Ashiq Jan 8 '15 at 10:31
  • 1
    On checkout order place session variable has been unset thus it may be create issue. – Amit Bera Jan 8 '15 at 10:35

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.