[英]Magento getModel('catalog/category') returns empty category name, id and url
我有一個擴展,可以從magento檢索類別。 您可以在下面看到代碼。 該代碼可用於多個站點,但對於其中兩個站點,我得到的是空值。 但是,它返回類別對象,僅此而已。 當我嘗試獲取類別的名稱,ID或URL時,它為空。
這是我如何獲得類別:
// Load all categories (flat)
$activeCategoryCollection = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*');
// Load all categories (tree)
$categoriesArray = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('name')
->addAttributeToSort('path', 'asc')
->load()
->toArray();
然后在foreach循環中,執行以下操作來創建我的對象:
// Expose composite category data
foreach ($categoriesArray as $categoryId => $category)
{
// Check if category is not at root category level
if (isset($category['name']) && isset($category['level']) && $category['level'] > $rootCategory['level'])
{
// Remove root category path from category
$path = str_replace($rootCategory['path'] . '/', '', $category['path']);
// Explode parent categories
$parents = explode('/', $path);
$name = '';
$url = '';
// Get category name and urls for each parent
foreach ($parents as $parent)
{
$name .= $activeCategorNamesById[$parent] . '>';
$url .= $activeCategoryUrlsById[$parent] . '>';
}
// Get products in category with their positions
$currentCategory = Mage::getModel('catalog/category')->load($categoryId);
$collection = $currentCategory->getProductCollection()->addAttributeToSort('position');
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
// Index category data
$categories[$categoryId] = array(
'name' => substr($name, 0, -1),
'level' => $category['level'],
'ids' => str_replace('/', '>', $path),
'url' => substr($url, 0, -1),
'id' => $categoryId,
'products' => $currentCategory->getProductsPosition()
);
}
}
目前,我無法訪問這些站點的ftp來手動更新擴展名,也無法在商店中對其進行更新。 我不知道問題是什么以及如何測試。 建議之一是,在那些商店中,他們已為類別自定義字段以顯示ID,標題和URL,但我仍然不知道該如何看待。 我可以訪問他們的magento管理網站,但與其他網站相比,我在這里找不到任何不同之處。
編輯:以下代碼返回空數組,即使它們在系統中具有類別
var_dump($this->getCategories());
此外,在“ System > Configuration
下的其他站點中,有“ Catalog
,但對於不起作用的站點,是“ System > Configuration > Catalogue
。 我不確定它們之間是否有區別。
$name
, $path
和$url
在哪里定義
嘗試
$categories[$categoryId] = array(
'name' => substr($category['name'], 0, -1),
'level' => $category['level'],
'ids' => str_replace('/', '>', $category['path']),
'url' => substr($category['url'], 0, -1),
'id' => $categoryId,
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.