Reset Magento Admin Users password

Whenever you wanna change the Magento’s Admin password directly in the database or wanna cross check the encrypted password in the db with the plain text password, you need to generate the Salt password hash. In the ‘admin_user’ table of your Magento database, you can find all the admin users password. In those password fields, you can notice the last to characters added to the string after the ‘:'(semi-colon). That is the Salt for your installation.

The password may be seem something like this

4eb15ef0fe5b1a87d3326cd78b594ec7:K5

In the above case K5 is the salt. If the have the raw password and wanna generate the encrypted password, you can do the following

echo md5($salt.'yourpassword').$salt;

Where $salt is the last two characters of your encrypted password that you found in the previous step. Since md5 is used for encryption, there is no decryption exist.

Sort Products by review count in Magento

The product collection in magento can be sorted by the direct product attributes like name, price, sku, created time, etc., by the following way.

$productCollection->addAttributeToSort(‘name’, ‘desc’);

If you want to sort a product collection by the number of review the product has received, there raises the complexity.

Because, ‘reviews_count’ is a attribute available in RatingSummary class and it is not directly related to Product Collection. So to sort products by their reviews count. We need to traverse the collection and obtain the reviews_count attribute from the RatingSummary class and add it directly to the product collection. Following is the snippet for performing the above mentioned task.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$newarray = array();
foreach ($this->_productCollection as $product){
    $rating_summary = $product->getRatingSummary();
    if($rating_summary != null){
        $reviews_count = $rating_summary->getReviewsCount();
        $newarray[$product->getId()] = $reviews_count; 
    } else {
        $newarray[$product->getId()] = "0";
    }
}
if(is_array($newarray) && count($newarray) > 1){
    if ($sortDirection == 'asc') {
        arsort($newarray);
    } elseif ($sortDirection == 'desc') {
        asort($newarray);
    }
    $merged = $this->_productCollection; 
    foreach($newarray as $k => $v) {
        $item = $this->_productCollection->getItemById($k);
        if (!$merged->getItemById($item->getEntityId())) {
            $merged->addItem($item);
        }
    }
    $this->_productCollection = $merged;
}

here the ‘$sortDirection’ variable carries the value in which way you need the products to be sorted. And finally at the end of the code you will get the product collection sorted by its number of reviews for each product.

happy Coding!! :)

Changing favicon in Magento

A favicon (short for favorites icon), also known as a shortcut icon, Web site icon, URL icon, or bookmark icon, is a file containing one or more small icons, most commonly 16×16 pixels, associated with a particular Website. In Magento we have a beautifulfavicon, that represents Magento logo. If you want your own favicon to be displayed on your site, you need to change the default one with the one that represents your site logo.

Generally, all the sites and CMS have their favicon in the root directory of the site, in Magento, it has favicon in six other places to displayed the icon different for each section. Lets say, If you want a different icon to be displayed in your frontend user section and other in the admin section, you have provision for that in Magento. 

You need to replace the favicon.ico in the following locations.

/{root_directory}

/errors/default/images

/skin/adminhtml/default

/skin/install/default

/skin/frontend/default

/downloader

If you are not confident of changing the files, Magento has a backend interface throught which you can change the favicon for the frontend site.

Login to the admin section and navigate to 'system -> configuration -> General -> Design'. Select the third tab saying 'HTML Head'. In the first field you can browser your local system and upload the favicon file to be displayed on your site.

Thank you for reading this article.

Adding Breadcrump to cms pages in Magento

 

Magento is a eCommerce application and it supports CMS features but it has few limitation. Magento displays breadcrumps, which helps the user to know where they are and it displays the navigation path. But this breadcrump is available only in the catalog pages an not in the cms pages or in the checkout pages. 

'breadcrump.phtml' in 'app/design/default/yourtemplate/page' folder is responsible for displaying the breadcrump. Add the following code to the breadcrump.phtml file before the normal breadcrump check  to display the breadcrump in the cms, shopping cart and checkout pages.

if ((!$crumbs || !is_array($crumbs)) && $this->getUrl('') != $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true))) {
 
    $breadcrumb = $this->getLayout()->getBlock('breadcrumbs');
 
    $breadcrumb->addCrumb('home', array('label' => Mage::helper('cms')->__('Home'), 'title' => Mage::helper('cms')->__('Home Page'), 'link' => Mage::getBaseUrl()));
 
    $breadcrumb->addCrumb('my_account activeBreadcrumb', array('label' => $this->getLayout()->getBlock('head')->getTitle(), 'title' => $this->getLayout()->getBlock('head')->getTitle(), 'last' => 1));
 
    $crumbs = $breadcrumb->_crumbs;
 
}

The above code restricts the breadcrump to be displayed from the home page, if you want that to be shown also in the home page, then remove the following condition from the first line of the above code.

$this->getUrl('') != $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true))

This code check whether the current page is home page or not. Since Home is a CMS page in Magento, normally breadcrumps won't be displayed. 

Debug magento with Zend_debug::dump

dump() is a static method defined in debug class under Zend. This one of the more stuffs I love in Magento. Because it helps me debug any variable or objects in simple ways. You can echo what ever you want, lets say, a simple variable, an array, an object, any complex data type and so on…

Syntax:

Zend_Debug::dump($var, $label = null, $echo = true);

The $var argument specifies the expression or variable about which the Zend_Debug::dump() method outputs information.

The $label parameter prepends the string provided before the output. Mostly we don’t use this parameter, but you can see its use when you dump values of same variable at different lines of code, that time only this $label field will help you to understand the corresponding results.

The $echo parameter is to make dump() function not to print the values if set to false. Regardless of the value of the $echo argument, the return value of this method contains the output.

In core php, the dump method is equivalent

echo “<pre>”;
var_dump();
echo “</pre>”;

 

But this function returns more formatted and readable result. Instead of writing three lines of code every time for debugging, we can now use a single line of code.  From now start using Zend_debug::dump() and your love towards Magento/Zend will be doubled for sure.