Wednesday 29 January 2014

Dispatch Event Creation in magento



You can create an event with the Mage::dispatchEvent method. If you have an object like the order then you have to define a parameterlist as Array. array( 'order' => $order)
Just implement following line to your extension class:
Mage::dispatchEvent('the_event_name',array( 'order' => $order));


To observe an event, specify the observer in the config.xml at /app/(codepool)/Extensionname/ .
<config>
  <global>
    <events>
      <the_event_name>
        <observers>
          <extensionname>
            <type>singleton</type>
            <class>Extensionname_Model_Observer</class>
            <method>methodName</method>
          </extensionname>
        </observers>
      </the_event_name>
    </events>
  </global>
</config>

Create an Extension and a model (Observer.php): /app/(codepool)/Extensionname/Model/Observer.php

class Extensionname_Model_Observer
{
    public function methodName($event)
    {
        $order = $event->getOrder();
        //for example send a new order mail to customer
        $order->sendNewOrderEmail()->setEmailSent(true)->
        save();
        //do something else ....
    }
}

Magento useful functions



General functions

Function Description
$this->getRequest()->getServer(‘HTTP_REFERER’);              Get the referer URL

 

Product related functions

Function Description
$product->getName() Get product name.
$product->getSku() Get product sku
Mage::getModel(‘cataloginventory/stock_item’)->loadByProduct($_product)->getQty() Get product stock
$product->getResource()->getAttribute(‘color’)->getFrontend()->getValue($product) Get a product attribute value (like color/size)
$product->isSaleable() checks if a product is salable
$product->isisAvailable() checks if a product type is salable
Mage::getModel(‘catalogrule/rule’)->calcProductPriceRule($product,$product->getPrice()) Gets the product final price after all catalog rules have been applied
$product->getTierPrice(1) Gets the product tier price if can be applied to the customer

Newsletter related functions

Function                    Description
$subscriber = Mage::getModel(‘newsletter/subscriber’);
$subscriber->subscribe($user_mail);
                          Add mail to subscribers list

 

Cache

Function Description
$tags = array(Mage_Catalog_Model_Category::CACHE_TAG); //Category block tag
Mage::app()->cleanCache($tags)
Clean specific cache tag (in this case the category list block)

Product images

Function Description
$_product->getMediaGalleryImages()->getItemByColumnValue(‘label’, ‘LABEL_NAME’)->getUrl() Get the url of a specific produc image given its label

Customer related functions

Function Description
$this->helper(‘customer’)->isLoggedIn() Checks if the customer is logged

Tuesday 28 January 2014

Displaying Currency Symbol after Price in Magento


 Change Price Symbol after Price

There is a one Trick for changing position of Currency Symbol from LEFT to RIGHT in Magento.

For that you have to do Minor changes in your Language File. Following is the Directory Structure of File.

=> root/lib/Zend/Locale/Data/en.xml (For English Language)

=> around line 2611 you can see following code.
<currencyFormat>
<pattern>¤#,##0.00;(¤#,##0.00)</pattern>
</currencyFormat>

=> Now Change above code with Following code.

  <currencyFormat>
<pattern>#,##0.00 ¤;(#,##0.00 ¤)</pattern>
</currencyFormat>

Note: The little square ( ¤ ) sets the currency symbol position after Price in magento.

=> Now clear Cache of your store and You can see changes.