Create Magento categories tree programmatically

Data entry in Magento is a quite time consuming operation. Especially if you have a lot of environments and want to keep them all up to date. In this article we’ll create Magento categories tree programmatically with ability to update them without re-installing database.

First add a mysql upgrade script in your custom module:

Then form an array with your categories and sub-categories. Pay attention – Top Category Two has a child with a same name and slug as Top Category One:

Next let’s write a function which will create/update the categories:

This function automatically pick up all children categories and run itself to create them recursively. At the beginning of the function we check if desired category already exists in scope of current parent category. If it does – update category with new values, if not – create a new one.

Hint: if you need to make a slight changes in your category tree – just delete a row (or lower the version) in core_resource table to run script again.

And last but not least add logic to walk through the categories array:

Make sure you set current store to Mage_Core_Model_App::ADMIN_STORE_ID and update mode to false to avoid any errors. Then retrieve root category data and setup new categories under root. And finally return Magento to current store and set update mode back to finish upgrade.

For your convenience here is the full upgrade file listing:

If you have any specific settings for each category – move it from saveCategory() function to categories array. For example: And then:
Make sure to refresh cache and reindex Catalog URL Rewrites after this upgrade.

This is it folks, hope that help. If you find this article helpful (or vice versa) – don’t hesitate to rate it below and share to help others in their Magento journey.

If you have any suggestions – please leave a comment.


Rating:

Create Magento categories tree programmatically
4.7 (93.33%) 6 vote[s]

How to add a date/time product attribute to Magento admin

Adding a date attribute to Magento is no problem – but what about date + time? In this article I’ll explain how to add a date/time product attribute to Magento admin programmatically with upgrade script. Basically to make it happen you need to configure the renderer for date field and source model to convert the date and save time as well as date.

So let’s start with renderer itself:

app/code/local/Youcompany/Yourmodule/Block/Adminhtml/Renderer/Datetime.php

Next – let’s create a source model that will handle this attribute on save:

app/code/local/Youcompany/Yourmodule/Model/Attribute/Datetime.php

And finally the upgrade script to create an attribute and connect it to model and renderer:

app/code/local/Youcompany/Yourmodule/sql/yourmodule_setup/mysql4-upgrade-0.0.1-0.0.2.php

You can use any setup options for you attribute, but the most important part here is:

and That’s it folks. Hope that helps.

Rating:

How to add a date/time product attribute to Magento admin
4.8 (95%) 4 vote[s]

Create a Magento CMS Page or Static Block programmatically

Creating a CMS pages and blocks in Magento is simple as navigate to Admin Panel -> CMS -> Pages or Admin Panel -> CMS -> Static Blocks. But while you developing your extension you might need to create a Magento CMS Page or Static Block programmatically.

Here is the way how you can achive it:

Creating a CMS Page: Creating a CMS Block: That simple. Hope it helps.

Rating:

Create a Magento CMS Page or Static Block programmatically
4.5 (90%) 6 vote[s]

How to remove .html from Magento URL (+programmatically)

To improve the SEO of your store you might consider to remove .html from Magento URL on category and product pages.

There is a few easy ways to make it happen.

First way is to remove it through Magento admin panel. Navigate to System -> Configuration -> Catalog -> Search Engine Optimization and remove the “.html” value from Product URL Suffix and Category URL Suffix.

Make sure you refresh the cache and re-index “Catalog URL Rewrites”.

Second way is to built it in your custom extension. Here is sql update script:


Rating:

How to remove .html from Magento URL (+programmatically)
4.8 (95%) 4 vote[s]

Change default controller URL in Magento

Sometimes it’s required to have some areas of you site to leave in non-standard urls. For better SEO or just because you want so. In this case Magento has an ability to rewrite default controller url to whatever you want. For example you want to hide your admin panel from unwelcome visitors. This is the simple one – and described in magento wiki with ability to even set this up as separate node.

You just need to change this:

to this

in your app/etc/local.xml.

But what about fronrend URL’s? Exactly the same thing! For example lets change default URL for my account pages. Add this lines in your module etc/config.xml:

clean cache and now all your urls changed from http://wwww.example.com/customer/* to http://wwww.example.com/user/*.


Yes, that simple! Hope that helps.


Rating:

Change default controller URL in Magento
5 (100%) 1 vote[s]

← Older posts

Newer posts →