我有一個擴展名來檢索magento的類別。你可以看到下面的代碼。該代碼適用於多個網站,但其中兩個我得到的是空值。但是,它返回類別對象,但它就是這樣。當我嘗試獲取類別的名稱,id或url時,它是空的。Magento getModel('catalog/category')返回空類別名稱,ID和網址
這是我得到的類別:
// 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循環中我做的followning創建我的對象:
// 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,標題和網址等自定義字段,但我仍然不知道如何看到這些信息。我有權訪問他們的magento管理站點,但在那裏我找不到與其他人相比有什麼不同。
編輯:下面的代碼返回空數組,即使他們在他們的系統類別
var_dump($this->getCategories());
此外,在其他網站System > Configuration
下有Catalog
,但對於那些沒有工作的一個很System > Configuration > Catalogue
。我不確定他們之間有什麼區別。
我剛剛更新的代碼中的問題,以顯示其中'$ name''$ path'和'$ url'定義和組。你能告訴我什麼是錯的嗎? – erincerol 2014-10-20 09:17:29