Magento URLs

Saturday 21 September 2013

MAGENTO: GET SKIN URL, GET MEDIA URL, GET BASE URL, GET STORE URL


Following code blocks will return magento default urls:

Get  Current URL
Mage::helper('core/url')->getCurrentUrl();


Get Home URL

Mage::helper('core/url')->getHomeUrl();

Get URL from url key

Mage::getUrl($urlKey, $params);

Get Magento Media Url

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);

Get Magento Skin Url

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);

Get Magento unsecure Skin Url 

$this->getSkinUrl('images/imagename.jpg');

Get Magento secure Skin Url 

$this->getSkinUrl('images/imagename.gif', array('_secure'=>true));

Get Magento Store Url

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);

Get Magento Js Url

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);


Get URL path in STATIC BLOCK


Skin Url

{{skin url='images/sampleimage.jpg'}}

Media URL

{{media url='/sampleimage.jpg'}}

Store URL

{{store url='url_key'}}

Base URL

{{base url='store/key.html'}}


I hope this will help :)

Magento - CMS Block/Page installer

You can manually create CMS Pages in Magento from CMS -> Pages. Similarly, you can create Static Blocks from CMS -> Static Blocks.
This article will show how to create and update CMS pages and Static blocks programmatically from code for single store websites and multi store websites. Also it will resolve your issue "A block identifier with the same properties already exists in the selected store" when you try to update a cms block.
1.Single store websites
  • Create/update CMS Pages
$storeId=0 // put your current store id here
$cmsPage array(
            'title' => 'CMS SAMPLE PAGE',
            'identifier' => 'test_url',
            'content' => 'cms page content',            
            'is_active' => 1,
            'sort_order' => 0,
            'stores' => array($storeId),
            'root_template' => 'three_columns'
            );
             
$page = Mage::getModel('cms/page')->load($cmsPage ['identifier']);
        if ($page->getId()) {
            $page->setContent($cmsPage ['content']);
           $page->setTitle($cmsPage ['title']);
           $page->save();
        } else {
            Mage::getModel('cms/page')->setData($cmsPage )->save();
        }

  • Create/update Static Blocks


$storeId=0 // put your current store id here
$staticBlock array(
                'title' => 'Test CMS Block',
                'identifier' => 'sample_block',                   
                'content' => 'cms block content',
                'is_active' => 1,                   
                'stores' => array($storeId)
                );
                 
$block= Mage::getModel('cms/block')->load($staticBlock ['identifier']);
        if ($block->getId()) { // resolving issue " A block identifier with the same properties                                                         //already exists in the selected store "            $block->setContent($staticBlock ['content']);
           $block->setTitle($staticBlock ['title']);
           $block->save();
        } else {
            Mage::getModel('cms/block')->setData($staticBlock )->save();
        }
1.Multi store websites
In muti store websites there will be multiple cms blocks and cms pages with same identifier. So when we fetch cms blocks/pages using identifier  it may load wrong block and should cause issues on updates.

We can use a custom function for resolving this issue,
/**
common function for creating and updating cms pages and blocks in multi store websites
*/
function saveCmsData($data, $storeId, $isPage = false) {
        $store = array($storeId);
        $store[] = Mage_Core_Model_App::ADMIN_STORE_ID;
        if ($isPage) {
            $model = Mage::getModel('cms/page');
        } else {
            $model = Mage::getModel('cms/block');
        }
        $collection = $model->getCollection()
                ->addFieldToFilter('identifier', $data['identifier'])
                ->addStoreFilter($storeId)
                ->addFieldToFilter('store_id', array('in' => $store));
        $cmsItem = $collection->getFirstItem();
        if ($cmsItem && ($cmsItem->getBlockId()||$cmsItem->getPageId())) {
            $cmsItem->load();
            $oldData = $cmsItem->getData();
            $data = array_merge($oldData, $data);
            $cmsItem->setData($data)->save();
        } else {
            $model->setData($data)->save();
        }
        return;
    }
  • Create/update CMS Pages
$storeId=1 // put your current store id here
$cmsPage array(
            'title' => 'CMS SAMPLE PAGE',
            'identifier' => 'test_url',
            'content' => 'cms page content',            
            'is_active' => 1,
            'sort_order' => 0,
            'stores' => array($storeId),
            'root_template' => 'three_columns'
            );
saveCmsData($cmsPage , $storeId, true);
  • Create/update CMS Blocks
$storeId=1 // put your current store id here
$staticBlock array(
                'title' => 'Test CMS Block',
                'identifier' => 'sample_block',                   
                'content' => 'cms block content',
                'is_active' => 1,                   
                'stores' => array($storeId)
                );saveCmsData($staticBlock, $storeId, false);


Hope this helps. Thanks.:)

 

Contact Form

Name

Email *

Message *

Most Reading