你好,我正在尝试在框架外的静态页面中嵌入一些magento函数。除了当我尝试使用产品列表的magento标准输出时,一切都正常。
正如我之前所说的,我想将Mage_Catalog_Block_Product_List与Catalog/Product/List.phtml.中的模板一起使用
代码为:
require_once ('app/Mage.php');
umask(0);
Mage::app('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));
$session = Mage::getSingleton('customer/session', array('name'=>'frontend'));
$layout = Mage::app('default')->setCurrentStore( Mage::app()->getStore()->getId() )
->getLayout();
$layout->getUpdate()
->addHandle('default')
->load();
$layout->generateXml()->generateBlocks();
$head=$layout->getBlock('head');
echo $head->toHtml();
<!-- NOW THE LIST BLOCK -->
$categoryid = 3;
$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$_productCollection = $category->getProductCollection();
$_productCollection->addAttributeToSelect('*');
$toolbar=new Mage_Catalog_Block_Product_List_Toolbar();
$toolbar->setCollection($_productCollection);
$list_block=new Mage_Catalog_Block_Product_List();
$list_block->setChild('toolbar', $toolbar);
$list_block->setCollection($_productCollection);
?>
<pre><?php echo $list_block->toHtml();?></pre>
我有这个错误:
Fatal error: Call to a member function createBlock() on a non-object in /webprod/sites/private/html/app/code/core/Mage/Catalog/Block/Product/List.php on line 183
你能帮我吗?谢谢
出现此错误是因为您正在使用new
创建Mage_Catalog_Block_Product_List
类的实例。您应该使用createBlock
方法,因为其中会发生其他事情。此外,我认为您不需要创建Mage_Catalog_Block_Product_List_Toolbar
块的实例。这应该自动发生(在您得到错误的同一行)。
当你在做这件事的时候,也不要像那样创建类别模型的实例。使用Mage::getModel()
我认为在列表块上调用setCollection
是行不通的。尝试设置类别id。所以你的代码将变成:
require_once ('app/Mage.php');
umask(0);
Mage::app('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));
$session = Mage::getSingleton('customer/session', array('name'=>'frontend'));
$layout = Mage::app('default')->setCurrentStore( Mage::app()->getStore()->getId() )
->getLayout();
$layout->getUpdate()
->addHandle('default')
->load();
$layout->generateXml()->generateBlocks();
$head=$layout->getBlock('head');
echo $head->toHtml();
<!-- NOW THE LIST BLOCK -->
$categoryid = 3;
$category = Mage::getModel('catalog/category');
$category->load($categoryid);
$list_block= Mage::app()->getLayout()->createBlock('catalog/product_list');
$list_block->setCategoryId($categoryid)->setTemplate('catalog/product/list.phtml');
?>
<pre><?php echo $list_block->toHtml();?></pre>
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));
$cat_id = 56;
$category = Mage::getModel('catalog/category')->load($cat_id);
$collection = $category->getProductCollection()->addAttributeToSort('position');
//$catcount = $collection->count();
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
foreach ($collection as $product) {
echo $product->getName();
echo $product->getPrice();
echo Mage::helper('catalog/image')->init($product, 'thumbnail')->resize(260);
echo $product->getProductUrl();
?>
Please use that code working fine.