I'm trying to write a block that loads the Magento shopping cart inside of a Drupal block.
The following code (located in /test.php) loads the shopping cart and its contents properly (Magento install located in /magento):
<?php
/*
* Initialize magento.
*/
require_once('magento/app/Mage.php');
umask(0);
Mage::app('default');
Mage::getSingleton('core/session', array('name'=>'frontend'));
Mage::getSingleton('customer/session');
/*
* Add specific layout handles to our layout and then load them.
*/
$layout = Mage::app()->getLayout();
$layout->getUpdate()
->addHandle('default')
->load();
/*
* Generate blocks, but XML from previously loaded layout handles must be
* loaded first.
*/
$layout->generateXml()
->generateBlocks();
/*
* Now we can simply get any block in the usual way.
*/
$cart = $layout->getBlock('cart_sidebar')->toHtml();
echo $cart;
?>
(I'm using FirePHP to debug session values -- that's what the fb(); calls are for.)
If I use that exact same code within Drupal (via a hook_menu callback), I get the following error:
Fatal error: Mage_Core_Model_Session_Abstract::getMessages(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "Mage_Core_Model_Message_Collection" of the object you are trying to operate on was loaded before unserialize() gets called or provide a __autoload() function to load the class definition in /home/aendrew/workspace/drupgento/magento/app/code/core/Mage/Core/Model/Session/Abstract.php on line 215
My guess is that Drupal's doing some sort of session handling that's conflicting with Magento's -- if I unset $_SESSION at the start of the script, it displays an empty cart (regardless of whether or not there are actually items in it). I've also tried putting the existing session in a temporary variable and then doing an array_merge() at the end, but that doesn't work either.
Any idea how I can do this? Thanks!