Programmatically create site categories using an install script
-
12-12-2019 - |
Question
Using Magento 1.9.0.1 I am trying to script my entire sites config and settings using install scripts so I can keep the whole site under version control.
I am now at the point of creating my categories.
I have seen many ways of doing this however nothing really non 'hacky'. Taking the following example category structure and keeping in mind I will have a maximum of only 20-30 categories what is the recommended way of approaching this? I would also like the script to Update if the category already exists.
- Public Catalog
- Mens
- Tops
- Bottoms
- Womens
- Dresses
- Shoes
I also want to be able to insert a category thumbnail and image which I have not found an example of.
Thanks
Solution
I think a cool way of doing this is using data-install-scripts. data-install scripts are like sql-install-scripts but they are run after environnement initailization so you can use Magento models for creating your categories like you would do in any standalone script.
You can place you data-install-script is a folder (almost like sql-scripts):
app/code/CODEPOOL/Namespace/Yourmodule/data/yourmodule_setup/data-install-0.0.1.php
app/code/CODEPOOL/Namespace/Yourmodule/data/yourmodule_setup/data-upgrade-0.0.1-0.0.2.php
If you have a multi-store-view shop, you should be sure your store configuration will be setup (maybe you place it also into setup scripts ?)
http://inchoo.net/magento/using-magento-1-6-data-install-scripts/