0

I have used Mage::getSingleton to share data across and within Modules before, but I'm coming across a problem where its not working in this specific scenario.

My module asks questions at checkout that ultimately effect the shipping rate of separate shipping module. The options are saved via AJAX into Mage::getSingleton('checkout/session')->setOptions($data); and then loaded from that within the shipping extension module.

In the normal checkout process everything works correctly. However, I have been working to add this into the Admin section for creating orders in the back end. I can set and get into Mage::getSingleton('checkout/session') from within my Options module, but when I try Mage::getSingleton('checkout/session') in the shipping module, it returns the object but without my setOptions array.

I can reload the admin area and do Mage::getSingleton('checkout/session')->getOptions() which will return the correct data, so I know it is being saved correctly in the session. I have also tried using Mage::getSingleton('adminhtml/session_quote') but with no change in results.

Any idea why, when loaded in the adminhtml area, the shipping module would not be (correctly) reading Magento Singletons?

--- edit ---

I am specifically testing this when editing an existing order, but creating an order would essentially be the same.

When the select field in this template(options.phtml) is changed, it is sent via AJAX to options/ajax/addOption (see addOptionAction() below).

Then, when the submit button is hit, it fires the magento function order.itemsUpdate(), which I confess I'm not very familiar with, but it gives the desired result of clearing the shipping quotes. Then the user clicks on the link that fires order.loadShippingRates(), which causes Magento to get all shipping quotes, eventually getting to Freightquote below.

This all seems to behave in the same way that the standard checkout process does, except in the Freightquote module, I cannot access my singleton or registry when creating an order from the admin side.

Added in the adminhtml local.xml:

<block type="adminhtml/sales_order_create_data" template="sales/order/create/data.phtml" name="data">
    <block type="adminhtml/template" name="options" as="options"  template="options/options.phtml" />
</block>

/design/adminhtml/default/[namespace]/template/options/options.phtml

<fieldset id="options">
    <h4>Does the destination have a...</h4>
        <label for="dock">Dock?*
            <select name="dock">
                <option></option>
                <option value="0">No</option>
                <option value="1">Yes</option>
                <option value="2" >Don't know</option>
            </select>
        </label>

// cut for brevity ... there are a number of select fields

        <button type="button" id="updateFQ" class="button" onclick="order.itemsUpdate()"><span><span>Save & Continue</span></span></button>
</fieldset>

/local/[namespace]/Options/controllers/AjaxController.php

public function addOptionAction()
{
    $params = $this->getRequest()->getParams();
    $session = Mage::getSingleton('checkout/session');

foreach($params as $key => $value){
    $data = $session->getOptions();
    $data[$key] = $value;
    $session->setOptions($data);
    Mage::register('options', $data);
}

$result['options'] = $session->getOptions();

$result['success'] = true;
header("Content-type: application/json", true);
    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}

/community/Freightquote/Shipping/Model/Carrier/Shipping.php

public function getDestConfig($type)
{
        $config = array(
            'liftgate' => $this->getConfigData('destination_liftgate'),
            'residential' => $this->getConfigData('destination_residence'),
            'construction' => $this->getConfigData('destination_construction'),
            'dock' => $this->getConfigData('destination_dock'),
        );

        $session = Mage::getSingleton('checkout/session')->getOptions(); //empty when called from admin area

        if(count($session) < 1){
            $session = Mage::registry('options');
            Mage::log($session); //Returns nothing!
        }

           //snipping the code here because the rest doesn't matter
}

-- Edit 2 --

The truly mystifying part of this, is that I've added the results of Mage::getSingleton('checkout/session') to the Ajax response; and it correctly shows the data from setOptions(). Even if I log out of the admin and log back in, the data that I set in that singleton is still retained, according to my Ajax controller, but that seems to be the only place it exists. I can't echo it in a template and I can't access it in any other blocks, models, templates, or controllers. Is my Ajax Controller somehow existing in its own session?

2
  • Is the correct session id definitely provided as part of the AJAX request when it's done through the admin section? Try Mage::loging the id in both places. Commented Oct 16, 2013 at 8:44
  • Added some code, in case there is something else going on
    – andyjv
    Commented Oct 16, 2013 at 15:18

2 Answers 2

1
Even if I log out of the admin and log back in, the data that I set in that singleton is still retained, according to my Ajax controller

I think that your sessions get mixed up. When logging out of the admin the session should not be able to supply data any more. Try with this first Mage::getSingleton('adminhtml/session')

Additionally I would create an admin ajax controller to separate it from your frontend.

1

Use the Mage::register('var_name', $var) method for storing global session data to access accross modules. Using Mage Register

4
  • 2
    If it's done via ajax, neither register nor singleton will help directly as it's going to need to be stored in the session, neither of these design patterns deal with cross request storage. Commented Oct 16, 2013 at 8:36
  • The Mage::register method does not store data in session. It somehow simulates a global variable.
    – Marius
    Commented Oct 16, 2013 at 8:41
  • Mage::register didn't do the trick. I will be editing my question to include the specific code in question, as well as more detail because there must be something else going on.
    – andyjv
    Commented Oct 16, 2013 at 12:13
  • Take a look at my edits and see if there is something else going on, since using the Mage Registry didn't work either
    – andyjv
    Commented Oct 16, 2013 at 15:19

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.