Category Archives: Magento

Magento

How to enable remember me functionality in Magento

By default “Remember me” functionality is not enabled in magento.
To enable it, login to admin panel then

1
Go to System->Configuration->Customers->Persistent Shopping Cart->General Options->Enable Persistence

From here select “Yes” options from select box and click on “Save Config” button.
Now refresh login page and you will see “Remember Me” along with checkbox will appear on login page.
If not appear it on login page then clear your cache from Cache Management.

Thanks!

How to show a drop down attribute output on product detail page in Magento

If you want to show a drop-down attribute value on product view page or product listing page in Magneto.

So, please use following code given below.

Suppose, your attribute code is “size”

So the code should be :

1) ON PRODUCT PAGE:

edit catalog/product/view.phtml and add :

1
<?php echo $_product->getResource()->getAttribute('size')->getFrontend()->getValue($_product) ?>

2) ON LIST/GRID PAGE:

edit catalog/product/list.phtml and add :

1
<?php echo $_product->getResource()->getAttribute('size')->getFrontend()->getValue($_product) ?>

Customize Magento toolbar for list page

Some times we need to show the toolbar as per our need ,But magento had show same tool bar at the bottom and top of listing page.

so, in order to change the toolbar we can customize in such a way that top toolbar will be different and bottom toolbar will be different.

1) First open list.phtml,you will find getToolbarHtml() function at the top and at the bottom in order to change the bottom toolbar we just add the following code

Replace

1
<?php //echo $this->getToolbarHtml() ?>

With

1
<?php  echo $this->getToolbarBlock()->setTemplate('catalog/product/list/toolbar_top.phtml')->toHtml(); ?>

2) Second create the new file “toolbar_top.phtml” in catalog/product/list and add the customize code as per the need.

Hope this will help you and you will enjoy!

How to create user account programmatically in Magento?

Unfortunately, there is no any API to create user account in Magento, We can create a customer using Magento Class / API.
which is very useful when we want to register a certain customers that registering in a form that outside of our Magento site.

So, here’s the code to create a user account programmatically in Magento

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);

require_once 'app/Mage.php'; // please include full path of Mage.php

Mage::app('default');

$customer_email = 'test@magentotutorial.in';  // email address 
$customer_fname = 'firstname';      // first name  
$customer_lname = 'lastname';       // last name 
$passwordLength = 10;               // the lenght of autogenerated password
$passowrd       = '123456';         // you can assign password or auto generated password, you have two option

$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($customer_email);
/*
* Check if the email exist on the system.
* If exist, it will not create a user account. 
*/

if(!$customer->getId()) {

   //setting data as email, firstname, lastname, and password 

  $customer->setEmail($customer_email); 
  $customer->setFirstname($customer_fname);
  $customer->setLastname($customer_lname);
  //$customer->setPassword($customer->generatePassword($passwordLength)); // you can use autogenerated password
  //or
  $customer->setPassword($password); //or can set your own password as you wish

}
try{
  //the save the data and send the new account email.
  $customer->save();
  $customer->setConfirmation(null);
  $customer->save(); 
  $customer->sendNewAccountEmail();
}

catch(Exception $ex){
echo $message = $ex->getMessage(); 
}
?>

Hope, you will enjoy!.

Why Magento is slow?

There are following things which implemented in Magento cause to slow, which are as follows:

1
2
3
1. EAV structure of magento database, even for retrieving single entity the query becomes very complex .
2. Magento’s template system involves a lot of recursive rendering.
3. Huge XML trees built up for layout configuration, application configuration settings.

Hope! You will enjoy!.

How to improve magento performance?

There are lot of things we should keep in mind to improve performance or speed of Magento sites. Some of them are as follows:

1
2
3
4
5
6
7
8
1. Enabled Magento Caching.
2. MySQL Query Caching.
3. Enable Gzip Compression.
4. Disable any unused modules.
5. Disable the Magento logs.
6. Optimize images.
7. Combine external CSS/JS into one file.
8. Enable Apache KeepAlives: Make sure your Apache configuration has KeepAlives enabled.

Hope! You will enjoy!.

Get product collections by some range of ids in magento

1
2
3
4
5
6
7
8
9
10
11
<?php

$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('entity_id', array('from' => $startId,'to' => $endId))->addFieldToFilter('status', 1)->addAttributeToSelect(array('name','sku','entity_id','special_price','price','status','visibility','updated_at','created_at','popularity','actual_stock_available','setting_type','average_clarity','carat_weight'));

foreach ($collection as $product) 
{ 
    echo $i.') '.$product->getData('entity_id');echo"\n";
    $i++;
}

?>

Create custom pagination on custom module in Magento

Here, Some time we need custom pagination instead of Magento default pagination.

So, here is a snippet example code that would help you to create pagination with custom collection on custom module in Magento.

Step 1: First paste the following code in your block class (e.g. testimonial.php)

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php

    private $_itemPerPage = 2;
    private $_pageFrame = 8;
    private $_curPage = 1;
    
    public function getCollection($collection = 'null')
    {
        if($collection != 'null'){
            $page = $this->getRequest()->getParam('p');
            if($page) $this->_curPage = $page;
            
            $collection->setCurPage($this->_curPage);
            $collection->setPageSize($this->_itemPerPage);
            return $collection;
        }
    }
    
    public function getPagerHtml($collection = 'null')
    {    
        $html = false;
        if($collection == 'null') return;
        if($collection->count() > $this->_itemPerPage)
        {
            $curPage = $this->getRequest()->getParam('p');
            $pager = (int)($collection->count() / $this->_itemPerPage);
            $count = ($collection->count() % $this->_itemPerPage == 0) ? $pager : $pager + 1 ;
            $url = $this->getPagerUrl();
            $start = 1;
            $end = $this->_pageFrame;
            
            $html .= '<ol>';
            if(isset($curPage) && $curPage != 1){
                $start = $curPage - 1;                                        
                $end = $start + $this->_pageFrame;
            }else{
                $end = $start + $this->_pageFrame;
            }
            if($end > $count){
                $start = $count - ($this->_pageFrame-1);
            }else{
                $count = $end-1;
            }
            
            for($i = $start; $i<=$count; $i++)
            {
                if($i >= 1){
                    if($curPage){
                        $html .= ($curPage == $i) ? '<li class="current">'. $i .'</li>' : '<li><a href="'.$url.'&p='.$i.'">'. $i .'</a></li>';
                    }else{
                        $html .= ($i == 1) ? '<li class="current">'. $i .'</li>' : '<li><a href="'.$url.'&p='.$i.'">'. $i .'</a></li>';
                    }
                }
                
            }
            
            $html .= '</ol>';
        }
        
        return $html;
    }
    
    public function getPagerUrl()   // You need to change this function as per your url.
    {
        $cur_url = mage::helper('core/url')->getCurrentUrl();
        $new_url = preg_replace('/\&p=.*/', '', $cur_url);
        
        return $new_url;
    }

?>

Setp 2: Now you have to modify your frontend phtml page. (e.g. testimonial.phtml)

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
//pagination on top of contents
<?php if($this->getPagerHtml($this->getProductCollection())):?>        // pass the default collection as parameter
<div class="pages">
    <span><?php echo $this->__('Page : ');?></span>
    <?php echo $this->getPagerHtml($this->getProductCollection());?>
</div>
<?php endif;?>
 
<?php
    $_collections = $this->getProductCollection();            // Default Collection on which you'll implement pagination.
    $_productCollection = $this->getCollection($_collections);  // calling the function that have been created in block page.
    foreach ($_productCollection as $_product):

   ------------------ Your code here ------------

   endforeach;
?>    

//pagination on bottom of contents
<?php if($this->getPagerHtml($this->getProductCollection())):?>        // pass the default collection as parameter
<div class="pages">
    <span><?php echo $this->__('Page : ');?></span>
    <?php echo $this->getPagerHtml($this->getProductCollection());?>
</div>
<?php endif;?>

Hope! you will enjoy :-) !!!

Get all products by attribute set name or id in Magento

Here going to write a code snippet example, how you can fetch all products by it’s attribute set name or id :-

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
26
27
28
29
30
31
32
<?php 

//Fetch attribute set id by attribute set name
$attrSetName = 'your_attribute_set_name'; // your attribute name ex: diamond, watch etc
$attributeSetId = Mage::getModel('eav/entity_attribute_set')
    ->load($attrSetName, 'attribute_set_name')
    ->getAttributeSetId();
 
//Load all products filtered by attribute set id 
$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('name')
    ->addFieldToFilter('attribute_set_id', $attributeSetId);
                OR
//Load only enable product filtered by attribute set id 
$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('name')
    ->addFieldToFilter('status', 1)
    ->addFieldToFilter('attribute_set_id', $attributeSetId);
    
 
//process your product collection
$productsName = array();
foreach($products as $_product){
    $productsName[] = $_product->getData('name');
}

//return all products name with attribute set 'your_attribute_set_name'
print_r($productsName); 

?>

Hope! you will enjoy :-) !!!

Get Store and Website information in Magento

Many a time it requires a programmer to access the store information like store id or store status, store name, website id, website name etc.

These methods will help to fetch the details of the current store and website in Magento.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*Get Store*/
echo Mage::app()->getStore();

/*Store Id*/
echo Mage::app()->getStore()->getStoreId();

/*Store Name*/
echo Mage::app()->getStore()->getName();

/*Store code*/
echo Mage::app()->getStore()->getCode();

/*Store is Active*/
echo Mage::app()->getStore()->getIsActive();

/*Store Home Url*/
echo Mage::app()->getStore()->getHomeUrl();

/*Website Id*/
echo Mage::app()->getStore()->getWebsiteId();

/*Website Name*/
echo Mage::app()->getStore()->getWebsiteName();

Hope! it will help.