Magento

Useful Magento Extensions

Posted on Updated on

  1. Enhanced Admin Grids (+ Editor)
    Allow to customize a vast majority of the administration grids and add to it some useful features + an editor
  2. Magento Debug
    Debug toolbar for Magento
  3. Featured products
    Display list of featured products
  4. Pulse Storm Launcher
    The Pulse Storm Launcher is a fast, free, and fantastic productivity extension for the Magento admin console
  5. Online Magento Module Creator
    Magento Module Creator is a magento online module tool
Advertisements

Magento: Get attribute Group Name of a product attribute

Posted on Updated on

Hello,
The code below will get you the  attribute group name of a product attribute.
For eg. if you want to get the attribute group name “General” of a product attribute say “description”, the code below will do the job for you.

$resource = Mage::getSingleton('core/resource');
/**
* Retrieve the read connection
*/
$readConnection = $resource->getConnection('core_read');
$product = Mage::getModel('catalog/product')->load(15);// replace 15 with the required product Id
$attributeId = 117; // id of the product attribute
$attributeSetId = $product->getAttributeSetId();
$query = 'SELECT * FROM `' . $resource->getTableName('eav/entity_attribute')
.'` AS main_table JOIN `'
.$resource->getTableName('eav/attribute_group')
.'` AS group_table ON main_table.attribute_group_id = group_table.attribute_group_id WHERE attribute_id = "' 
. $attributeId
.'" and main_table.attribute_set_id = "'
. $attributeSetId . '"';
/**
* Execute the query and store the results in $results
*/
$results = $readConnection->fetchAll($query);

if(count($results)){
// gives you the name of the required group name
$attributeGroupName = $results[0]['attribute_group_name'];
echo $attributeGroupName;
}

Thank you for reading.
Hope this was helpful to you.
Cheers!!

Magento : Redirect to a url from Event Observer Model

Posted on Updated on

//get the required url

$url = Mage::getUrl('checkout/cart');//eg to redirect to cart page

//get the http response object

$response = Mage::app()->getFrontController()->getResponse();

//set the redirect header to the response object

$response->setRedirect($url);

//send the response immediately and exit since there is nothing more to do with the current execution 🙂

$response->sendResponse();
exit;

Thank you for reading.
Hope this was helpful to you.
Cheers!!

Magento : Paypal Russian Ruble(RUB) and Philippine Peso(PHP) currencies support patch

Posted on Updated on

Paypal has recently added support for Russian Ruble a month ago it seems.
http://rt.com/business/paypal-rubles-ebay-russia-965/

Magento’s paypal module is unaware of it and hence paypal payment method doesn’t appear if we have RUble as our store’s base currency. So in order to add the support for the new currencies, we need to add the currency to the list of supported currencies.

Here is what we need to do:

Open up the file app/code/core/Mage/Paypal/Model/Config.php
Replace line 207
protected $_supportedCurrencyCodes = array(’AUD’, ‘CAD’, ‘CZK’, ‘DKK’, ‘EUR’, ‘HKD’, ‘HUF’, ‘ILS’, ‘JPY’, ‘MXN’,
‘NOK’, ‘NZD’, ‘PLN’, ‘GBP’, ‘SGD’, ‘SEK’, ‘CHF’, ‘USD’, ‘TWD’, ‘THB’);

with

protected $_supportedCurrencyCodes = array(’AUD’, ‘CAD’, ‘CZK’, ‘DKK’, ‘EUR’, ‘HKD’, ‘HUF’, ‘ILS’, ‘JPY’, ‘MXN’,
‘NOK’, ‘NZD’, ‘PLN’, ‘GBP’, ‘RUB’, ‘SGD’, ‘SEK’, ‘CHF’, ‘USD’, ‘TWD’, ‘THB’);

For maintaining Magento standards, please create your own module and override the paypal’s config model wink

I have made a patch for this. you can download it from github
https://github.com/shakyaabiral/paypal_currency_patch

Thank you for reading.
Hope this was helpful to you.
Cheers!!

Sql query to search for a value in csv field

Posted on Updated on

Sql query to search for a value in csv field
SELECT * FROM `TABLE_NAME` WHERE `FIELD_NAME` REGEXP ‘(\,VALUE\,)|(^VALUE\,)|(\,VALUE$)’

Magento Version
$collection->addAttributeToFilter(‘FIELD_NAME’, array(‘regexp’=> ”(\,VALUE\,)|(^VALUE\,)|(\,VALUE$)’));

Example:

Table ‘test’
———————————
id |value                        |
1  |1,2,3,4,5,6                |
2  |2,4,11,12                  |
———————————-

Search for a row which has a value of 1 in the csv in value column

SQL Query
SELECT * FROM ‘test’ WHERE `value` REGEXP ‘(\,1\,)|(^1\,)|(\,1$)’

Create a custom router in Magento using Event/Observer

Posted on

Have you ever wanted to create a seo-friendly url management feature for a custom module like News module or blog Module in Magento.
Here we are going to create a custom router as done by the CMS module to route a url to a specific page created in admin.

A little background into Magento Routers.
Magento by default has 4 routers which are Admin, Standard, Cms and Default respectively.
In short, during a page load, Magento loads the Mage_Core_Controller_Varien_Action class. This class is responsible for finding the routers that matches with the current request url and identify the controller and action to be called.

While loading the routers, “controller_front_init_routers” event is fired.

We can use this event to add our custom router feature into Magento.

So, Here we go.

Step 1.
Setup an observer in config.xml file of your module that listens to the “controller_front_init_routers” event.
<config>
<global>
<events>
<controller_front_init_routers>
<observers>
<magentotraining_router>
<class>Companyname_ModuleName_Controller_Router</class>
<method>initControllerRouters</method>
</magentotraining_router>
</observers>
</controller_front_init_routers>
</events>
</global>
</config>

Step 2.
Create a event listener class that actually does the job of adding a new router and also matches the url request to the required content in our module. For ease of coding, we will be using the cms class “Mage_Cms_Controller_Router” as the Magento’s core CMS module uses the same appraoch. We will make the changes as required.
So create a new file Router.php at app/code/codePool/CompanyName/ModuleName/Controller/ which is an exact copy of “app/code/core/Mage/Cms/Controller/Router.php”
and put the following code.

Read the comment in the code to make the necessary changes.

class CompanyName_ModuleName_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract
{
/**
* Initialize Controller Router
*
* @param Varien_Event_Observer $observer
*/
public function initControllerRouters($observer)
{
/* @var $front Mage_Core_Controller_Varien_Front */
$front = $observer->getEvent()->getFront();

$front->addRouter(‘router_name’, $this);//replace router_name with a suitable name
}

/**
* Validate and Match router with the Page and modify request
*
* @param Zend_Controller_Request_Http $request
* @return bool
*/
public function match(Zend_Controller_Request_Http $request)
{
if (!Mage::isInstalled()) {
Mage::app()->getFrontController()->getResponse()
->setRedirect(Mage::getUrl(‘install’))
->sendResponse();
exit;
}

$identifier = trim($request->getPathInfo(), ‘/’);

$condition = new Varien_Object(array(
‘identifier’ => $identifier,
‘continue’ => true
));
Mage::dispatchEvent(‘magentotraining_controller_router_match_before’, array(
‘router’ => $this,
‘condition’ => $condition
));

$identifier = $condition->getIdentifier();

if ($condition->getRedirectUrl()) {
Mage::app()->getFrontController()->getResponse()
->setRedirect($condition->getRedirectUrl())
->sendResponse();
$request->setDispatched(true);
return true;
}

if (!$condition->getContinue()) {
return false;
}

/** this is the actual implementation in CMS module to find the page ID from the request url, we can write something similar to **get the id of our item from the requested URL.
* for eg. we could have url = ‘about-us’ for id 1.
* So if the current url is http://site_url/about-us
* we will get 1 as pageId by quering through our module.
*/

// $page = Mage::getModel(‘cms/page’);
// $pageId = $page->checkIdentifier($identifier, Mage::app()->getStore()->getId());
// if (!$pageId) {
// return false;
// }

$pageId = 1; //1 is retrieved by quering our custom module table.
$request->setModuleName(‘modulename’)// replace modulename with the frontname of the router of your controller
->setControllerName(‘index’)// replace index with your controller name
->setActionName(‘index’) // replace index with your action name
->setParam(‘page_id’, $pageId); //$pageid is the id param that is passed into the action to retrieve the desired item

$request->setAlias(
Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
$identifier
);

return true;
}
}

And we’re done.

Now when Magento processes the current request it also passes through our module to check if it finds a match and set the controller and action of our module for processing the request.
So, now we have a clean and elegant url for our item pages.
http://magento.com/test-content.html
instead of
http://magento.com/testmodule/testcontroller/testaction/view/1
Looks good 😀

For more detail and intensive study on Magento routers and front Action Processing, you can have a look at the following posts:
http://blog.belvg.com/magento-certified-developer-exam-request-routing.html
http://blog.belvg.com/magento-front-controller-pattern.html

Thank you for reading.
Hope this was helpful to you.
Cheers!!

Log all Sql Queries in Magento

Posted on Updated on

Have you ever wondered what sql queries are executed when we open a new page in magento or when a product is added to cart or an order is placed?

Well, Magento has a very useful profiler tool that lets us view all the queries that are run during a page request.

One way to see all the queries in the browser itself when we run a code is :

Activate the Zend SQL Profiler with the following node in your app/etc/local.xml

<resources>
 <default_setup>
  <connection>
   <profiler>1</profiler>

Then you can access the profiler somewhere in your code and retrieve a lot of informations about all executed queries:

$profiler = Mage::getSingleton('core/resource')->getConnection('core_write')->getProfiler();

To simply output all queries:

echo("<pre>");
print_r($profiler->getQueryProfiles());
echo("</pre>");

This code will echo all the queries that are executed before this code is executed. So you might wanna place this code at the end of index.php file at the root for quick viewing. but this may break the ajax calls. So beware and use it as you feel right.

We can also log all the queries in a text file.
For that we can follow the following process.


In Varien_Db_Adapter_Pdo_Mysql

/lib/varien/Db/Adapter/Pdo/Mysql.php

set

protected $_debug               = true;

protected $_logAllQueries       = true;

and

protected $_debugFile           = ‘var/debug/pdo_mysql.log; // this is the file where the logs are stored. change the filename to your need. Also give read / write permission to the file

And its done. You can now view all the sql queries run. Beware Magento run loads and loads of query during a request. So you might have to forge your way through the log file to find the query you want 😀

Thank you for reading.
Hope this was helpful to you.
Cheers!!