我正在尝试编写一个在 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() ,但这也不起作用。
知道我该怎么做吗?谢谢!
我必须“停止”Joomla 会话,使用 Magento 完成我的工作,然后在同一脚本过程中再次启动 Joomla 中的会话。这是我为 Joomla 插件所做的示例,您可以从中获得灵感,因为我不了解 Drupal Framework,但在这里您可以找到我为 Joomla 插件所做的代码:
http://pastie。组织/5505841#4destroyTemporaryJoomlaSession
、
loadAndStartMagentoBootstrap
、restartJoomlaSession
、startMagentoSession
和 stopMagentoSession
。然后我在一些 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 ();
希望有帮助!