Wednesday 31 October 2012

Magento : useful functions list

General functions

Function
$this->getRequest()->getServer(‘HTTP_REFERER’);

Product related functions

Function
$product->getName()
$product->getSku()
Mage::getModel(‘cataloginventory/stock_item’)->loadByProduct($_product)->getQty()
$product->getResource()->getAttribute(‘color’)->getFrontend()->getValue($product)
$product->isSaleable()
$product->isisAvailable()
Mage::getModel(‘catalogrule/rule’)->calcProductPriceRule($product,$product->getPrice())
$product->getTierPrice(1)

Newsletter related functions

Function
$subscriber = Mage::getModel(‘newsletter/subscriber’);
$subscriber->subscribe($user_mail);

Cache

Function
$tags = array(Mage_Catalog_Model_Category::CACHE_TAG); //Category block tag
Mage::app()->cleanCache($tags)

Product images

Function
$_product->getMediaGalleryImages()->getItemByColumnValue(‘label’, ‘LABEL_NAME’)->getUrl()

Customer related functions

Function
$this->helper(‘customer’)->isLoggedIn()

Magento: Very Useful Collection Functions

/**
 * Get Zend_Db_Select instance
 */
$collection->getSelect();
 
/**
 * Get collection size
 */
$collection->getSelect()->getSize();
 
/**
 * Get sql select string or object
 */
$collection->getSelect()->getSelectSql();
 
/**
 * Add select order
 */
$collection->getSelect()->setOrder($field, $direction);
 
/**
 * Add field filter to collection
 *
 * If $attribute is an array will add OR condition with following format:
 * array(
 *     array('attribute'=>'firstname', 'like'=>'test%'),
 *     array('attribute'=>'lastname', 'like'=>'test%'),
 * )
 */
$collection->getSelect()->setOrder($field, $condition);
 
/**
 * Set select distinct
 */
$collection->getSelect()->distinct($flag);
 
/**
 * Get all data array for collection
 */
$collection->getSelect()->getData();
 
/**
 * Reset loaded for collection data array
 */
$collection->getSelect()->resetData();
 
/**
 * Print and/or log query
 */
$collection->getSelect()->printLogQuery(true, true);
More functions below:-
Varien_Data_Collection_Db class extends Varien_Data_Collection class. Here are some more functions present in Varien_Data_Collection class:-
/**
 * Get current collection page
 */
$collection->getSelect()->getCurPage();
 
/**
 * Retrieve collection last page number
 */
$collection->getSelect()->getLastPageNumber();
 
/**
 * Retrieve collection page size
 */
$collection->getSelect()->getPageSize();
 
/**
 * Retrieve collection all items count
 */
$collection->getSelect()->getSize();
 
/**
 * Retrieve collection first item
 */
$collection->getSelect()->getFirstItem();
 
/**
 * Retrieve collection last item
 */
$collection->getSelect()->getLastItem();
 
/**
 * Retrieve collection items
 */
$collection->getSelect()->getItems();
 
/**
 * Clear collection
 */
$collection->getSelect()->clear();
You can also use the select functions as present in Zend_Db_Select class. This class file is present in lib/Zend/Db/Select.php

Tuesday 9 October 2012

Magento :: get URL paths for skin, media, Js OR base URL

If anyone of you facing problems to fetch the Magento URL paths of  skin, media, Js or simple base URL of Magento while customization/programming, then following lines should be helpful for you:
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);
//http://magento.demo/js/
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
//http://magento.demo/index.php/
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
//http://magento.demo/media/
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
//http://magento.demo/skin/
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
//http://magento.demo/