Thursday, June 27, 2013

how to redirect referer page after login in magento


>> Only admin setting (step 1) needed to redirect customer after login. Using this I'm showing how will you restrict a page only for registered customer. If someone click the url www.example.com/restricted_page.html, it'll redirect to login page. Then after login it should redirect back to the www.example.com/restricted_page.html.
Here's how I did it :

    Step 1: Go to Admin => System => Configuration => Customer Configuration => Login Options => "Redirect Customer to Account Dashboard after Logging in" set it "No".
 
    Step 2: If you're using a module page then you've the controller Action method like:
  
            public function indexAction()
            {
                // use here to restrict the page //
                if(!Mage::helper('customer')->isLoggedIn()){
                    Mage::getSingleton('customer/session')->setBeforeAuthUrl('Restricted Page Url');
                       $this->_redirect('customer/account/login');
                }
                //        End your addition //

              
                $this->loadLayout();   
                $this->renderLayout();
            }
            you can use one of this code to redirect:
 
            Mage::getSingleton('customer/session')->setBeforeAuthUrl('Restricted Page Url');
            $this->_redirect('customer/account/login');
         
                                OR
                             
            $this->_redirect('customer/account/login/referer/'.Mage::helper('core')->urlEncode('Restricted Page Url'));
          
    Step 3: (optional) If you're using a cms page then follow this step:
          
            In your admin CMS section include a phtml page and write the below code at the top of the page:
          
            if(!Mage::helper('customer')->isLoggedIn()){
                Mage::getSingleton('customer/session')->setBeforeAuthUrl('Restricted Page Url');
                $this->_redirect('customer/account/login');
            }
          
>> Now if you want to use the login url inside a page with referer url follow this:
      
    <?php $referer = Mage::helper('core')->urlEncode(Mage::helper('core/url')->getCurrentUrl()); ?>
    <a href="<?php echo Mage::getUrl('customer/account/login', array('referer' => $referer)) ?>">Login</a>
  
  
  
      

Monday, June 24, 2013

How to add multi-select attribute in Admin product Grid view


Here I've shown a multi-select attribute named "brand".

>> In admin Grid.php find the method "_prepareColumns()" add the following code:

        $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'brand');        // attribute code here
        foreach ( $attribute->getSource()->getAllOptions(true, true) as $option)
        {
            if($option['value'] != '')
            $valArr[$option['value']] =  $option['label'];
        }
        // above code will use to show the dropdown
       
        $this->addColumn('brand',
            array(
                'header'=> Mage::helper('catalog')->__('Brands'),
                'width' => '60px',
                'index' => 'brand',
                'type'  => 'options',
                'options' => $valArr,
                'renderer'  => 'Mage_Adminhtml_Block_Catalog_Product_Renderer_Brands', // Will have to create the renderer.
                'filter_condition_callback' => array($this, '_filterBrandCondition')
        ));
       
        Then in this page you need to define the method "_filterBrandCondition" to filter with selected item.
       
        protected function _filterBrandCondition($collection, $column)
        {
            if (!$value = $column->getFilter()->getValue()) {
                return;
            }
            $this->getCollection()->addFieldToFilter('brand', array('finset' => $value));
        }
       
       
>> Now create the Brands.php file in code/local/Mage/Adminhtml/Block/Catalog/Product/Renderer directory and use the following code:

        class Mage_Adminhtml_Block_Catalog_Product_Renderer_Brands extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
        {
            public function render(Varien_Object $row)
            {
                $brands = explode(',',$row->getBrand());
                $html = "";
                if(count($brands)>0)
                {
                    $html ="<ul>";
                    foreach($brands as $value)
                    {
                        $_productModel = Mage::getModel('catalog/product');
                        $attribute = $_productModel->getResource()->getAttribute("brand");
                        if ($attribute ->usesSource()) {
                            $html .= "<li>".$attribute ->getSource()->getOptionText("$value")."</li>";
                        }
                    }
                    $html .= "</ul>";
                }
                return $html ;
            }
        }
       
 cheer :-)


Wednesday, May 29, 2013

Add extra cost to every shipping method in Magento


>>  I needed to add extra cost to every shipping method price. There would be any other better way to do it but I did the following :

         Open the Rate.php file within app/code/core/Mage/Sales/Model/Quote/Address directory. Find this method:
    public function importShippingRate(Mage_Shipping_Model_Rate_Result_Abstract $rate)
    {
        if ($rate instanceof Mage_Shipping_Model_Rate_Result_Error) {
            $this
                ->setCode($rate->getCarrier().'_error')
                ->setCarrier($rate->getCarrier())
                ->setCarrierTitle($rate->getCarrierTitle())
                ->setErrorMessage($rate->getErrorMessage())
            ;
        } elseif ($rate instanceof Mage_Shipping_Model_Rate_Result_Method) {
            $this
                ->setCode($rate->getCarrier().'_'.$rate->getMethod())
                ->setCarrier($rate->getCarrier())
                ->setCarrierTitle($rate->getCarrierTitle())
                ->setMethod($rate->getMethod())
                ->setMethodTitle($rate->getMethodTitle())
                ->setMethodDescription($rate->getMethodDescription())
                ->setPrice($rate->getPrice())
            ;
        }
        return $this;
    }

    Add extra 10 price with $rate->getPrice() like :  
                   ->setPrice($rate->getPrice()+10)


  Note: Editing the core file is not a good practice. If anyone find any other solution, please must share with me. :)

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