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.
2 Answers 2
Try below block code
<block type="vendor_module/list" output="toHtml" name="promotedproduct" template="vendor/module/list.phtml"/>
-
Unfortunately it still gives me an error. It seems like i dont get productCollection on homepage. Still warning "Invalid argument supplied for foreach()".SebastianT– SebastianT2019年07月04日 10:14:47 +00:00Commented Jul 4, 2019 at 10:14
-
protected $_itemCollection = array(); should resolve your warning.Gulshan Maurya– Gulshan Maurya2019年07月04日 10:19:15 +00:00Commented Jul 4, 2019 at 10:19
-
O.K. I solved this problem. It was my fault. I didnt see this. When you told me to change $_itemCollection to array, i checked one more time this block code and i saw that there is $catid var, and when i called this block from cms contac page i give attribute catid="252", but when i tried to call this block on homepage i didnt give an attribute value and it return false. Set $catid solved it. Thanks :DSebastianT– SebastianT2019年07月04日 10:34:58 +00:00Commented Jul 4, 2019 at 10:34
O.K. I solved this problem. It was my fault. I didnt see this. When @GulshanMaurya told me to change $_itemCollection to array, i checked one more time this block code and i saw that there is $catid var, and when i called this block from cms contac page i give attribute catid="252", but when i tried to call this block on homepage i didnt give an attribute value and it return false. Set $catid solved it. When use the code from tutorial, read it twice :D
Explore related questions
See similar questions with these tags.
<?php echo $this->getChildHtml('promotedproducts'); ?>. But it gives nothing. im new to magento. Greetings!