1

I am using Magento CE 1.9.1.0. I can't seem to get the category URL key to save properly. If I add the category through the admin panel, everything works fine. But I am trying to add the categories programatically.

The problem is that the url_key that I am setting is being ignored, and a differnet url_key is being generated based on the name of the category.

$storeId = 3; // my store ID
$parentId = 18; // the root category ID
$category = Mage::getModel('catalog/category');
$category->setName('Contact Us');
$category->setIsActive(1);
$category->setDisplayMode('PRODUCTS');
$category->setIsAnchor(0);
$category->setStoreId($storeId);
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category->setPath($parentCategory->getPath());
$category->setUrlKey('customer-service-contact-us');
$category->setIncludeInMenu(false);
$category->save();
$savedCategoryId = $category->getId();
$category = Mage::getModel('catalog/category')->load($savedCategoryId);
var_dump($category->getUrlKey());
// I get 'contact-us' instead of 'customer-service-contact-us'     

I expected to get 'customer-service-contact-us' for the url key, but instead I'm getting the auto-generated url key 'contact-us'.

Any insight would be greatly appreciated. I've tried disabling all 3rd party modules, but the problem still happens.

1 Answer 1

2

The problem was the store ID.

Before:

$storeId = 3; // my store ID

After:

$storeId = 0; // my store ID

Even though 3 is the correct store ID, the category model won't save the custom url_key unless you set the store ID to 0 (all stores).

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.