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?
Mage::log
ing the id in both places.