5

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.

1
  • So far I think I was able to found out that it was related to flat tables. If i Disable them my categories would be working properly.
    – zzarbi
    Commented Sep 12, 2012 at 1:35

5 Answers 5

10

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();
2
  • 4
    that's because you are using an upgrade script for something that you should put inside a data-upgrade script (creating a category is DML not DDL). If you are using a data installer you don't need that hack
    – Enrique
    Commented Mar 8, 2013 at 18:22
  • That's interesting! I will look into that.
    – zzarbi
    Commented Mar 14, 2013 at 18:57
9

I ran into the same issue when updating a category via a data install script. The solution provided in the accepted answer did work for updating the category, but can be improved upon as follows:

  • In the solution, the user that triggers the update script is forced to the admin environment. This can be remedied by saving the current store id and switching back at end of the script.
  • It doesn't seem that adding isSecureArea to the registry or disabling update mode had any use (at least for the use case of updating a category).

I ended up with the following data install script for updating a category (in this example, a category is loaded by name, after which the name is updated):

<?php
    $this->startSetup();

    //Switch to admin store (workaround to successfully save a category)
    $originalStoreId = Mage::app()->getStore()->getId();
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

    //update category
    $category = Mage::getModel('catalog/category')
        ->loadByAttribute('name', 'OLD_CATEGORY_NAME');
    if ($category) {
        $category
            ->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
            ->setName('NEW_CATEGORY_NAME')
            ->save();
    }

    //Set store to original value
    Mage::app()->setCurrentStore($originalStoreId);

    $this->endSetup();
?>
1

Try this

<?php
require_once "../app/Mage.php";
umask(0);
Mage::app('default');
$proxy  = new SoapClient("http://127.0.0.1/magento/index.php/api/soap/?wsdl");
$sessionId  = $proxy->login($magento_webservices_username,  $magento_webservices_passwd);

$data = array('name'=>'Nokia',
            'description'=>'',
            'meta_description'=>'',
            'meta_keywords'=>'',
            'default_sort_by'=>'price',
            'available_sort_by'=>'price',
            'is_active'=>1
);
$newCategoryId = $proxy->call($sessionId, 'category.create', array(3, $data, 1));
echo "Category ID: ".$newCategoryId;

?>

And also have a look Magento create category

1
  • Yes I could use Magento SOAP API but I prefer not.
    – zzarbi
    Commented Sep 13, 2012 at 17:03
-1

Take a look at this. Hope it will help you.
http://inchoo.net/ecommerce/magento/how-to-add-new-custom-category-attribute-in-magento/

4
  • This is to add a custom attribute in a category
    – zzarbi
    Commented Sep 12, 2012 at 1:21
  • oops. my bad. this should be the one. sonassi.com/knowledge-base/… Commented Sep 12, 2012 at 1:25
  • I tried that as well, and I have that same effect. Actually i even copy/paste the logic from the Admin Controller that save categories and it still doesn't work.
    – zzarbi
    Commented Sep 12, 2012 at 1:30
  • then, maybe it would be better if you post this question to the magento forum. there are magento geeks in there who could surely help you. :) Commented Sep 12, 2012 at 1:33
-1

I have created multiple categories via installer script.

<?php
$installer = $this;
$installer->startSetup();

Mage::register('isSecureArea', 1);

$category = Mage::getModel('catalog/category');
$category->setPath('1/2/4') // set parent to be root category
->setName('CAT NAME') //Category Name
->setIsActive(1) // Category Status
->setIncludeInMenu(1) // Show in Menu
->setIsAnchor(1) // used for Layered navigation
->setDisplayMode('PAGE') //  Product Only
->setPageLayout('one_column') // Page layout
->save();

$installer->endSetup();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.