我使用下面的代码显示产品在我的产品页面上的类别。但是,我使用相同的产品运行多店,并且还显示其他网站的类别。我怎样才能显示我正在访问的网站的类别?
<?php $categories = $_product->getCategoryIds(); ?>
<?php foreach($categories as $k => $_category_id): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
<a href="<?php echo $_category->getUrl() ?>"><?php echo $_category->getName() ?> | </a>
<?php endforeach; ?>
答案 0 :(得分:0)
检查加载的类别ID是否存在
<?php $categories = $_product->getCategoryIds(); ?>
<?php foreach($categories as $k => $_category_id): ?>
<?php $_category= Mage::getModel('catalog/category')->load($_category_id)?>
<?php if($_category->getId()):?>
<a href="<?php echo $_category->getUrl() ?>">
<?php echo $_category->getName() ?> | </a>
<?php endif;?>
<?php endforeach; ?>
答案 1 :(得分:0)
使用此代码获取当前商店的类别。
$storeId = Mage::app()->getStore()->getStoreId();
$rootCategoryId = Mage::app()->getStore($storeId)->getRootCategoryId();
$categoriesCollection = Mage::getModel('catalog/category')
->getCollection()
->setStoreId($storeId)
->addFieldToFilter('is_active', 1)
->addAttributeToFilter('path', array('like' => "1/{$rootCategoryId}/%"))
->addAttributeToSelect('*');
foreach($categoriesCollection as $cat)
{
$id = $cat->getId();
$name = $cat->getName();
}