Sunday, July 26, 2015

Upload Website Logo from Admin in Magento

In default Magento when ever you want to change the website logo than must upload a logo into folder and assign that logo path in admin panel. So it's some complicated method for upload logo in Magento. Better is that you can directly upload a logo from Magento admin panel and also delete and change whatever you want only from admin panel.
For that you must change some core functionality.here below listed changes in files with location
For website logo change from admin in System -> Configuration -> General -> Design -> Header in Logo Image Source field which is text field defaul so you must change it to image upload field for upload image.that field declares in app\code\core\Mage\Page\etc\system.xml
Open that file and find below code

<logo_src translate="label">
    <label>Logo Image Src</label>
    <frontend_type>text</frontend_type>
    <sort_order>10</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>

which defines image, logo source and save logo path also now replace below code instead of this.

<logo_src translate="label comment">
    <label>Logo Image Source</label>
    <comment>Allowed file types: PNG, GIF, JPEG files</comment>
    <frontend_type>image</frontend_type>
    <backend_model>adminhtml/system_config_backend_image_logo</backend_model>
    <base_url type="media" scope_info="1">logo</base_url>
    <sort_order>10</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
</logo_src>
Here we just add image incited of text and add a new model for handling upload image file. Here enter any message in `comments` and `base_url` declare save logo path in 'media' folder.

Now you must create a model for logo image for handling uploaded image file.For that see `app\code\core\Mage\Adminhtml\Model\System\Config\Backend\Image\` folder in that `Favicon.php` file already exits so just copy that file and rename to `Logo.php`


  • Change class name `Mage_Adminhtml_Model_System_Config_Backend_Image_Favicon` to `Mage_Adminhtml_Model_System_Config_Backend_Image_Logo`(Here file name and class name same as `backend_model` declare in system.xml file)
  • Change `const UPLOAD_DIR = 'favicon';` to `const UPLOAD_DIR = 'logo';` (Here directory name same as `base_url` declare in system.xml file)
  • In that file also can change `_getAllowedExtensions()` function's return array according to requirement.

Now clear all caches and check on backed website logo image directly upload from admin and also display logo there after upload.

Now for display logo in front-end you must change `getLogoSrc()` function which declare in header.html file for display logo.Open app\code\core\Mage\Page\Block\Html\Header.php file and file function code as below.

public function getLogoSrc()
{
 if (empty($this->_data['logo_src'])) {
     $this->_data['logo_src'] = Mage::getStoreConfig('design/header/logo_src');
    }
    return $this->getSkinUrl($this->_data['logo_src']);
}

Replace this function as below for just add `media` folder URL.

public function getLogoSrc()
{
 if (empty($this->_data['logo_src'])) {
     $this->_data['logo_src'] = Mage::getStoreConfig('design/header/logo_src');
    }
    return Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'logo/'.$this->_data['logo_src'];
}

Now uploaded logo display in fronted.

1 comment :