Search code examples
phpsessionmagentodrupal

Magento and Drupal sessions conflicts -- how to resolve?


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?


Solution

  • I worked on an integration between Joomla and MAgento I get the same problem. The solution I provide is maybe not the best solution but it was the only one I found to share sessions between a single PHP script process.

    I had to "stop" the Joomla session, do my stuff with Magento and start again the session in Joomla all of it during the same script process. Here is a sample of what I did for a Joomla plugin, you can get an inspiration of that because I'm not aware of Drupal Framework but here you will find the code I did for the Joomla plugin: http://pastie.org/5505841#4

    The most interesting part in the code provided are the methods destroyTemporaryJoomlaSession, loadAndStartMagentoBootstrap, restartJoomlaSession, startMagentoSession and stopMagentoSession.

    Then I use this plugin in some Joomla module in this way:

    $plgMageLib = new plgSystemMagelib ( );
    $plgMageLib->destroyTemporaryJoomlaSession ();
    if ($plgMageLib->loadAndStartMagentoBootstrap ()) :
        $plgMageLib->startMagentoSession ();
    
        /* Content of Magento logic, blocks or else */
    
        $html = '';
        $blockId = $params->get ( 'block_id', '' );
        echo JFusion_Helper_Mageselectblock::callblock ( $blockId );
    
        /* EOF */
    
        $plgMageLib->stopMagentoSession ();
    
    endif;
    $plgMageLib->restartJoomlaSession ();
    

    Hope it helps!