Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a category to Magento via Setup script?

Tags:

php

magento

I actually can add a category via setup script, the thing is for some reason some of the fields doesn't get set properly. Here's is my code

$this->startSetup();
Mage::register('isSecureArea', 1);

$category = Mage::getModel('catalog/category');
$category->setPath('1/2') // set parent to be root category
    ->setName('Category Name')
    ->setUrlKey('category-name')
    ->setIsActive(0)
    ->setIncludeInMenu(1)
    ->setInfinitescroll(1)
    ->setDisplayMode('PAGE')
    ->setLandingPage($idToCmsBlock)
    ->setPageLayout('anotherLayoutThanDefault')
    ->setCustomUseParentSettings(0)
    ->setCustomLayoutUpdate('<reference name="head"><action method="addCss"><stylesheet>css/somecss.css</stylesheet></action></reference>')
->save();
$this->endSetup();

After running this script, I have a category created with all my value set in the EAVs table. However the Flat table will be missing displayMode, landingPage, pageLayout, customLayoutUpdate even if I re-index the flat table.

The weird thing is that if I go in the admin, I can see all those fields properly set but if I go in my frontend most of those fields are ignored. I will have to go to the admin, unset those value and reset them for each of them to work properly.

Also let say I use setEnabled(1), my category will be "enable" in the admin but not show up in the frontend.

PS: I have Flat Category activated, if I disable it seems to work fine but if I re-index it still not working.

like image 320
zzarbi Avatar asked Sep 12 '12 01:09

zzarbi


People also ask

How do I assign a product to a category in Magento 2 programmatically?

To add to the categories you would assign $product->getCategoryIds() to a variable, add your IDs to the array, then setCategoryIds(), then save.

What is setup script in Magento 2?

Save. As the name defines: Setup script files used to perform some action on your data or table while installing or upgrading up your module. All the setup scripts files are created under Setup folder which is at path: app/code/Webkul/Test/Setup.


1 Answers

I finally found it, I'm not sure why but those fields are not showing up properly because they were inserted for the default store (storeId=1) because my script is running in an update script. You need to use the storeId 0.

With this information you would think that the solution would be something like :

$this->startSetup();
Mage::register('isSecureArea', 1);

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$category = Mage::getModel('catalog/category');
$category->setPath('1/2') // set parent to be root category
    ->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
    ->setName('Category Name')
    ...
    ->save();
$this->endSetup();

But this code doesn't work either. Indeed after looking into Mage::app() (Mage_Core_Model_App Line 804) I noticed a IF condition that would always return the default store if you're in a setup script.

The trick is to fake that you're not in a setup script, my working solution is:

$this->startSetup();
Mage::register('isSecureArea', 1);

// Force the store to be admin
Mage::app()->setUpdateMode(false);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$category = Mage::getModel('catalog/category');
$category->setPath('1/2') // set parent to be root category
    ->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
    ->setName('Category Name')
    ...
    ->save();
$this->endSetup();
like image 154
zzarbi Avatar answered Nov 07 '22 15:11

zzarbi