Monday, April 29, 2013

Import dump data to mysql through command promt


>> Mysql import from CSV :

      > mysqlimport --fields-terminated-by=, --fields-enclosed-by="\"" --lines-terminated-by="\r\n" DBNAME FILEPATH -u USERNAME -p --columns=code,city,state,county


Thursday, April 18, 2013

How to get all subcategories of a category in Magento


 >> Suppose you need to get the list of all subcategory of "cat1" (id : 10)

    <?php
       $category = Mage::getModel('catalog/category')->load(10); // load the parent category
    ?>
    There are many way to do it. I'll show you the two :
   
    1. $subcatCollection = Mage::getModel('catalog/category')->getCollection()
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('is_active','1')
                ->addAttributeToFilter('parent_id',array('eq' => 10));
               
       // This will return the subcategory collection.
               
   2. $subcategories = $category->getChildren();
   
       // This will return all subcategory id with comma separator like : 11,12,13


Thursday, April 11, 2013

How to show review block on product page in Magento

>> To get the review block in product details page. You need to follow the two steps:

       1) Add this code to catalog.xml like:
            <catalog_product_view translate="label">
                <reference name="content">
                    <block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">
                       
                            <!--You need to copy below section -->
                        <block type="page/html_pager" name="product_review_list.toolbar"/>
                        <block type="core/template" name="product_review_list.count" template="review/product/view/count.phtml" />
                        <block type="review/product_view_list" name="product.detail.info.product_additional_data" as="product_review_data" template="review/product/view/list.phtml">
                            <block type="review/form" name="product.review.form" as="review_form">
                                <block type="page/html_wrapper" name="product.review.form.fields.before" as="form_fields_before" translate="label">
                                    <label>Review Form Fields Before</label>
                                    <action method="setMayBeInvisible"><value>1</value></action>
                                </block>
                            </block>
                        </block>
                            <!--        END            -->

                  
                   </block>
               </reference>
            </catalog_product_view>
           
       2) Now go to the view.phtml page. here you need to add this code:
      
               <?php echo $this->getChildHtml('product_review_data') ?>
           
            you are done> :)
           
           
>> To change the default number of reviews shown per page, follow this:

        By default it shows 10, 20, 50. Go to app/code/core/Mage/Page/Block/Html/Pager.php
        there you can find the line 
            <?php protected $_availableLimit = array(10=>10,20=>20,50=>50);  ?>
           
        change it as you want        
            <?php protected $_availableLimit = array(5=>5,10=>10,50=>50);  ?>
           
   Note*: You should do this step either by overriding the block OR copy the same file in the local directory like:  app/code/local/Mage/Page/Block/Html/Pager.php





Friday, March 29, 2013

Custom error/success message in Magento


>> A simple and most needed thing that we frequently use in Magento that how to add custom Error and Success message in Controller page.

You just need to call these methods:
$session = Mage::getSingleton('core/session');  
// You can specify the session with yours like:  
// Mage::getSingleton('customer/session'),
// Mage::getSingleton('checkout/session')

$session->addSuccess('your message here');
$session->addError('your message here');


Tuesday, March 26, 2013

Special character of Regular Expression

Regular Expression :

>> Meaning of Special character [](){}|?+-*^$\

    []    => Square brackets can be used to define a character group. [abc] The example finds one of the characters a, b or c.   

    -    => The hyphen can be used to define a range of characters. [a-f] The example finds one of the characters a, b, c, d, e or f. Also in this example, the regular expression only matches one character.

    ^    => With the meta character ^ at the beginning of a character group, the character group is negated. That means, the example will match any character but not an a. [^a]

           ^a If the meta character ^ is not included into a character group, it stands for the beginning of a string or a line. The example would match all lines or strings beginning with a.

    $    => a$ Like the meta character ^ stands for the beginning of a string or a line, the character $ stands for its end. The example would match all strings or lines ending with an a.
   
            ^abc$ Here the meta characters ^ and $ are used together. This example would match all strings or lines which are equal to "abc".
            Example 1: abc
            Example 2: abc abc

Friday, March 22, 2013

Prevent visitors to view the store without login in Magento


>> Magento use methods :  _redirect() AND _redirectUrl()
for redirect in Mage_Core_Controller_Varien_Action controller. If you see these methods, they are using this code to redirect $this->getResponse()->setRedirect('your_url');

Now you just need to call like :
                        Mage::app()->getFrontController()
                               ->getResponse()
                               ->setRedirect(Mage::getBaseUrl('your_url');


I used this code to prevent visitors to view my store without login.
With the following code in head.phtml :

<?php
    $CusSession=mage::getSingleton('customer/session');
    $frontController = Mage::app()->getFrontController();
  
    if(!$CusSession->isLoggedIn() && $frontController->getRequest()->getActionName() != 'login')
    {
       $frontController->getResponse()->setRedirect(Mage::getUrl("customer/account/login"));
    }
?>




Wednesday, February 20, 2013

How to add image in magento admin product Grid


>> With the help of this code you can able to add product image in admin Grid View :

    Step 1:
    First you need to copy the core block to local. Copy Grid.php from app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php to
    app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php
    Here you'll find a method named _prepareColumns(), add the below code within this method
  
        $this->addColumn('product_image', array(
              'header'    => Mage::helper('catalog')->__('Image'),
              'align'     =>'left',
              'index'     => 'entity_id',
              'width'     => '100px',
              'renderer'  => 'Mage_Adminhtml_Block_Catalog_Product_Renderer_Image'
        ));
  

    Step 2:
    Now create the renderer file named Image.php in the following path
    app/code/local/Mage/Adminhtml/Block/Catalog/Product/Renderer/Image.php
    Here add the below code..
  
    class Mage_Adminhtml_Block_Catalog_Product_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
    {
        public function render(Varien_Object $row)
        {
            $_product = Mage::getModel('catalog/product')->load($row->getEntityId());
            if($_product->getImage() != 'no_selection'){
                  $image = "<img src='".Mage::helper('catalog/image')->init($_product, 'image')->resize(100)."' title='".$_product->getName()."' />";
            }
            return $image;
        }
    }