I'm having an issue with session variables saving after a redirect. Here's a mock of the relevant code:
class Company_X_Model_Session extends Mage_Core_Model_Session_Abstract {
const KEY_QUOTE = 'quote';
protected function _construct() {
$this->init('company_x');
}
public function getQuote() {
if (!$this->hasData(self::KEY_QUOTE)) {
$quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore()->getId());
/* @var $quote Mage_Sales_Model_Quote */
if ($this->getQuoteId()) {
$quote->load($this->getQuoteId());
}
$this->setData(self::KEY_QUOTE, $quote);
}
return $this->getData(self::KEY_QUOTE);
}
public function clear() {
$this->_quote = null;
$this->setQuoteId(null);
$this->setLastSuccessQuoteId(null);
return parent::clear();
}
}
In the controller class:
class Company_X_SomeController extends Other_Company_SomeController {
protected function relevantAction() {
$session = Mage::getSingleton('company_x/session');
/* @var $session Company_X_Model_Session */
$quote = $session->getQuote();
/* @var $quote Mage_Sales_Model_Quote */
$payment = $quote->getPayment();
$payment->importData(array('method' => 'paypal_express'));
/// all sorts of interesting quote-setting-up code here
$quote->collectTotals()->save();
/// This variable is not saving properly
$session->setQuoteId($quote->getId());
$this->_redirectUrl($quote->getPayment()->getCheckoutRedirectUrl());
}
}
Controller that the redirect hits:
class Company_X_ExpressController extends Mage_Paypal_ExpressController {
protected function _getQuote() {
if(!$this->_quote) {
$this->_quote = $this->_getCheckoutSession()->getQuote();
}
return $this->_quote;
}
protected function _getCheckoutSession() {
/// quote_id is not printed here.
print_r(Mage::getSingleton('company_x/session')->getData());
return Mage::getSingleton('company_x/session');
}
}
Can anyone shed some light on why the quote_id
variable is not persisting after the redirect?
Edit 1:
In the first controller:
Company_X_Model_Session Object
(
[_skipSessionIdFlag:protected] =>
[_sessionHosts:protected] => Array
(
[company.x.local] => 1
)
[_data:protected] => Array
(
[_session_validator_data] => Array
(
[remote_addr] => 10.0.2.21
[http_via] =>
[http_x_forwarded_for] =>
[http_user_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36
)
[quote_id] => 445
)
[_hasDataChanges:protected] =>
[_origData:protected] =>
[_idFieldName:protected] =>
[_isDeleted:protected] =>
[_oldFieldsMap:protected] => Array
(
)
[_syncFieldsMap:protected] => Array
(
)
)
In the redirect controller:
Company_X_Model_Session Object
(
[_skipSessionIdFlag:protected] =>
[_sessionHosts:protected] => Array
(
[company.x.local] => 1
)
[_data:protected] => Array
(
[_session_validator_data] => Array
(
[remote_addr] => 10.0.2.21
[http_via] =>
[http_x_forwarded_for] =>
[http_user_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36
)
)
[_hasDataChanges:protected] =>
[_origData:protected] =>
[_idFieldName:protected] =>
[_isDeleted:protected] =>
[_oldFieldsMap:protected] => Array
(
)
[_syncFieldsMap:protected] => Array
(
)
)
Resolution:
The Other_Company_SomeController
class had overridden the preDispatch
method and did not properly call through to the parent class.
Mage::getSingleton('company_x/session')
returning aCompany_X_Model_Session
object?setFoo('bar')
and retrieve it later successfully?