Tuesday, January 21, 2014

Uses of $this->getChildHtml() in Magento


        >> $this->getChildHtml() is a known features to include a child block in a page. I'm simplifying this with an example:
     
      In your XML : 
       <block type="core/template" name="parent_block" template="example/test.phtml">
             <block type="core/template" name="child_block" template="example/test_child.phtml"/>
       </block>

      Now in your test.phtml you can include the child block like : 
             <?php echo $this->getChildHtml('child_block'); ?>

      
       >> You can also pass a variable to this block. I've shown this in my previous post. Now if you want to pass dynamic value to the child block, here's it:

           <?php
                $this->getChild('child_block')->setData('product_id', $productId);
                echo $this->getChildHtml('child_block', false);
            ?>


         Here $productId is your dynamic value and you need to pass "false" (Block Caching No) as  additional parameter in childHtml(). By default it takes "true" (Block Caching Yes).
     
        You can get the product id in test_child.phtml with $this->getProductId()




Tuesday, December 3, 2013

How to create custom category attribute in Magento


>> If you need to create a category attribute there's no option in magento admin. You need to create it through code or sql. I'm showing the two:

1) Here you can use this code with a new module or create a upgrade sql  for an existing module and write below code by changing your attribute code/Label name.

$installer = $this;
$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->addAttribute('catalog_category', 'custom_attribute', array(
    'type' => 'int',        // "varchar" for input type text box
    'group'     => 'General Information',
    'backend' => '',
    'frontend' => '',
    'label' => 'Custom Attribute',
    'input' => 'select',    // "text" for input type text box
    'class' => '',
    'source' => 'eav/entity_attribute_source_boolean',            // Leave blank for input type text box
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible' => true,
    'required' => false,
    'user_defined' => false,
    'default' => '0',        //Leave blank for input type text box
    'searchable' => false,
    'filterable' => false,
    'comparable' => false,
    'visible_on_front' => false,
    'unique' => false,
));

$installer->endSetup();

                                                                           OR
2)  Or you can run direct sql like this way:

        INSERT INTO `eav_attribute` (`attribute_id`, `entity_type_id`, `attribute_code`, `attribute_model`, `backend_model`, `backend_type`, `backend_table`, `frontend_model`, `frontend_input`, `frontend_label`, `frontend_class`, `source_model`, `is_required`, `is_user_defined`, `default_value`, `is_unique`, `note`) VALUES ('', 3, 'mp3', NULL, '', 'varchar', '', '', 'text', 'Mp3 URL', '', '', 1, 0, '', 0, '');

**** Suppose last inserted ID is 134. This ID will place in the following two table.

        INSERT INTO `eav_entity_attribute` (`entity_attribute_id`, `entity_type_id`, `attribute_set_id`, `attribute_group_id`, `attribute_id`, `sort_order`) VALUES ('', 3, 3, 3, 134, 10);

   Note:- Please check in your database for your attribute type_id , set_id and group_id.

        INSERT INTO `catalog_eav_attribute` (`attribute_id`, `frontend_input_renderer`, `is_global`, `is_visible`, `is_searchable`, `is_filterable`, `is_comparable`, `is_visible_on_front`, `is_html_allowed_on_front`, `is_used_for_price_rules`, `is_filterable_in_search`, `used_in_product_listing`, `used_for_sort_by`, `is_configurable`, `apply_to`, `is_visible_in_advanced_search`, `position`, `is_wysiwyg_enabled`, `is_used_for_promo_rules`) VALUES (134, '', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, '', 0, 0, 0, 0);


Note* : Will suggest to follow the first step and follow the second to know the table reference.



Friday, November 29, 2013

How to remove State/Region from Estimate Shipping in Magento


If you want to remove the State/Region dropdown from Estimate shipping block. Find the code
new RegionUpdater('country', 'region', 'region_id', <?php echo $this->helper('directory')->getRegionJson() ?>);  and comment out in the app/design/frontend/enterprise/default/template/checkout/cart/shipping.phtml directory



Tuesday, August 13, 2013

Programmatically change product attribute value in a store Magento


>> We have already known that magento has the save() method to save the data through model. We can save a product data programmatically with the product object. Here's a example:

            $_product = Mage::getModel('catalog/product')->load("PRODUCT_ID");
            $_product->setData('name','value');   OR   $_product->setName('value');
            $_product->save();
            // With this we can change the product name. But the save() method will save globally.

Now suppose you need to change a product visibility for a particular store. Then using this code you can change a particular attribute for a store without saving entire product attributes. Here's it :

            $_product = Mage::getModel('catalog/product')->load("PRODUCT_ID");
            $_product->setStoreId(1);
            $_product->setVisibility(1);
            $_product->getResource()->saveAttribute($_product, 'visibility');




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();
        }