2

This is my code:

$product_id = Mage::registry('current_product')->getId();
$product = Mage::getModel('catalog/product')->load($product_id);
$categories_name = array(); $i = 0;
$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
    $_cat = Mage::getModel('catalog/category')->load($category_id) ;
    if ($_cat->getName() == "Hobby"){
        $name = "Pasionat";
    }else{
        $name = $_cat->getName();
    }
    $categories_name[$i] = $name;
    $i++;
}

its working perfectly when I'm on the product page, but when I'm on other page im getting this error: Fatal error: Call to a member function getId() on a non-object

3

Chester, Mage::registry('current_product') is working only onproduct. that

if(Mage::registry('current_product')){
product_id = Mage::registry('current_product')->getId();
$product = Mage::getModel('catalog/product')->load($product_id);
$categories_name = array(); $i = 0;
$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
    $_cat = Mage::getModel('catalog/category')->load($category_id) ;
    if ($_cat->getName() == "Hobby"){
        $name = "Pasionat";
    }else{
        $name = $_cat->getName();
    }
    $categories_name[$i] = $name;
    $i++;
}
}
3

You need to check, whether Mage::registry('current_product') contains a product, e.g.

is_object(Mage::registry('current_product'))

or

Mage::registry('current_product') instanceof Mage_Catalog_Model_Product

A better way would be (as Amit indicated) is to only add your block or code to the product detail page.

And a second tipp: Don't load every category on its on, use a collection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.