Magento display products list outside magento

Magento display products list outside magento

October 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento display products list outside magento

Embedding Magento Functionality Outside the Framework

Hello! We’ll address your question about embedding Magento functionality into a static page outside the framework. Let’s dive right in.

The Current Challenge

You’re facing an issue with displaying a list of products using the Mage_Catalog_Block_Product_List block and its associated template (catalog/product/list.phtml). Your current setup includes the following:

require_once ('app/Mage.php');
...

$layout = Mage::app('default')->setCurrentStore(Mage::app()->getStore()->getId())
    ->getLayout();
...

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

...
?><?php echo $list_block->toHtml();?><?

However, you’re encountering a fatal 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

Understanding the Problem

The issue arises because you’re trying to use Magento blocks and templates outside of their typical context within the Magento framework. The Mage_Catalog_Block_Product_List block relies on certain Magento core components that are not initialized when you directly instantiate it.

The Solution: Using Magento’s API Properly

To resolve this issue, we need to ensure that the necessary Magento environment is properly set up before trying to use the blocks and templates. Here’s a corrected version of your code:

require_once ('app/Mage.php');
Mage::app();

$_productCollection = Mage::getModel('catalog/product')->getCollection();
$_productCollection->addAttributeToSelect(array('*'));

$categoryid = 3;
$category = Mage::getModel('catalog/category')->load($categoryid);
$_productCollection->addCategoryFilter($category);

$toolbar = $this->getLayout()->createBlock('catalog/product_list_toolbar');
$toolbar->setCollection($_productCollection);
$list_block = $this->getLayout()->createBlock('catalog/product_list');
$list_block->setChild('toolbar', $toolbar);
$list_block->setCollection($_productCollection);

echo $list_block->toHtml();

Key Changes Made

  1. Initialize the Magento application: We used Mage::app() to ensure the entire Magento environment is set up properly.

  2. Use Magento models: Instead of directly loading the product collection, we used the appropriate model (Mage::getModel('catalog/product')).

  3. Apply category filter: To get products from a specific category, we loaded the category model and applied the filter to the collection.

  4. Create blocks using layout object: We used $this->getLayout()->createBlock() to create the toolbar and list blocks. This ensures that they are created within the Magento context.

Best Practices for Embedding Magento Functionality

When embedding Magento functionality outside of its standard flow, always remember:

  • Initialize the entire Magento environment using Mage::app(). This ensures all core components necessary for block creation are available.
  • Use Magento models and methods to interact with data where possible. This helps maintain consistency and prevents potential errors.
  • Create blocks through the layout object, as demonstrated above, to ensure they have access to the necessary dependencies.

Conclusion

In this post, we explored how to properly embed Magento functionality into a static page outside of its standard framework flow. By following the steps outlined here—initializing the entire Magento environment, using Magento models and methods, and creating blocks through the layout object—you can safely integrate Magento components in your custom projects.

Remember, while it’s possible to embed specific Magento features like product lists, you should always consider the potential security risks and performance implications of running Magento outside its intended context. Be sure to follow best practices for protecting sensitive data and optimizing performance.

For more advanced insights on extending Magento functionality, be sure to check out our comprehensive guide at Laravel Company.

Catalog Themes Errors

Our network