Part 1: Fundamentals

Sunday, 26 January 2014

Magento is established based on Principles of Object-Oriented Programming on Zend Framework with MVC architecture. It uses Event-Driven Architecture (EDA) to manage events. In this part, we will talk about: 1. Describe and apply basic principles and processes of Object Oriented Programming (OOP) and Model-View-Controller (MVC) to build Magento websites 2. Identify and describe principles of Event-Driven Architecture (EDA) 3. Sample questions & answers I - Describe and apply basic principles and processes of Object Oriented Programming (OOP) and Model-View-Controller (MVC) to build Magento websites 1. Object Oriented Programming OOP  Abstraction Use function/class without knowing exactly how that function/class accomplishes its tasks. What we concern is parameters sent to it and its returned value. A typical example is function _init() in class Mage_Core_Model_Abstract, this function will be executed to define with which table in CSDL the model will work. - Inherited class of Abstract class is just the instance of the abstract class. - Calling abstract function requires its instance function, which means in the children class you must rewrite this function.  Encapsulation Objects are encapsulated. When changing data, you must go through functions to work with that object. In other words, a product cannot know information of another product. The common thing between the 2 products is the method working with them.  Polymorphism Object instances of a same class are different. For example, products of different types (simple/grouped/…) have different price calculated methods.  Inheritance Inheritance is exploited in Magento, especially Varien_Object class which is inherited by many classes in Magento. (Function setData/getData is inherited from Varien_Object). 2. Model-View-Controller MVC  Model Model is the classes providing data, service related to data and business logic. This class works directly with data and provides data for other elements. In a module, these classes are contained in Model folder.  View View is the classes which define data presented method (not update data). These classes are contained in the folder Block of module.  Controller Controller is the classes which control application stream. They receive input which is requirement of users through HTTP request, transfer those requests to the classes which process the requests directly. From the link, Router will get to Controller which controls that link. In Magento module, these classes are contained in controller folder. II - Identify and describe principles of Event-Driven Architecture (EDA) 1. Event-Driven Architecture 2. Magento event  Event Listener & Generator: When an event happens, this element will Listen the event, create signals of that event and send that signal to Event Engine.  Event Engine: When receiving signals of the event, this element will send them to factor Client to process that event.  Client: When receiving signals of the event, this element will execute steps to respond to that event.

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