سؤال

I'm new to the whole Magento thing... I'm trying to show products on the front page. I registered a custom template and my home cms page uses it to load. In my template file, here is the code.

$category = Mage::getModel('catalog/category')->load(41); 
$_productCollection = Mage::getResourceModel('catalog/product_collection')
    ->addCategoryFilter($category)->addAttributeToSelect('*');
$_productCollection->getSelect()->order('rand()');
foreach($_productCollection as $_product) {

No mather what I do or try, the $_productCollection contains an object and therefore, doesn't work with the foreach, giving me nothing in $_product. Where am I wrong?

Thank You!

هل كانت مفيدة؟

المحلول 2

I finally found the problem. The problem was not in the code but in the catalog itself... I did not set inventory nor the website... I started that catalog with sample date with multiple storefront/websites.

After setting the product to in stock and clicking it to work in the main website, everything came together and the product appeared in the page.

Thanks to everyone who helped me on that one!

نصائح أخرى

This is probably your best option to show products on your homepage: http://www.magentocommerce.com/wiki/5_-_modules_and_development/catalog/how_to_create_a_featured_product

The other option is create a category called something like homepage products. Then load those products in the CMS page for your homepage using this type of code:

 {{block type="catalog/product_list" category_id="1" template="catalog/product/list.phtml"}} 

Change the 1 in category_id="1" to whatever the category ID is (viewable on the catalog>manage categories page)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top