Archive for : September, 2010

Magento check if customer is logged in

This is a method to verify if a customer is logged in or not:

// Code to execute

Magento get all products

Next code is a method to get all product from magento database:

$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('status', 1);//enabled
$products->addAttributeToFilter('visibility', 4); //catalog, search
$products->addAttributeToFilter('type_id', 'simple'); //catalog, search
$prodIds = $products->getAllIds();
foreach($prodIds as $productId)
$product = Mage::getModel('catalog/product');

Magento get all categories

Here is a sample code to extract all categories from magento database:

$collection = Mage::getModel('catalog/category')-<getCollection()-<addAttributeToSelect("name");
$catIds = $collection-<getAllIds();
$cat = Mage::getModel('catalog/category');
foreach ($catIds as $catId) {
$category = $cat-<load($catId);
print $category-<getName() . "

Magento get products from a specific category

Next code is a sample to extract all products from a specific category:

$prodCat = Mage::getModel('catalog/category')->load($catId);
$prodCat = $prodCat->getProductCollection();
foreach( $prodCat->getAllIds() as $prodId)
print $_product->getName()."

Javascript confirm window

Next code can be used to add a confirm window from javascript:

var conf = confirm("Are you sure do you want to delete?");
if (conf == true)
// Code to execute

Magento countries and regions

Get all countries:

$countries = Mage::getModel('directory/country_api')->items();

Get regions based on country code:

$regions = Mage::getModel('directory/region_api')->items($countryCode);

Magento get stores

Get all Magento Stores from database:

$stores = Mage::app()->getStores();

Magento Url’s

Bellow are few methods to get some useful magento url’s:






Magento get bundled products

Next code can be used to extract bundled products:

$bundled_product = new Mage_Catalog_Model_Product();
$selectionCollection = $bundled_product->getTypeInstance(true)->getSelectionsCollection($bundled_product->getTypeInstance(true)->getOptionsIds($bundled_product), $bundled_product);
$bundled_items = array();
foreach($selectionCollection as $option)
$bundled_items[] = $option->product_id;

Magento session

To set a value in magento session use:

Mage::getSingleton('core/session')->setMySessionData('My session data');

To get variable value from magento session use:

$mySessionData = Mage::getSingleton('core/session')->getMySessionData();