4

I need to develop a landing page with products from different categories. Something like:

Banner

Products from category 1

Banner

Products from category 2

I'm trying to do it like this:

Banner

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

Banner

{{block type="catalog/product_list" name="cat2" id="cat2" category_id="2" template="catalog/product/list.phtml"}}

But it is not working. It works when I test with only category, but the moment I add the second block it doesn't display anything.

Is there any limit using the same block more than once in the same CMS Page?

I have also tried creating a Static CMS block with each block and then add the widgets to the CMS Page but the result is the same, well, in this case it is displaying the first block but not the second one.

I have also tried changing the layout but result is the same, with one block is ok, it is work but with 2 blocks it is not displaying anything.

Any ideas?

I'm in Magento EE1.13.0.2.

Update:

If I try with the layout like this:

<reference name="content">
    <block type="catalog/product_list" name="featured" template="catalog/product/list.phtml">
        <action method="setCategoryId"><category_id>1</category_id></action>
    </block> <block type="catalog/product_list" name="featured" template="catalog/product/list.phtml">
        <action method="setCategoryId"><category_id>2</category_id></action>
    </block> </reference>

Doing it with layers display the 2 blocks but repeated! So both blocks are displaying the category 2. It seems like a product list block can be used only once per page?

Update 2:

ok, so to prevent this, you need to give a diferent name to each block:

<reference name="content">
        <block type="catalog/product_list" name="featured_1" template="catalog/product/list.phtml">
            <action method="setCategoryId"><category_id>1</category_id></action>
        </block> <block type="catalog/product_list" name="featured_2" template="catalog/product/list.phtml">
            <action method="setCategoryId"><category_id>2</category_id></action>
        </block> </reference>

but now the problem is that I get a MySQL error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

Trace:
#0 /public_html/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#1 /public_html/app/code/core/Zend/Db/Statement.php(291): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#2 /public_html/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#3 /public_html/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('SELECT `mage_ca...', Array)
#4 /public_html/lib/Varien/Db/Adapter/Pdo/Mysql.php(428): Zend_Db_Adapter_Pdo_Abstract->query('SELECT `mage_ca...', Array)
#5 /public_html/lib/Zend/Db/Adapter/Abstract.php(753): Varien_Db_Adapter_Pdo_Mysql->query(Object(Varien_Db_Select), Array)
#6 /public_html/app/code/core/Mage/Core/Model/Resource/Db/Abstract.php(380): Zend_Db_Adapter_Abstract->fetchRow(Object(Varien_Db_Select))
#7 /public_html/app/code/core/Mage/Core/Model/Abstract.php(225): Mage_Core_Model_Resource_Db_Abstract->load(Object(Mage_Catalog_Model_Category), Object(Mage_Catalog_Model_Category), NULL)
#8 /public_html/app/code/core/Mage/Catalog/Block/Product/List.php(80): Mage_Core_Model_Abstract->load(Object(Mage_Catalog_Model_Category))
#9 /public_html/app/code/core/Mage/Catalog/Block/Product/List.php(142): Mage_Catalog_Block_Product_List->_getProductCollection()
#10 /public_html/app/code/core/Mage/Core/Block/Abstract.php(885): Mage_Catalog_Block_Product_List->_beforeToHtml()
#11 /public_html/app/code/core/Mage/Core/Block/Text/List.php(43): Mage_Core_Block_Abstract->toHtml()
#12 /public_html/app/code/core/Mage/Core/Block/Abstract.php(886): Mage_Core_Block_Text_List->_toHtml()
#13 /public_html/app/code/core/Mage/Core/Block/Abstract.php(605): Mage_Core_Block_Abstract->toHtml()
#14 /public_html/app/code/core/Mage/Core/Block/Abstract.php(549): Mage_Core_Block_Abstract->_getChildHtml('content', true)
#15/public_html/app/design/frontend/novaworks/bearstore/template/page/1column.phtml(60): Mage_Core_Block_Abstract->getChildHtml('content')
#16 /public_html/app/code/core/Mage/Core/Block/Template.php(241): include('/...')
#17 /public_html/app/code/core/Mage/Core/Block/Template.php(272): Mage_Core_Block_Template->fetchView('frontend/novawo...')
#18 /public_html/app/code/core/Mage/Core/Block/Template.php(286): Mage_Core_Block_Template->renderView()
#19 /public_html/app/code/core/Mage/Core/Block/Abstract.php(886): Mage_Core_Block_Template->_toHtml()
#20 /public_html/app/code/core/Mage/Core/Model/Layout.php(555): Mage_Core_Block_Abstract->toHtml()
#21 /public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(390): Mage_Core_Model_Layout->getOutput()
#22 /public_html/app/code/core/Mage/Cms/Helper/Page.php(137): Mage_Core_Controller_Varien_Action->renderLayout()
#23 /public_html/app/code/core/Mage/Cms/Helper/Page.php(52): Mage_Cms_Helper_Page->_renderPage(Object(Mage_Cms_PageController), '122')
#24 /public_html/app/code/core/Mage/Cms/controllers/PageController.php(45): Mage_Cms_Helper_Page->renderPage(Object(Mage_Cms_PageController), '122')
#25 /public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Mage_Cms_PageController->viewAction()
#26 /public_html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('view')
#27 /public_html/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#28 /public_html/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#29 /public_html/app/Mage.php(683): Mage_Core_Model_App->run(Array)
#30 /public_html/index.php(166): Mage::run('', 'store')
#31 {main}

Thanks!

  • Did you check if both category_id 1 & 2 are enabled and both have product assigned to it? – Rahul Kumar Das Feb 10 '15 at 6:10
  • Yes, I tested and if I leave only one of the blocks they work alone, the problem is when I put both blocks in the same CMS page. – Maria Feb 10 '15 at 9:00
3

Maria,

Your method is almost correct, you simply need give each block a unique name.

<reference name="content">
    <block type="catalog/product_list" name="featured_1" template="catalog/product/list.phtml">
        <action method="setCategoryId"><category_id>1</category_id></action>
        <block type="core/text_list" name="product_list.after" as="after" />
     </block>
     <block type="catalog/product_list" name="featured_2" template="catalog/product/list.phtml">
        <action method="setCategoryId"><category_id>2</category_id></action>
        <block type="core/text_list" name="product_list.after" as="after" />
     </block>
</reference>

Update The method above works in Magento 1.9.1.0, using the rwd/default package/theme. However does not address the OP's requirement to also display the category banner. To do this, I have revised the answer to the following:

Create this file: app/code/frontend/PACKAGE_NAME/THEME_NAME/template/catalog/category/view/multiple.phtml

(replacing package name and theme name with what applies to your site)

In it, have the following contents:

<?php
/**
 * Displays multiple category view templates along with the product list for it
 */
/* @var $this Mage_Core_Block_Template */

$category_ids     = explode(',', $this->getData('category_ids'));
$current_category = Mage::registry('current_category');

if ($current_category) {
    Mage::unregister('current_category');
}

foreach ($category_ids as $id) {

    $category = Mage::getModel('catalog/category')->load(trim($id));

    if (!$category->getId()) {
        continue;
    }

    Mage::register('current_category', $category);

    $product_list = $this->getLayout()->createBlock('catalog/product_list', 'product_list_'.$id, array('template' => 'catalog/product/list.phtml'));

    echo $this
        ->getLayout()
        ->createBlock('catalog/category_view', 'category_'.$id, array('template' => 'catalog/category/view.phtml'))
        ->append($product_list, 'product_list')
        ->toHtml();

    Mage::unregister('current_category');
}

if ($current_category) {
    Mage::register('current_category', $current_category);
}

Within the "Design" tab of your CMS page, add the following XML instructions:

<reference name="content">
    <block type="core/template" name="multiple_categories" template="catalog/category/view/multiple.phtml">
        <action method="setData"><key>category_ids</key><val>1,2</val></action>
     </block>
</reference>

Make sure to update the comma separated list of category ids with the IDs you actually want to display.

  • nice thanks! But now I'm getting an error: – Maria Feb 10 '15 at 17:26
  • What is the error? – Darren Felton Feb 10 '15 at 17:32
  • I have updated the post so you can see it. Thanks for your help, really appreciate it! – Maria Feb 10 '15 at 17:37
  • 1
    @Maria I'm not certain how to address the SQL error you're having. I was able to perform the same thing on my current development project without issue. I did realize however, that this approach skips the adding of category banner. I'll see if I can update my question in a way that works for you. – Darren Felton Feb 10 '15 at 17:55
  • 1
    @Maria I have revised my answer to better fulfill your original requirements (include the banner for the category). If you're still getting an SQL error with these changes, I'd contact envatoMarket, and ask them for support. It appears thar you're using their BearStore theme. – Darren Felton Feb 10 '15 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.