Thursday, August 1, 2013

How to use WYSIWYG editor in Admin form field Magento


>> You need to do two small changes. Here's it

    1) open the Edit.php page on your module Yourmodule_Block_Adminhtml_Yourblock_Edit
    Add the method.

     protected function _prepareLayout() {
        parent::_prepareLayout();
        if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) {
            $this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
        }
    }


    2) Now open the Form.php change the field you want to change like following:

        $fieldset->addField('description', 'editor', array(
            'name'      => 'description',
            'label'     => Mage::helper('Youmodule')->__('Content'),
            'title'     => Mage::helper('Youmodule')->__('Content'),
            'style'     => 'height:300px; width:700px',
            'config'    => Mage::getSingleton('cms/wysiwyg_config')->getConfig(),
            'wysiwyg'   => true,

            'required'  => false,
        ));

     ...........................

Friday, July 26, 2013

Magento :: get static block content mapped with category


>> You can display a static block in a category page by Managing the category from admin.



     But if you want the block in a custom page for that category. You use the following code:

         // load the category
        $_category = Mage::getModel('catalog/category')->load($categoryId);
       
        // Fetching the block data
        $html = $this->getLayout()->createBlock('cms/block')
                    ->setBlockId($_category->getLandingPage())
                    ->toHtml();
                   
        echo $html;





Thursday, July 4, 2013

All categories of a product show in breadcrumbs in magento


>> Here's I've shown how to show all categories of a product in breadcrumbs. It looks like :
      Home / cat1 | Home / cat1 / cat2 | Home / cat3 / Product  You can use a separator before every Home link.
  Edit the file app/code/core/Mage/Catalog/Block/Breadcrumbs.php
 Note* : You must copy the file in your local directory OR override the block before Edit.

Find the method _prepareLayout() and replace with the following code:

        protected function _prepareLayout()
        {
            if ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs')) {
              
                /************                Customize for product breadcrumbs            ***********/
                                              
                $product = $this->getProduct();
                $categories = $product->getCategoryIds();
                $parentArr = array();
                $childArr = array();
              
                foreach($categories as $catId){
                    if($catId != 2){
                        $category = Mage::getModel('catalog/category')->load($catId);
                      
                        if(in_array($category->getParentId(),$categories) && $category->getParentId() != 2){
                            if(!array_key_exists($category->getParentId(),$parentArr)){
                                $childArr[] = $category->getId();
                                $parentArr[$category->getParentId()] = $childArr;
                            }else{
                                if($parentArr[$category->getParentId()] == '') $childArr = array();
                                array_push($childArr,$category->getId());
                                $parentArr[$category->getParentId()] = $childArr;
                            }
                        }else{
                            $parentArr[$category->getId()] = '';
                        }
                    }
                }
              
                $i=0;
                foreach($parentArr as $pId => $cArr){
                    $cat = Mage::getModel('catalog/category')->load($pId);
                    $firstClass = ($i++ == 0) ? $cat->getId().' first' : $cat->getId();
                  
                    $breadcrumbsBlock->addCrumb('home product '.$firstClass, array('label'=>Mage::helper('core')->__('Home'),
                                                'title'=>Mage::helper('core')->__('Home Page'), 'link'=>Mage::getBaseUrl()));
                    $breadcrumbsBlock->addCrumb(strtolower(str_replace(' ','_',$cat->getName())), array('label'=> $cat->getName(),
                                                    'title'=> $cat->getName(), 'link'=> $cat->getUrl()));
                    foreach($cArr as $cId){
                        $child = Mage::getModel('catalog/category')->load($cId);
                        $breadcrumbsBlock->addCrumb('home product '.$cId.$cat->getId(), array('label'=>Mage::helper('core')->__('Home'),
                                                    'title'=>Mage::helper('core')->__('Home Page'), 'link'=>Mage::getBaseUrl()));
                        $breadcrumbsBlock->addCrumb(strtolower(str_replace(' ','_',$cat->getName())).$cId, array('label'=> $cat->getName(),
                                                        'title'=> $cat->getName(), 'link'=> $cat->getUrl()));
                        $breadcrumbsBlock->addCrumb(strtolower(str_replace(' ','_',$child->getName())), array('label'=> $child->getName(),
                                                    'title'=> $child->getName(), 'link'=> $child->getUrl()));
                    }      
                }
                if(count($parentArr) == 0){
                    $breadcrumbsBlock->addCrumb('home product first', array('label'=>Mage::helper('core')->__('Home'),
                                                'title'=>Mage::helper('core')->__('Home Page'), 'link'=>Mage::getBaseUrl()));
                }
                $breadcrumbsBlock->addCrumb('prod'.$product->getId(), array('label'=> $product->getName(), 'title'=> $product->getName(), 'link'=> ''));
              
                /***********            Customize for product breadcrumbsBlock    END            *************/
              
            }
            return parent::_prepareLayout();
        }

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