Magento-Functions
A Resource of Magento Functions
Table of Contents
Category
Product
Fetch Product Collection
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToSort('name', 'DESC')
->setOrder('name', 'ASC');
Magento Load Products
Individual Product Helper
--------------------------------------------------------------------------------------
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct();
Load Product from Collection
--------------------------------------------------------------------------------------
// Load by name
$_product = Mage::getModel('catalog/product')->loadByAttribute('name', 'product_name');
// Load by SKU
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', '234SKU93');
//Load by ID (just load):
$_product = Mage::getModel('catalog/product')->load($productID);
Fetch Default Product Info
echo $_product->getShortDescription(); //product's short description
echo $_product->getDescription(); // product's long description
echo $_product->getName(); //product name
echo $_product->getSku(); //product Sku
echo $_product->getPrice(); //product's regular Price
echo $_product->getSpecialPrice(); //product's special Price
echo $_product->getProductUrl(); //product url
echo $_product->getImageUrl(); //product's image url
echo $_product->getSmallImageUrl(); //product's small image url
echo $_product->getThumbnailUrl(); //product's thumbnail image url
echo $_product->getAvailability(); //product's thumbnail image url
?>
Custom Product Attributes
For drop-down Product Attributes use the following code
--------------------------------------------------------------------------------------
echo $_product->getAttributeText('attribute_name') ?>
For all other Product Attribute types
--------------------------------------------------------------------------------------
echo $_product->getAttributeName() ?>
-or-
echo $_product['attribute_name'];?>
Display Product Attributes Globally
--------------------------------------------------------------------------------------
if($product->getResource()->getAttribute('ATTRIBUTE_CODE_HERE')->getFrontend()->getValue($product)) : ?>
echo 'Attribute Title: '.$product->getResource()->getAttribute('ATTRIBUTE_CODE_HERE')->getFrontend()->getValue($product); ?>
endif; ?>
Product Stock Count
$stock_count = (int) Mage::getModel(’cataloginventory/stock_item’)->loadByProduct($this->getProduct())->getQty();
echo $stock_count;
?>
User
/* Check if the customer is logged in or not */
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
/* Get the customer data */
$customer = Mage::getSingleton('customer/session')->getCustomer();
/* Get the customer's full name */
$fullname = $customer->getName();
/* Get the customer's first name */
$firstname = $customer->getFirstname();
/* Get the customer's last name */
$lastname = $customer->getLastname();
/* Get the customer's email address */
$email = $customer->getEmail();
}
Cart
/* Get all the items in the cart */
$cartItems = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
/* Iterate through the items */
foreach ($cartItems as $item) {
/* Load the product and get the custom attribute */
Zend_Debug::dump(Mage::getModel('catalog/product')->load($item->getProduct()->getId())->getMyCustomAttribute());
}
Checkout
Shipping
Retrive Shipping Method from Quote
--------------------------------------------------------------------------------------
$rate = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingRatesCollection();
$rate->getCarrier(); // This will provide you with the carrier code
$rate->getCarrierTitle(); // This will give you the carrier title
$rate->getCode(); // This will give you **current shipping method** code
$rate->getMethod(); // This will provide you with the **shipping method** code
$rate->getMethodTitle(); // This will tell you current shipping method title
$rate->getMethodDescription(); // And this is the description of the current shipping method and **it could be NULL**
General
Working with Blocks
Step 1:
Create your static block
Step 2:
Open the file that references the page you intend to put the block into IE‘page.xml’.
Step 3:
Add this to the appropriate place in the XML file
xxxxxx
Step 4:
Navigate your way to the template folder (app > design > frontend > default > your_theme > template) Open the file that you would like the block to appear in and insert the following code in the appropriate position where xxxxxx is the ‘Identifier’ you set earlier when creating your block.
getChildHtml('xxxxxx') ?>
Render Block within .phtml
getLayout()->createBlock('cms/block')->setBlockId('my-new-block')->toHtml() ?>
####Passing Data Through Blocks
getChild('block_name')->setData("product", $_product); ?>
getData('product'); ?>
Account
Navigation Links
Removing Navigation Links
In local.xml add the following
recurring_profiles
billing_agreements
reviews
downloadable_products
OAuth Customer Tokens
account
account_edit
address_book
orders
tags
wishlist
newsletter
Adding Navigation Links
In local.xml add the following
my_new_section
module/index/index
<_secure>true
URLs
To Retrieve URL path in STATIC BLOCK
---------------------------------------
To get SKIN URL
{{skin url='images/sampleimage.jpg'}}
To get Media URL
{{media url='/sampleimage.jpg'}}
To get Store URL
{{store url='mypage.html'}}
To get Base URL
{{base url='yourstore/mypage.html'}}
TO Retrieve URL path in PHTML
---------------------------------------
Not secure Skin URL:
echo $this->getSkinUrl('images/sampleimage.jpg') ?>
Secure Skin URL
echo $this->getSkinUrl('images/ sampleimage.gif', array('_secure'=>true)) ?>
Get Current URL
$current_url = Mage::helper('core/url')->getCurrentUrl();
Get Home URL
$home_url = Mage::helper('core/url')->getHomeUrl();
Get Magento Media Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
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 Store Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
Get Magento Js Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);
Mage::getBaseUrl() => Gets base url path e.g. http://my.website.com/
Mage::getBaseUrl('media') => Gets MEDIA folder path e.g. http://my.website.com/media/
Mage::getBaseUrl('js') => Gets JS folder path e.g. http://my.website.com/js/
Mage::getBaseUrl('skin') => Gets SKIN folder path e.g. http://my.website.com/skin/
Get DIRECTORY paths (physical location of your folders on the server) – Relative URL Path
Mage::getBaseDir() => Gives you your Magento installation folder / root folder e.g. /home/kalpesh/workspace/magento
Mage::getBaseDir('app') => Gives you your Magento's APP directory file location e.g. /home/kalpesh/workspace/magento/app
Mage::getBaseDir('design') => Gives you your Magento's DESIGN directory file location e.g. /home/kalpesh/workspace/magento/design
Mage::getBaseDir('media') => Gives MEDIA directory file path
Mage::getBaseDir('code') => Gives CODE directory file path
Mage::getBaseDir('lib') => Gives LIB directory file path
Mage::helper('core/url')->getCurrentUrl()
base Mage::getBaseDir()
Mage::getBaseDir('base') /var/www/magento/
app Mage::getBaseDir('app') /var/www/magento/app/
code Mage::getBaseDir('code') /var/www/magento/app/code
design Mage::getBaseDir('design') /var/www/magento/app/design/
etc Mage::getBaseDir('etc') /var/www/magento/app/etc
lib Mage::getBaseDir('lib') /var/www/magento/lib
locale Mage::getBaseDir('locale') /var/www/magento/app/locale
media Mage::getBaseDir('media') /var/www/magento/media/
skin Mage::getBaseDir('skin') /var/www/magento/skin/
var Mage::getBaseDir('var') /var/www/magento/var/
tmp Mage::getBaseDir('tmp') /var/www/magento/var/tmp
cache Mage::getBaseDir('cache') /var/www/magento/var/cache
log Mage::getBaseDir('log') /var/www/magento/var/log
session Mage::getBaseDir('session') /var/www/magento/var/session
upload Mage::getBaseDir('upload') /var/www/magento/media/upload
export Mage::getBaseDir('export') /var/www/magento/var/export