How to pull new product image in magento for hover image in product list page

Posted by Damodar Bashyal on May 28, 2014

 

Lets create new image attribute first at: magento admin > Catalog > Attributes > Manage Attributes > Add New Attribute

Set Attribute Properties as below:

Attribute Code *: hover_image
Scope: Store View
Catalog Input Type for Store Owner: Media Image
Apply To *: All Product Types
Manage Label 'admin': Hover image

Save this new attribute and go to manage attribute set at:

magento admin > Catalog > Attributes > Manage Attribute Sets. 

Click on your attribute e.g. Defaults. Then drag 'hover_image' from Unassigned Attributes to group Images and Save attribute set.

Now edit your product and assign image to hover image that you want to display on list page on mouse over.

Go to one of your extension and open config.xml file from etc folder and add hover_image on product collection.

<?xml version="1.0"?>
<config>
  <frontend>
      <product>
          <collection>
              <attributes>
                  <hover_image />
              </attributes>
          </collection>
      </product>
  </frontend>
</config>

Now go to your mysql and run:

mysql > select e.attribute_id,c.is_visible_on_front,c.is_visible,c.used_in_product_listing,e.attribute_code from catalog_eav_attribute c, eav_attribute e where e.attribute_id = c.attribute_id and e.frontend_input = 'media_image';

Check if hover_image has value 1 under column used_in_product_listing, if not update it with value 1.

Now go to your shell and run reindex as:

shell > php indexer.php reindexall

Now you have everything ready for backend. Now time for frontend edit.

First duplicate:

 skin/frontend/base/default/images/catalog/product/placeholder/image.jpg

and rename duplicate image to

skin/frontend/base/default/images/catalog/product/placeholder/hover_image.jpg

Open magento product list template file:

 app/design/frontend/base/default/template/catalog/product/list.phtml

in editor and find:

<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />

And replace it with:

<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" onmouseover="this.src='<?php echo $this->helper('catalog/image')->init($_product, 'hover_image')->resize(135) ?>';" onmouseout ="this.src='<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135) ?>';" />

All that you required to have a hover image in your list page. You can copy above code to other templates to have same effect.

zorro posted on - Monday 25th of August 2014 12:37:36 AM

Hello,

Need your help.

""Check if hover_image has value 1 under column used_in_product_listing, if not update it with value 1.""

In my case, the value =0, and i can't change it. i have always an error:
A fatal JavaScript error has occurred. Would you like to send an error report?

Do you have a solution ??

My theme is in localhost now.
Thanks.

Damu posted on - Monday 25th of August 2014 12:40:23 AM

It's hard to tell without looking at the code. I could have helped if I had access.

saurabh posted on - Wednesday 17th of September 2014 10:59:50 AM

this works fine but every time on hover same image ,i add extra image throw admin but not working..why?on every product has same image on hover

Skirek posted on - Monday 20th of October 2014 09:24:44 PM

Thanks! It works perfect!

Jehzeel Laurente posted on - Wednesday 3rd of September 2014 09:15:20 AM

Nice. I think this will resolve the errors I encountered after implementing pixafy.com's solution here:

http://www.pixafy.com/blog/2012/12/adding-a-fourth-image-position-to-the-admin-for-a-magento-product/

After I echoed $this->helper('catalog/image')->init($_product, 'attribute_code')->resize(400,300); in my product list page, I encountered multiple errors.

Will try your solution, I hope this works. :D

Sreedarsg posted on - Tuesday 11th of November 2014 01:38:13 AM

Works perfectly...Thank you so much

Shreshth posted on - Saturday 28th of May 2016 07:10:09 AM

Modified code according to custom theme. Still works like a charm. Simple and neat guide. Thanks!
 
not published on website


QR Code: How to pull new product image in magento for hover image in product list page