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?

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);
  • This doesn't seem to be working in all cases. Is there something else that could be a factor? – Steve Robbins Aug 20 '13 at 0:53
  • describe the cases where it is not working then – Anton S Aug 20 '13 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 Apr 4 '17 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 May 12 '17 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 May 12 '17 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');
  • 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. – Steve Robbins May 4 '14 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) – Sveta Oksen May 4 '14 at 17:08

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.