Magento – How to get the quote from the admin session

adminadminhtmlmagento-1quote

I have an ajax script where I use Mage::getSingleton('adminhtml/session_quote')->getQuote()->getId() however the result is always null.

Best Answer

This can happen if you load Magento for the frontend instead of the backend.

$ajaxCWD = realpath(dirname(__FILE__));
$_SERVER['SCRIPT_FILENAME'] = realpath($ajaxCWD.'/index.php');
require_once(realpath($ajaxCWD.'/app/Mage.php'));
$vararr = get_defined_vars();
foreach ($vararr as $varName => $varValue)
  $GLOBALS[$varName] = $varValue;
Mage::init();
Mage::getSingleton('core/session', array('name'=>'frontend'));
$_SERVER['SCRIPT_FILENAME'] = $originalScriptFileName;
[...]
error_log(gettype(Mage::getSingleton('adminhtml/session_quote')->getQuote()->getId()));

will always return "NULL"

To fix this you have to load Magento explicitly for the admin session.

[...]
//Mage::getSingleton('core/session', array('name'=>'frontend'));
Mage::getSingleton('core/session', array('name'=>'adminhtml'));
[...]
error_log(Mage::getSingleton('adminhtml/session_quote')->getQuote()->getId());

Now it should work.

Related Topic