How to add a category to Magento via Setup script?

I really can add a category through a setup script, thing for some reason some of the fields are not set properly. Here 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 EAV table. However, Flat Table will be missing displayMode, landingPage, pageLayout, customLayoutUpdate, even if I re-index the flat table.

The strange thing is that if I go to the admin, I can see all these fields correctly, but if I go to my interface, most of these fields will be ignored. I need to go to the administrator, disable these values ​​and reset for each of them so that they work correctly.

, setEnabled (1), "" admin, .

PS: Flat Category, , , , .

+5
5

, , , , , , (storeId = 1), script script. storeId 0.

, :

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

. , Mage:: app() (Mage_Core_Model_App Line 804), IF, , script.

, script, :

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

script. , , , :

  • , script, . , script.
  • , isSecureArea ( , ).

script ( , ):

<?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();
?>
+9

try it

<?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 see Magento create a category

+1
source

I created several categories using the 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();
-1
source

All Articles