dragon2025 2013-09-26 20:28
浏览 40

如何根据类别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

  • 写回答

1条回答 默认 最新

  • dongmi4720 2013-09-27 02:01
    关注

    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();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)