phpmagentocategoriesmagento-1.8

Creating categories programmatically


Trying to set categories with update script. First I delete category, that Magento creating by default, then create categories with recursion. Also I got settings in modules/xml:

<depends>
     <Mage_Catalog />
</depends>

I have my upgrade script here:

/* @var $installer Mage_Eav_Model_Entity_Setup */
$installer->startSetup();

Mage::register('isSecureArea', true);
Mage::getModel('catalog/category')->load(2)->delete();
Mage::unregister('isSecureArea');

$categories = array(
    array(
        'name' => 'Brand Category',
        'children' => array(
            array(
                'name' => 'Carrier Bags',
                'children' => array(
                    array(
                        'name' => 'Retail Bags',
                    ),
                    array(
                        'name' => 'Outlet Bags',
                    ),
                ),
            ),
            array(
                'name' => 'Giftpackaging',
            ),
        ),
    ),
    array(
        'name' => 'Default Category',
        'children' => array(
            array(
                'name' => 'Draagtassen',
                'children' => array(
                    array(
                        'name' => 'Papier',
                        'children' => array(
                            array(
                                'name' => 'Luxe papier',
                            ),
                            array(
                                'name' => 'Kraft papier',
                            ),
                        ),
                    ),
                ),
            ),
            array(
                'name' => 'Geschenkverpakkingen',
                'children' => array(
                    array(
                        'name' => 'Geschenkdozen',
                    ),
                    array(
                        'name' => 'Cadeauzakken',
                        ),
                    array(
                        'name' => 'Cadeau-/Inpakpapier',
                        ),
                ),
            ),
        ),
    ),
);

function createCat(array $data, $path = '1') {
    $data = array_merge(
        array(
            'is_active'                     => 1,
            'is_anchor'                     => 0,
            'display_mode'                  => 'PRODUCTS',
            'custom_use_parent_settings'    => 0,
            'path'                          => $path,
        ),
        $data
    );

    $children = array();
    if (isset($data['children'])) {
        $children = $data['children'];
        unset($data['children']);
    }

    $category = Mage::getModel('catalog/category')->setData($data)->save();

    if (!empty($children)) {
        foreach ($children as $child) {
            createCat($child, $category->getPath());
        }
    }
}

foreach ($categories as $cat) {
    createCat($cat);
}

$this->endSetup();

What I have in the end it's:

Default Category with children, that's good and then Default Category once more. But no Brand Category. Thanks.


Solution

  • Script must be in data folder, not in mysql4.