max7
max7

Reputation: 800

how to get category name based on category ID Magento

I'm currently using the following code to create variable tags for use in the Magento CMS to print out various portions of dynamic data for a product:

<?php 

    $_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $this->getData('sku'));

    $_categories = Mage::getModel('catalog/category')->loadByAtribute('id', $this->getData('id')); 

    switch($this->getData('valuetype')){       
        case "retail":
            echo number_format($_product->getPrice(), 2, '.', ',');
            break;
        default:
        case "final":
            echo number_format($_product->getFinalPrice(), 2, '.', ',');
            break;
        case "diffDollar":
            $difference = $_product->getPrice() - $_product->getFinalPrice();
            echo number_format($difference, 2, '.', ',');
            break;
        case "diffPercent":
            $difference = (1 - ($_product->getFinalPrice() / $_product->getPrice())) * 100;
            echo number_format($difference, 0, '.', ',');
            break;
        case "prodName":
            echo ($_product->getName());
            break;
        case "urlPath":
            $prodName = ($_product->getName());
            $prodName = strtolower($prodName);
            $path = explode(" ", $prodName);
            $path = implode("-", $path);
            echo $path;
            break;
        case "catName":
            $catName = ($_categories->getName());
            echo $catName;
            break;
    }
?>

Inside of the CMS I would use the following to bring in the data for $_categories:

{{block type="catalog/category" id="176" template="catalog/product/cmsprice.phtml" valuetype="catName"}}

For $_product I would use:

{{block type="catalog/products" sku="1000145" template="catalog/product/cmsprice.phtml" valuetype="retail"}}

Using $_product works perfectly but when I try to use $_categories to print out the category name, the page from the CMS does not render on the homepage but the rest of the page appears and there are no errors displayed.

I feel like I'm close to getting this but can't see what I'm missing. Any help would be greatly appreciated

Upvotes: 0

Views: 17167

Answers (1)

otherchirps
otherchirps

Reputation: 1846

Your model fetching at the start looks a bit off:

$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $this->getData('sku'));

$_categories = Mage::getModel('catalog/category')->loadByAtribute('id', $this->getData('id'));

This looks like you're trying to load a category model using the product's id? You need a category id, to load a category instance.

Also, unsure if you're worried about it, but how do you cope with products in multiple categories?

If you've already got the product instance, for which you want to find all the category names it's in, this is something I ran with:

$categories = $_product->getCategoryCollection()
                       ->addAttributeToSelect('name')
                       ->addAttributeToFilter('is_active', array('eq' => 1));
$value = array();
foreach($categories as $category) {
    $value[] = $category->getName();
}

Upvotes: 1

Related Questions