Magento Product and Category Related Functions

Magneto Product Collection

$collection = Mage::getModel(‘catalog/product’)->getCollection()->addAttributeToSelect(‘*’);

Get product of particular category

$products = Mage::getModel(‘catalog/category’)->load(category_id); //put your category id here
$productslist = $products->getProductCollection()->addAttributeToSelect(‘*’);

Product details from Product ID.

$productid=1234;
$model = Mage::getModel(‘catalog/product’) //getting product model
$_product = $model->load($productid); //getting product object for particular product id
echo $_product->getShortDescription(); //product’s short description
echo $_product->getDescription(); // product’s long description
echo $_product->getName(); //product name
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

product collection from the category and then refine it down from there.

$products = Mage::getModel(‘catalog/category’)->load(410)
->getProductCollection()
->addAttributeToSelect(‘*’)
->addAttributeToFilter(‘status’, 1)
->addAttributeToFilter(‘visibility’, 4)
->addAttributeToFilter(‘special_price’, array(‘neq’ => “”))
->addAttributeToFilter(‘discontinued’, array(‘neq’ => 1))
->setOrder(‘price’, ‘ASC’);

Get Current product category id

$_helper = $this->helper(‘catalog/output’);
$_category_detail=Mage::registry(‘current_category’);
echo $_category_detail->getName(); //gives current category name
echo $_category_detail->getId(); //gives current category id

Display all categories in magento

function get_categories(){
$category = Mage::getModel(‘catalog/category’);
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
$arr = array();
if ($ids){
foreach ($ids as $id){
$cat = Mage::getModel(‘catalog/category’);
$cat->load($id);
$arr[$id] = $cat->getName();
}
}
return $arr;
}
$arr = get_categories();
$arr = array_flip($arr);

Get Parent Category of current category

echo $cat_idd = $this->getCurrentCategory()->getParentCategory()->getId();

Get the category ID by product id

$productObj = Mage::getModel(‘catalog/product’)->load($product_id);
$categoryIds = $productObj->getCategoryIds();

To find the parent ID of current category

$_cat = new Mage_Catalog_Block_Navigation();
$curent_cat = $_cat->getCurrentCategory();
$curent_cat_id = $curent_cat->getId();

Get all categories for a product in Magento

$product = Mage::getModel(‘catalog/product’)->load($productId);
$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
$_cat = Mage::getModel(‘catalog/category’)->load($category_id) ;
echo $_cat->getName();
}

Advertisements

2 thoughts on “Magento Product and Category Related Functions

  1. Pingback: How to show the Exact searh result in Magento | Jeremy's Blog

  2. Pingback: All Catogory Listing in Magento | Magento For Fresher

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s