2013-04-22 84 views
0

我想在我的模塊的數據文件夾中以編程方式創建一個類別。平面類別選項已啓用。當在Magento中啓用平板模式時以編程方式創建類別

當我嘗試創建一個類別,象這樣:

$category 
    ->setStoreId(0) 
    ->setName('My category') 
    ->setUrlKey('club-campaigns') 
    ->setPath($rootCategory->getPath()) 
    ->setIsActive(1) 
    ->setIsAnchor(1) 
    ->setIncludeInMenu(1) 
    ->addData($data) 
    ->setCustomDesignApply(1) 
    ->save(); 

我得到它說,catalog_category_flat不存在錯誤。好的,我知道平面類別信息保存在catalog_category_flat_store_storenumber表中。我看着在數據庫中,我有如下表:

catalog_category_flat_store_1
catalog_category_flat_store_2
catalog_category_flat_store_3
catalog_category_flat_store_4
catalog_category_flat_store_5
catalog_category_flat_store_6

,我想創建店面6.良好的類別,現在如果我這樣做:

$category 
    ->setStoreId(6) 
    ->setName('My category') 
    ->setUrlKey('club-campaigns') 
    ->setPath($rootCategory->getPath()) 
    ->setIsActive(1) 
    ->setIsAnchor(1) 
    ->setIncludeInMenu(1) 
    ->addData($data) 
    ->setCustomDesignApply(1) 
    ->save(); 

該類別創建時沒有錯誤,並且它在catalog_category_flat_store_6中設置了信息,但是如果我轉到管理>管理類別並且看不到創建的類別。

我認爲,當我創建一個類別時,我應該設置管理員(0)的te商店ID,以便我可以在管理面板中看到它,但隨後出現上述錯誤,並且如果使用商店6創建了我在管理員看不到它。我很困難。

我該如何正確創建我的類別編程方式沒有問題?

+0

是否重建索引幫助嗎? – 2013-04-22 07:26:42

+0

我會嘗試重新索引,但我認爲我應該在創建類別之前重新索引。因爲我看到類似catalog_category_flat_store_X的表是在reindex進程之後創建的。 – zuzuleinen 2013-04-29 07:18:19

回答

4

動態創建類別:

$category = Mage::getModel('catalog/category'); 
$category->setStoreId(Mage::app()->getStore()->getId()); 

$cat['name'] = "Custom Category Name here"; 
$cat['path'] = "1/2/30"; //parent relationship.. 
$cat['description'] = "categorie's description"; 
$cat['is_active'] = 1; 
$cat['is_anchor'] = 0; //for layered navigation 
$cat['page_layout'] = 'two_columns_left'; 
$cat['url_key'] = "custom-category"; //url to access this category 
$cat['image'] = "custom-category.jpg"; 

$category->addData($cat); 
$category->save(); 

然後動態重新索引catalog_category_flat:

$process = Mage::getSingleton('index/indexer')->getProcessByCode('catalog_category_flat'); 
$process->reindexEverything(); 
+0

如何獲取ID剛創建的類別? – Cherven 2014-05-21 19:56:14

+0

$ category-> getId();通過使用這個獲取當前插入的類別id – Kailas 2016-01-11 05:47:37

相關問題