Prototype – Cheat Sheet

I had to deal with Prototype while working with Magento. Here’s a little cheat sheet of some very simple stuff I had to do.

Make an Ajax Request with parameters

new Ajax.Request(url, {
                   params : { data1 : 'myParam' }
                   onSuccess: function(xhr){
                    var json = JSON.parse(transport.responseText);

Change value of an input

$('ID OF THE ELEMENT').setValue('The new value');

Change data within a span tag

$('ID OF THE ELEMENT').update('Your new data');


Magento – Custom routing

If you implemented several store views in your Magento shop (let’s say for French and English), if you implement a new router, he will be associated to this views.

Ex: In Magento’s default behavior, # KO # OK # OK

The solution to this problem is to implement custom routingInchoo wrote an awesome article about it.


PHP / Magento – Direct SQL Queries

Not the cleanest but here’s a quick solution to do a direct query on Magento. Might be faster than calling Magento’s objects ?

   * Get the resource model
$resource = Mage::getSingleton('core/resource');
  * Acccess to the database in read only
$readConnection = $resource->getConnection('core_read');
$query = 'SELECT * FROM ' . $resource->getTableName('catalog/product');
 * Execute the query  
$results = $readConnection->fetchAll($query);

Thank you DevProblems !

SQL Magento – Get store credit balance

Valid for Magento EE only. The feature of store credits isn’t native in CE edition.

select amount 
from enterprise_customerbalance 
where customer_id = @customer_id ;