Domanda

I want to create programatically a category in my data folder of my module. The Flat categories option is enabled.

When a I try to create a category like so:

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

I get an error which says that catalog_category_flat doesn't exists. Ok, so I know that flat categories info is kept in catalog_category_flat_store_storenumber table. I looked in the database and I have the following tables:

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

and I want to create a category for store 6. Good, now if I do like this:

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

the category is created without errors, and it sets the info in catalog_category_flat_store_6 but if I go to admin>Manage Categories and cant't see my category created.

I think that when I create a category I should set te store id of the admin(0) so I can see it in the admin panel but then I get the error above, and if I create with store 6 I don't see it in the admin. I'm really stuck.

How can I properly create my category programatically without problems?

È stato utile?

Soluzione

Create category dynamically:

$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();

Then reindex catalog_category_flat dynamically:

$process = Mage::getSingleton('index/indexer')->getProcessByCode('catalog_category_flat');
$process->reindexEverything();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top