I have a problem with custom module and displaying custom block template on homepage.
I create a custom module to show product form specified category.
my config.xml file:
<config>
<modules>
<Vendor_Module>
<version>0.1.0</version>
</Vendor_Module>
</modules>
<global>
<blocks>
<vendor_module>
<class>Vendor_Module_Block</class>
</vendor_module>
</blocks>
<models>
<vendor_module>
<class>Vendor_Module_Model</class>
</vendor_module>
</models>
</global>
<frontend>
<layout>
<updates>
<promotedproducts>
<file>promotedproducts.xml</file>
</promotedproducts>
</updates>
</layout>
</frontend>
</config>
my Model file:
<?php
class Vendor_Module_Model_Products extends Mage_Catalog_Model_Product
{
public function getItemsCollection($valueId)
{
$category = Mage::getModel('catalog/category')->load($valueId);
$productCollection = $category->getProductCollection();
$productCollection
->addStoreFilter()
->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds())
->addMinimalPrice()
->addFinalPrice()
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addUrlRewrite();
return $productCollection;
}
}
my Block file:
<?php
class Vendor_Module_Block_List extends Mage_Catalog_Block_Product_Abstract
{
protected $_itemCollection = null;
public function getItems()
{
$catid = $this->getCatid();
if (!$catid)
return false;
if (is_null($this->_itemCollection)) {
$this->_itemCollection = Mage::getModel('vendor_module/products')->getItemsCollection($catid);
}
return $this->_itemCollection;
}
}
and here is my template file :
<?php $_items = $this->getItems(); var_dump($_items);die; ?>
<div id="promoted-product-list">
<div class="block-content">
<ul class="promoted-products-list">
<?php foreach ($_items as $_item): ?>
<?php endforeach; ?>
</ul>
</div>
</div>
and here is my custom layout file:
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
<reference name="head">
<action method="addCss"><stylesheet>css/promotedproducts.css</stylesheet></action>
</reference>
</default>
<cms_index_index>
<reference name="head">
<action method="addCss"><stylesheet>css/promotedproducts.css</stylesheet></action>
</reference>
<reference name="content">
<block type="vendor_module/list" name="promotedproduct" template="vendor/module/list.phtml"/>
</reference>
</cms_index_index>
</layout>
So, when i use
{{block type="vendor_module/list" name="promotedproduct" template="vendor/module/list.phtml" catid="252"}}
in my cms contact page it shows as i expected. but on cms_index_index
it does not show. the custom css file is included, but the block does not show. It gives me a warning
Warning: Invalid argument supplied for foreach()
, and when I use
$_items = $this->getItems()->getData();
it gives me an error that it is type boolean, but using var_dump($_items)
on cms_contact
shows me object, and var_dump($_items->getData())
shows me array with 4 elements (I have 4 product in this category).
Any help would be appreciated. Thanks in advance.
<?php echo $this->getChildHtml('promotedproducts'); ?>
. But it gives nothing. im new to magento. Greetings!