Show a CATEGORY and its products on Homepage, Magento1.9

别说谁变了你拦得住时间么 提交于 2019-12-02 15:39:18

问题


I want to show a Category with its Products on homepage. Magento has built in option to show New Products on homepage and I have no idea about how to show different categories on homepage. For example I have created a category and I want to show the products in this category on homepage as below:

Featured Products

Product1 Product2 Product3

I have tried below code (from previous posts)

{{block type="catalog/product_new" name="home.catalog.product.list" alias="products_homepage" template="catalog/product/list.phtml"}}

But this gives me below error

Fatal error: Call to a member function getSortedChildren() on a non-object in C:\wamp\www\magento1901\app\design\frontend\rwd\default\template\catalog\product\list.phtml on line 134

Apparently code I mentioned above is for previous versions or Magento. However version 1.9.0.1 gives error.

Please guide how to show categories on homepage. Thanks


回答1:


for new product list in magento 1.9 use this one

   {{block type="catalog/product_new" name="home.catalog.product.new" alias="product_homepage" template="catalog/product/new.phtml"}}

or category wise listing

The new RWD design has two child blocks for the product list.more info

<block type="core/text_list" name="product_list.name.after" as="name.after" /> 
<block type="core/text_list" name="product_list.after" as="after" />

You can first call your block in CMS Homepage like below:

{{block type="catalog/product_list" name="home.catalog.product.list" alias="products_homepage" category_id="4" template="catalog/product/list.phtml"}}

Now in your catalog/product/list.phtml find line no 74: and 133

find this code

<?php
    $_nameAfterChildren = $this->getChild('name.after')->getSortedChildren();
    foreach($_nameAfterChildren as $_nameAfterChildName):
        $_nameAfterChild = $this->getChild('name.after')->getChild($_nameAfterChildName);
        $_nameAfterChild->setProduct($_product);
?>
    <?php echo $_nameAfterChild->toHtml(); ?>
<?php endforeach; ?>

replace with

<?php
if($this->getChild('name.after')):
    $_nameAfterChildren = $this->getChild('name.after')->getSortedChildren();
    foreach($_nameAfterChildren as $_nameAfterChildName):
        $_nameAfterChild = $this->getChild('name.after')->getChild($_nameAfterChildName);
        $_nameAfterChild->setProduct($_product);
?>
    <?php echo $_nameAfterChild->toHtml(); ?>
    <?php endforeach; 
endif;
?>

go line no 188

add code into

if($this->getChild('after')):

//code

endif;



回答2:


I have a below error: Fatal error: Call to a member function getSortedChildren() on a non-object in C:\wamp\www\magento1901\app\design\frontend\rwd\default\template\catalog\product‌​\list.phtml on line 183

find this code

<?php 
 //set product collection on after blocks
 $_afterChildren = $this->getChild('after')->getSortedChildren();
foreach($_afterChildren as $_afterChildName):
$_afterChild = $this->getChild('after')->getChild($_afterChildName);
$_afterChild->setProductCollection($_productCollection);
?>
<?php echo $_afterChild->toHtml(); ?>
<?php endforeach; ?>

replace code with

<?php
if($this->getChild('after')):
//set product collection on after blocks
$_afterChildren = $this->getChild('after')->getSortedChildren();
foreach($_afterChildren as $_afterChildName):
$_afterChild = $this->getChild('after')->getChild($_afterChildName);
$_afterChild->setProductCollection($_productCollection);
?>
<?php echo $_afterChild->toHtml(); ?>
<?php endforeach;
endif;
?>


来源:https://stackoverflow.com/questions/24049034/show-a-category-and-its-products-on-homepage-magento1-9

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!