1
votes

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!

2
@Zyava I've gone through that already. Method cart.info requires integer quoteId, which is the ID of an existing shopping cart. I could avoid all this if I had the quoteId, but I don't know how to get it for the current user (And especially outside of a Magento session) -- this is really what I'm asking in Point #2. - aendra
Worth adding that I have my cookie path set to "/" in admin->system->configuration->web. - aendra
Have you tried to load the magento part with ajax instead? So that the two frameworks are never running for the same request. - regilero
@regilero -- That's kind of been my fall-back idea, but it's not nearly as optimized as simply loading the Magento app via PHP (It would essentially double the load on Apache) -- plus it requires the user to have JavaScript enabled. Sylvain's solution below is working great, I'll post how I modified his JFusion plugin class for Drupal later on today. - aendra

2 Answers

3
votes

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!

0
votes

Mage_Core_Model_Session_Abstract_Varien::start();