Hello i'm trying to embed some magento functions in a static page outside the framework. Everything is working except when i try to use the magento standard output for list of products.

As i said before i want to use the Mage_Catalog_Block_Product_List with the template in catalog/product/list.phtml.

The code is:

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>

I have this error:

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

Can you help me? Thanks.

有帮助吗?

解决方案

The error appears because you are creating an instance of the Mage_Catalog_Block_Product_List class using new. You should use the createBlock method because some other stuff happens in there. Also I don't think you need to create an instance of Mage_Catalog_Block_Product_List_Toolbar block. This should happen automatically (on the same line where you get the error).

While you are at it, don't create an instance of the category model like that either. Use Mage::getModel().
And I don't think that calling setCollection on the list block will work. Try setting the category id. So your code shold become:

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.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top