Thursday, 5 June 2014

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’);

What permissions required for file and folder In Magento?

A security Issue happen if Setting all the files & folders to 777 will make them writable by everyone.!
Normal Operation
For the normal operation or installation of a Magento store, only 2 folders need to be writable:
/media – for web accessible files, such as product images
/var – for temporary (cache, session) and import/export files
Installation During installation Magento Install Wizard will create app/etc/local.xml file which contains database configuration and global encryption key for your Magento copy. Meaning app/etc folder has to be writable to web service.
Web based MagentoConnect manager If you wish to use web based downloader for installation, upgrades or MagentoConnect extensions installation, you will need to have write permissions on ALL magento files including the root Magento folder. Files and folders will need to be returned to 655 non-writable permissions after installation.