Magento和Drupal会话冲突 - 如何解决?

时间:2012-11-27 16:19:05

标签: api magento session drupal

我正在尝试编写一个在Drupal块中加载Magento购物车的块。

以下代码(位于/test.php中)正确加载购物车及其内容(Magento安装在/ 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;
?>

(我正在使用FirePHP来调试会话值 - 这就是fb();调用的用途。)

如果我在Drupal中使用完全相同的代码(通过hook_menu回调),我会收到以下错误:

  

致命错误:Mage_Core_Model_Session_Abstract :: getMessages():   脚本试图执行方法或访问不完整的属性   宾语。请确保类定义   您所在对象的“Mage_Core_Model_Message_Collection”   尝试操作是在 unserialize()被调用之前加载   提供__autoload()函数来加载类定义   /home/aendrew/workspace/drupgento/magento/app/code/core/Mage/Core/Model/Session/Abstract.php   在第215行

我的猜测是Drupal正在进行与Magento冲突的某种会话处理 - 如果我在脚本开头取消设置$ _SESSION,它会显示一个空的购物车(无论是否有实际的商品) 。我也尝试将现有会话放在一个临时变量中,然后在最后执行array_merge(),但这也不起作用。

知道我怎么能这样做吗?谢谢!

2 个答案:

答案 0 :(得分:3)

我参与了Joomla和MAgento之间的整合,我遇到了同样的问题。 我提供的解决方案可能不是最好的解决方案,但它是我发现在单个PHP脚本进程之间共享会话的唯一解决方案。

我不得不“停止”Joomla会话,用Magento做我的东西,并在同一个脚本过程中重新开始Joomla中的所有会话。以下是我为Joomla插件所做的一个示例,你可以从中获得灵感,因为我不知道Drupal Framework,但在这里你会找到我为Joomla插件做的代码:http://pastie.org/5505841#4

提供的代码中最有趣的部分是方法destroyTemporaryJoomlaSessionloadAndStartMagentoBootstraprestartJoomlaSessionstartMagentoSessionstopMagentoSession

然后我以这种方式在一些Joomla模块中使用这个插件:

$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 ();

希望它有所帮助!

答案 1 :(得分:0)

Mage_Core_Model_Session_Abstract_Varien ::开始();