2

I have a script that looks something like

$parent = Mage::getModel("catalog/category")->load(50);
$parent->setCustomUseParentSettings(false);
$parent->setCustomApplyToProducts(false);
$parent->setPageLayout("two_columns_left");
$parent->save();

And when I look at the category in Manage Categories of the Admin, I can see that the values updated correctly, but when I look on the frontend, I see it's still using a different layout.

All I do then is click "Save" without modifying anything, and then it renders correctly on the front page.

I've tried reindexing Category Flat tables, but this is the only way I've been able to fix this. There's ~2000 categories so saving them all isn't a solution. How can I do this programmatically? Why doesn't it work in the first place?

2 Answers 2

11

there is a trick involved as Magento only allows category save on EAV structure and it differences this (if you ask for category model) in code by checking admin store scope so you have to fake the scope to be able to save the categories if you are doing this outside of admin

$currentStore = Mage::app()->getStore()->getId();
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
$parent = Mage::getModel("catalog/category")->load(50);
$parent->setCustomUseParentSettings(false);
$parent->setCustomApplyToProducts(false);
$parent->setPageLayout("two_columns_left");
$parent->save();
// set the store scope back
Mage::app()->getStore()->setId($currentStore);
6
  • This doesn't seem to be working in all cases. Is there something else that could be a factor? Commented Aug 20, 2013 at 0:53
  • describe the cases where it is not working then
    – Anton S
    Commented Aug 20, 2013 at 19:31
  • My case was a shell script, and extending Mage_Shell_Abstract set the admin store id properly, so this doesn't fix my issue. Back to searching.
    – Domino
    Commented Apr 4, 2017 at 20:49
  • I encounter exactly the same problem with category update script that i run from shell script. I tried both propositions from Anton S and Sveta Oksen without success. The config category param is visible in backend after update and not visible in frontend (even after reindex). @Jacque Goupil: did you find a way to fix it?
    – DarkCowboy
    Commented May 12, 2017 at 6:41
  • @DarkCowboy I don't work where I used to so I can't look at the code anymore, but the issue was caused by the fact that some properties were set to NULL after importation with MAGMI, and some of Magento's code didn't work properly as a result. Saving the results in the admin panel would store a 0 in those properties, making it behave properly.
    – Domino
    Commented May 12, 2017 at 21:18
1

Try the following:

$categorySingleton = Mage::getSingleton('catalog/category');
$categorySingleton->setId(4); // desired category id to update
$categorySingleton->setCustomUseParentSettings(false);
$categorySingleton->setCustomApplyToProducts(false);
$categorySingleton->setPageLayout("two_columns_left");

/**
* IMPORTANT: set the desired store view id in setStoreId, or use setStoreId(0) for Default (all store views)
*/
$categorySingleton->setStoreId(0);

Mage::getModel('catalog/category')->getResource()->saveAttribute($categorySingleton, 'custom_use_parent_settings');
Mage::getModel('catalog/category')->getResource()->saveAttribute($categorySingleton, 'set_custom_apply_to_products');
Mage::getModel('catalog/category')->getResource()->saveAttribute($categorySingleton, 'page_layout');
2
  • Not seeing a category id being specified. Or is this supposed to update all categories? Thanks for the additional examples by the way. This is much more efficient than loading an saving an entire category. Commented May 4, 2014 at 0:58
  • Thank you for the comment. You are right, I forgot to add $categorySingleton->setId(4); (4 is just an example of the ID of the category that you'd like to update) Commented May 4, 2014 at 17:08

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.