Magento redirect route in Custom Module and Observer

I was asked to redirect all routes(except admin) in Magento 1.9.2 to a custom module‘s frontend.  With Observer pattern, it is very easy to achieve it.

In Magento, when the front page layout beginning to display, an event “controller_front_send_response_before” will occur, and we will subscribe to this event to trigger our function for redirection.

To subscribe to this event, add the following xml tags in etc/config.xml in our custom module directory in the global tag.

This is what I guess the meaning of this xml tag, but I may be wrong: Magento will use singleton method, get model observer, and call customRedirect() function.  (e.g. Mage::getSingleton(‘custom_module/observer’)->customRedirect() )

Since we will need a new model “Observer”, in our custom module directory, under Model folder, create a file call “Observer.php”.

In customeRedirect() function, my code will check if this is admin path, or custommodule path.  If pattern does not match, it will redirect to custommodule route.

if you want to redirect to HTTPS route, you will need to provide the second parameter in setRedirect function like this:

setRedirect(Mage::getUrl(“customemodule/index/index”, array(‘_forced_secure’ => true);

 

 

Read More

Magento 1.9.2 Custom order amount in dashboard

A module I am building which will create a custom order, and everything is ok until I found that the Grand Total was not showing up Last 5 orders in Dashboard.

dashboard

Google again, and I found that there is a property in Order Model must be set in sales_flat_order table. base_to_global_rate must set to 1.0 (I am not sure what the value meant, but 1.0 is the value that when place order normally)

Also, another problem is create custom order and I called setShippingMethod to ‘flatrate_flatrate’, in Order View it did not show up flatrate.

flatrate

Google around again, and I found that I must set Shipping Description in the order, and now it works!

flatrate2

I have embed the Snippet from my code in Gist.

Read More

Magento Custom Module EAV Attribute Table Unique Key

I created a Magento 1.9.2 module with EAV Table for importing custom data, but when I tried to import second time and update the existing data, it created new entries in Attribute table instead of updating it. I searched for awhile finally figure out that the Setup script won’t create unique key (entity_id, entity_type_id, store_id, attribute_id) for my attributes table.

Although I can just use command to add the unique key, I want to do it inside the setup script. End up it is straight forward.

 

Read More

Creating Magento Custom Module with Entity table

I was writing a Magento module and trying to create a custom entity table for my Model.  However, every time I run the installation script, it just create table with name t_xxxxxxx  (x is random number) and the associate attribute tables.  Took me so long to figure out that my config.xml was wrong with this format:

<entities>
<foo>
<table>
foo_entity
</table>
</foo>
</entities>

Turn out that is not correct. I need to put the table name right after the <table>


<entities>
<foo>
<table>foo_entity</table>
</foo>
</entities>

Then it create a table with name ‘foo_entity’

FML for spending so much time to figure this out.

Read More