I thought it was about time that someone created a place developers could go to find some of the most commonly used Magento methods/functions. Below you will find many quick ways to achieve your everyday coding requirements in Magento. If there is something you would like to see added to the list please feel free to post a comment or please do so if this list has helped you.
Product Collections
Get product collection
$collection = Mage::getModel('catalog/product')->getCollection();
Get product collection by filtering EAV collections
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('some_attribute', $filter);
Get product collection by filtering Non-EAV collections
$collection = Mage::getModel('catalog/product')->getCollection()
->addFieldToFilter('some_attribute', $filter);
Get product collection and add all available attributes to the collection
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*');
Get product collection and select the attributes that we want to retrieve in our collection
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('some_attribute');
Get product info by product SKU
$collection = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
Get product info by product id
$collection = Mage::getModel('catalog/product')->load($productId);
Get product collection based on the category id
$category = new Mage_Catalog_Model_Category();
$category->load($categoryId);
$collection = $category->getProductCollection();
Get product collection based on the category id and filter by attribute option id
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter(
$attributeCode,
array(
'eq' => Mage::getResourceModel('catalog/product')
->getAttribute($attributeCode)
->getSource()
->getOptionId($attributeOptionId)
))
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToFilter('category_id', $categoryId);
$collection->getSelect()
->group('e.entity_id');
Category Collections
Get the current category collection
$category = Mage::registry('current_category');
Examples
echo $category->getEntityId();
echo $category->getName();
etc…
Get category attributes
$data = Mage::getResourceModel('catalog/category')->getAttributeRawValue($categoryId, "sub_tabs", Mage::app()->getStore()->getId());
Static Blocks
Get static blocks
echo $this->getLayout()->createBlock('cms/block')->setBlockId('maintenance_page')->toHtml();
*****OR*****
echo Mage::app()->getLayout()->createBlock('cms/block')->setBlockId('maintenance_page')->toHtml();
Custom Variables
Get Custom Variables
echo Mage::getModel('core/variable')->loadByCode('overnight_99_text')->getValue('plain');
URLS
Get Base URL
echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
Get Secure URL
echo Mage::getStoreConfig(Mage_Core_Model_Store::XML_PATH_SECURE_BASE_URL);
Get Skin URL
echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
Get URL Referrer
echo Mage::app()->getRequest()->getServer('HTTP_REFERER');
Get Current URL
echo Mage::helper('core/url')->getCurrentUrl();
Get URL Parameters
$params = Mage::app()->getRequest()->getParams();
*****OR*****
$params = Mage::app()->getRequest()->getParam('name');
More coming soon!……
I was just about to ask why there is no API reference manual for Magento and thought, maybe I should do a quick search just to make sure and found this.
http://alanstorm.com/magento_api_helper_manual
…But the question still stands – Why does Magento have to be so difficult?
Hello,
This may be of some help also.
https://www.beckin.com/magento-soap-api-v2/
Thanks