Thursday, 5 June 2014

Magento – display root category products with filters

I had to display all the child categories products of the root category on a page with filtered navigation on the left column, but by default Magento does not display the root category products.
After some search and research I find the solution for this. It can be accomplished using Layout Update XML inside the CMS page. Not only layered nav but toolbar will also be displayed. For this we need to create a new CMS page or we can use any existing page where we want to display the products of the root category with filtered nav.
Follow steps:
1. Open Manage Category page in admin and choose the root category you want to display. Make sure that the category is Active and Is Anchor yes. Note down the category id.
2. Create a new CMS page, CMS > Pages > Add New Page or Edit any existing one where you want to display the products.
3. Fill the proper information in the required fields.
4. Under Design tab, paste the given code in the Page Layout > Layout Update XML textarea.
5. In the code find “category_id 3 category_id” and change your category id with 3. For example your category id is “2″ so it should be “category_id 2 category_id“.
6. Select the Layout 1, 2 or 3 columns whatever you want.
7. Note: I used reference name=”left” so layered nav will display only if 2columns with left bar is selected. If you want the right side column than change reference name=”left” to reference name=”right”.


<reference name="content">
     <block type="catalog/product_list"  name="product_list" template="catalog/product/list.phtml">
        <block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
            <block type="page/html_pager" name="product_list_toolbar_pager"/>
         </block>
         <action method="setCategoryId"><category_id>3</category_id></action>
        <action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
     </block>
 </reference>
<reference name="left">
    <block type="catalog/layer_view" name="catalog.leftnav" after="currency" template="catalog/layer/view.phtml"/> </reference> 
 
Save the page. Refresh cache and visit the page using the URL assigned in the CMS page .






Magento get related products by product id

Magento displays related products block in product page default. If u would like to display this block in other page, u need to customize this block.


<?php
  
//load a product resources
$_product = Mage::getModel('catalog/product')->load($productId);
  
//save the product into the registry
Mage::register('product', $_product);
  
//call $_product's related products
echo $this->getLayout()->createBlock('catalog/product_list_related')->setTemplate('catalog/product/list/related.phtml')->toHtml();
  
?>

Wednesday, 4 June 2014

Magento display top level categories and subcategories

<?php
/*
 * http://www.magentobooker.com
 *
 * Display top level categories and subcategories
 *
 * This code is compatible with Magento CE 1.3 and up
 *
**/
?>
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?>
    <ul class="category">
        <?php foreach($_categories as $_category): ?>
            <li>
                <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                    <?php echo $_category->getName() ?>
                </a>
                <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                <?php $_subcategories = $_category->getChildrenCategories() ?>
                <?php if (count($_subcategories) > 0): ?>
                    <ul class="subcategory">
                        <?php foreach($_subcategories as $_subcategory): ?>
                            <li>
                                <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                    <?php echo $_subcategory->getName() ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif;
?>

How to show maintenance mode page in magento (website under construction)?


50311 How to show maintenance mode page in magento (website under construction)
If you want your Magento website to show in maintenance mode, you will have to do two things.
1. Create a file name maintenance.flag in your magento root directory. Contents under this file doesn’t matter, you can keep it empty.
2. Change the maintenance file (located in magento root -> errors -> default directory) to show proper message when user visits your website.

How will you log current collection’s SQL Query?

$collection->printLogQuery(true);
$collection->getSelect()->;__toString();

How will you get First Item and Last Item from collection?

$collection->getFistItem();
$collection->getLastItem();

How to run custom MySQL Query in Magento?

$db = Mage::getSingleton(‘core/resource’)->getConnection(‘core_write’);
$result = $db->query(‘Select * from Tablename’);