Saturday, March 11, 2017

Magento difference between Mage::getModel() and Mage::getSingleton()

Mage::getModel() will always return a new Object for the given model:

/**
 * Retrieve model object
 *
 * @link    Mage_Core_Model_Config::getModelInstance
 * @param   string $modelClass
 * @param   array|object $arguments
 * @return  Mage_Core_Model_Abstract|false
 */
public static function getModel($modelClass = '', $arguments = array())
{
    return self::getConfig()->getModelInstance($modelClass, $arguments);
}
Mage::getSingleton() will check whether the Object of the given model already exists and return that if it does. If it doesn't exist, it will create a new object of the given model and put in registry that it already exists. Next call will not return a new object but the existing one:

/**
 * Retrieve model object singleton
 *
 * @param   string $modelClass
 * @param   array $arguments
 * @return  Mage_Core_Model_Abstract
 */
public static function getSingleton($modelClass='', array $arguments=array())
{
    $registryKey = '_singleton/'.$modelClass;
    if (!self::registry($registryKey)) {
        self::register($registryKey, self::getModel($modelClass, $arguments));
    }
    return self::registry($registryKey);
}
In your case you always want a completely new Product object/model since every product is unique.

Extending / Overriding Magento controller

We can override any controller using our custom extension, so you will need to create below extension files and folders:-

For ex. here we are overriding checkout cart controller.


  • app/etc/modules/Namespace_YourModuleName.xml: It's a file used to enable our custom module.
  • app/code/local/Namespace/YourModuleName/etc/config.xml: It's a module configuration file in which we'll set up controller class overriding using certain tags as per the Magento conventions.
  • app/code/local/Namespace/YourModuleName/controllers/CartController.php: It's a custom controller class file which we'll use to override the core "Cart" controller class.
In your app/etc/modules/Namespace_YourModuleName.xml file, write below code:

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Namespace_YourModuleName>
<active>true</active>
<codePool>local</codePool>
</Namespace_YourModuleName>
</modules>
</config>

In your app/code/local/Namespace/YourModuleName/etc/config.xml file, write below code:

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Namespace_YourModuleName>
<version>0.1.0</version>
</Namespace_YourModuleName>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<Namespace_YourModuleName before="Mage_Checkout">Namespace_YourModuleName</Namespace_YourModuleName>
</modules>
</args>
</checkout>
</routers>
</frontend>
</config>


In your app/code/local/Namespace/YourModuleName/controllers/CartController.php file, write below code:

<?php
require_once 'Mage/Checkout/controllers/CartController.php';
class Namespace_YourModuleName_CartController extends Mage_Checkout_CartController
{
public function addAction()
{
echo 'I successfully Override Cart Controller';
parent::addAction();
}
public function indexAction()
{
echo 'I successfully Override Cart Controller';
parent::addAction();
}
}
?> 



Magento : Get Skin Url, Base Url , Media Url , Store Url , Js Url , and Current Url

Get Skin Url :-

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);

Get Base Url :

Mage::getBaseUrl();

Secure Skin Url :

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

Unsecure Skin Url :

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

Get Media Url :

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);

Get Js Url :

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);

Get Store Url :

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);

Get Current Url

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

Get Url in cms pages or static blocks


Get Skin Url :

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

Get Base Url :

{{store url=""}}

Get Media Url :

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

Get Store Url :

{{store url='mypage.html'}}

Previous and next navigation buttons in magento2

Download official Magento marketplace extension Previous and next navigation buttons for magento2 that is successfully passed in extension ...