0

I am trying to create many sub-categories for one category in Magento CE 1.9 through an upgrade script.

Here is the upgrade script:

$category = Mage::getModel('catalog/category');

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

Mage::app()->setUpdateMode(false);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); //Make admin store

$category->setPath('1/2/23/')  //Creating as subcategory for category ID 23
    ->setName('English')
    ->setUrlKey('english')
    ->setLanguageCode('ENG')
    ->setIsActive(1)
    ->setIncludeInMenu(1)
    ->setDisplayMode(1)
    ->setPageLayout(0)
->save();

$category->setPath('1/2/23/') 
    ->setName('French')
    ->setUrlKey('french')
    ->setLanguageCode('FRE')
    ->setIsActive(1)
    ->setIncludeInMenu(1)
    ->setDisplayMode(1)
    ->setPageLayout(0)
->save();

$category->setPath('1/2/23/') 
    ->setName('Spanish')
    ->setUrlKey('spanish')
    ->setLanguageCode('SPA')
    ->setIsActive(1)
    ->setIncludeInMenu(1)
    ->setDisplayMode(1)
    ->setPageLayout(0)
->save();

Following this approach, I find that only the last subcategory given (Spanish) is created and all others are skipped. Is there any sort of reset that we must give for the catalog/category model object before creating each category? Where am I going wrong?

0

You are re-using the object. Which means that once the first category is created, each subsequent save is actually an update to the first created category.

Eg.

  1. Create "english" category
  2. Update "english" category to be "french"
  3. Update "french" category to be "spanish"

You can solve this by modifying your script like so:

$category = Mage::getModel('catalog/category');

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

Mage::app()->setUpdateMode(false);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); //Make admin store

$category->setPath('1/2/23/')  //Creating as subcategory for category ID 23
->setName('English')
->setUrlKey('english')
->setLanguageCode('ENG')
->setIsActive(1)
->setIncludeInMenu(1)
->setDisplayMode(1)
->setPageLayout(0)
->save();

$category = Mage::getModel('catalog/category');
$category->setPath('1/2/23/') 
->setName('French')
->setUrlKey('french')
->setLanguageCode('FRE')
->setIsActive(1)
->setIncludeInMenu(1)
->setDisplayMode(1)
->setPageLayout(0)
->save();

$category = Mage::getModel('catalog/category');
$category->setPath('1/2/23/') 
->setName('Spanish')
->setUrlKey('spanish')
->setLanguageCode('SPA')
->setIsActive(1)
->setIncludeInMenu(1)
->setDisplayMode(1)
->setPageLayout(0)
->save();

An alternative would probably have been to unset the entity_id after each save.

0

Here you start with one object and save it three times, it is still the same object and it is not going to leave a 'backup copy' of itself every time you change it.

Add $category = new $category; a couple of times in the right places.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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