How to Replace the 'Add to Cart' button with an Image in Ubercart

This is an easy-to-use Drupal module to replace Ubercart's "Add to Cart" submit button with a custom image based on this Ubercart support post.

Initially I had tried to do this with just CSS, but I could not make it work in IE.

A big shout out to Vince Rowe for providing a workable solution that we could turn into a module, and to Andy over at the Proof Group for helping me with this module.

How to use this module:

  1. Download uc_gfxbtn.zip.
  2. Next place your "Add to Cart" image in the uc_gfxbtn folder.
  3. Next, edit the uc_gfxbtn.module. Specify the image size and replace the "/add-to-cart.jpg" with the name of your image.
  4. To activate this module, simply drop it in your module directory.
  5. Scrolldown to the Ubercart -Extras section and enable Primal Media "add to cart" as image.

That's it. Alll your Ubercart "Add to Cart" buttons should now be replaced by the custom image you put in the uc_gfxbtn folder.

This Drupal / Ubercart module has been tested across platforms and the major browsers (Internet Explorer, Firefox, Safari). 

I hope this makes your Ubercart experience easier. I look forward to hearing any feedback or improvements on this module.

Comments

Got it working, Just had to set an absolute path to my image... Thanks Great module!
Great article and great module. Thanks a whole bunch
One question, Should the path to the image be /var/www/html/path/to/module/image or the way the web server sees it /path/to/module/image or just relative image
Ok, figured it out I just want to post it here in case people have the same question in the future. The image location works when you put the image in the "uc_gfxbtn" folder and then when your specifying where the image is just use the name of the file and append a / to the beginning like so: "/image.png". This seems to work no problems.
This wasn't working for me because in the .module file on line 27 where it had src="/"...that was making it not show up because my drupal installation was in a subdirectory of my domain. I just removed the '/' and it worked fine. You might want to change that so it's relative. Nice module, thanks!
Hi I am working on Drupal 6.8 and this module seems to be incompatible with it. Can you guide me the way I can replace the default "add to cart button" with the custom image? Thanks!!!
Great Article!
Hi. I cant figure it out how to set the path. I have tested several paths. path to mysite is like this: http://www.mysite.com/drupal So this should work ? src="/'. drupal_get_path('module', 'uc_gfxbtn') . '/buybtn.png">
Ok i figured it out. changed the path to src="'.base_path() . path_to_theme() . '/images/add_to_basket.jpg'. and it works fine.
Hi. Can you make this module for Drupal 6 please.
Works great, but... If you have a product with required attribute that gets added to cart without the attribute selected, the page reloads and show two buttons. One is the add to cart image button and the other is a submit button. I get that the submit button needs to be there to make the form work with the forms API but it needs to be hidden. This is very confusing and seems to be caused by this module.
If an unselected Attribute causes you to get a "Submit" button, simply hide it in your CSS. It's tested and works.
Thanks for the tip. I had the same problem on my lyric site which is also done in drupal. I am trying to add an ecommerce section so I can sell some music related products and am using Ubercart.
It's not compatible with 6.12. You wouldn't happen to have a newer version, would you?
Works like a charm on my homepage. Thanks... you saved me hours on this one.

I agree it would be great to see this for Drupal6. I tried to get it there. A couple of things I know about are:

1) "core = 6.x" needs to be added to uc_gfxbtn.info

2) $form['#base'] has been depreciated in D6. But this replacement should work:

Replace: if($form['#base'] == 'uc_product_add_to_cart_form'){

With: if( $form_id['#validate'][0] == 'uc_product_add_to_cart_form_validate' && $form_id['#submit'][0] == 'uc_product_add_to_cart_form_submit'){

3) But after these changes the module was still not working in D6. I was not able to get it past this. I suspect it has something to do with using $form[] instead of $form_id[]. I also don't know the variable $form['submit_image']. I typically have only seen $form['submit'] variables before. But obviously there is a lot I don't know. Just trying to give food for thought if anyone else wants to step up to the plate.

Thanks, maestrojed

Here is a fixed up version for D6:

function uc_gfxbtn_form_alter($form_id, &$form) {
if( $form_id['#validate'][0] == 'uc_product_add_to_cart_form_validate' && $form_id['#submit'][0] == 'uc_product_add_to_cart_form_submit'){

$form_id['submit']['#type'] = 'hidden';

// strip '?submit_x=15&submit_y=27' from $_POST
if (isset($_POST['submit_x'])) {
$form_id['submit'] = array(  
'#type' => 'submit',
'#value' => 'submit',
);
}

$form_id['submit_image'] = array(
'#value' => '<input name="submit" type="image" width="98" height="26" class="add_to_cart" title="'. t('Add to Shopping Cart') .'" src="/'. drupal_get_path('module', 'uc_gfxbtn') .
'/button.png">',
);
}
}
please make this module compatible with drupal 6 , unfortunately the code that posted broken egg not work :-(
Actually the code snippet broken egg provided works great!
broken egg's code worked great for me, I just had to add the code= 6.x in the .info file and then follow the read me.rtf directions. Works great! Thank you!
YES !! it works :D

here's what I did in the .info

;;$Id$ uc_gfxbtn.info,v 1.2 2009/01/27 14:03:28 longwave Exp $
name = Custom Image As Shopping Button
description = Allow administrators to change the display shopping cart button
dependencies[] = uc_store
dependancies[] = uc_product
dependancies[] = uc_cart
package = Ubercart - payment
core = 6.14

; Information added by drupal.org packaging script on 2009-07-30
version = "6.x-1.4"
core = "6.x"
project = "uc_gfxbtn.info"
datestamp = "1248955928"



and for the .module i used broken egg's code :



function uc_gfxbtn_form_alter($form_id, &$form) {
if( $form_id['#validate'][0] == 'uc_product_add_to_cart_form_validate' && $form_id['#submit'][0] == 'uc_product_add_to_cart_form_submit'){

$form_id['submit']['#type'] = 'hidden';

// strip '?submit_x=15&submit_y=27' from $_POST
if (isset($_POST['submit_x'])) {
$form_id['submit'] = array(  
'#type' => 'submit',
'#value' => 'submit',
);
}

$form_id['submit_image'] = array(
'#value' => '<input name="submit" type="image" width="98" height="26" class="add_to_cart" title="'. t('Add to Shopping Cart') .'" src="/'. drupal_get_path('module', 'uc_gfxbtn') .
'/button.png">',
);
}
}
This is great, but why are the hook_form_alter arguments in that order? In Drupal 6 they should be: function example_form_alter(&$form, $form_state, $form_id) ... http://api.drupal.org/api/function/hook_form_alter/6 I'm confused, mostly because the $form_id should be a string, not an array. Also, note that the first argument is passed by reference. In my case, I'm trying to replace the Add to Cart button with text or a link if the user either has the product (course enrollment) or is not logged in.
This method works for me in product pages, but not for views. I have a view on the front page that displays the products, but I can't for the life of me figure out how to get the graphic button to show up on catalog pages or in views. Any ideas?
I also have same problem as Ian. Has anybody found a solution yet?
I hope I fixed problem regarding catalog pages using views: I added extra checking: if (($form_id == 'uc_product_add_to_cart_form_'.$form_nid) OR ($form_id == 'uc_catalog_buy_it_now_form_'.$form_nid))
I added this extra condition: if (($form_id == 'uc_product_add_to_cart_form_'.$form_nid) OR ($form_id == 'uc_catalog_buy_it_now_form_'.$form_nid)) }
Hi, thanks a lot for this module. I am wondering if anyone has had trouble getting it to work with a 'product kit' in ubercart. The module works well for me with 'product' nodes, but for 'product kit' it is not showing up. Thanks a lot!

Add a comment

This field will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <em> <strong><ul> <ol> <li>
blog