您好我正在尝试将一些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
你能帮帮我吗?
感谢。
答案 0 :(得分:0)
出现错误是因为您正在使用Mage_Catalog_Block_Product_List
创建new
类的实例。您应该使用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>
答案 1 :(得分:0)
<?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.