Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检索产品';来自Magento的顶级分类_Php_Magento - Fatal编程技术网

Php 检索产品';来自Magento的顶级分类

Php 检索产品';来自Magento的顶级分类,php,magento,Php,Magento,我的Magento商店里有一种产品。它属于C类 我的建筑是A类>B类>C类 使用下面的“我的代码”,在产品页面上显示类别B。我如何调整下面的代码以显示A类 if(Mage::registry('current_product')) { $_category = Mage::registry('current_category'); $parentCategoryId = $_category->getParentId(); $parentCategory = Ma

我的Magento商店里有一种产品。它属于C类

我的建筑是A类>B类>C类

使用下面的“我的代码”,在产品页面上显示类别B。我如何调整下面的代码以显示A类

if(Mage::registry('current_product')) {

    $_category = Mage::registry('current_category');
    $parentCategoryId = $_category->getParentId();
    $parentCategory = Mage::getModel('catalog/category')->load($parentCategoryId);

    $url = strtolower(str_replace(" ","-",$parentCategory->name));

    echo '<a href="/'.$url.'">'.$parentCategory->name.'</a>';

}
if(Mage::registry('current_product')){
$_category=Mage::注册表(“当前_类别”);
$parentCategoryId=$\u category->getParentId();
$parentCategory=Mage::getModel('catalog/category')->load($parentCategoryId);
$url=strtolower(str_replace(“,”-“,$parentCategory->name));
回声';
}
我曾想过也许可以使用URL中的“/”来更改我的代码


非常感谢您的指点。

类别C的父类别是类别B,而不是类别A。要获取给定产品的顶级父类别,您需要爬上类别树:

$category = Mage::registry('current_category');

while ($category->getLevel() != 2) {
    $category = Mage::getModel('catalog/category')->load($category->getParentId());
}

每个类别都有一个级别:级别1是目录根,级别2是顶级类别,超过级别2的任何内容都是子类别。

天哪!我真的花了一整天的时间把我的头发撕下来,你一下子就把它钉好了。先生,请给我来一杯你选择的虚拟啤酒或点心。非常感谢。