13 replies [Last post]
drupalmind's picture
Offline
Joined: 01/06/2009
Juice: 46
Was this information Helpful?

i have two classes of product. i want to use "add to cart" button on real product contents. and wants to use "register" text on other content and "submit" button for third content. but control will go to cart page in all case (as usual).

benko's picture
Offline
Joined: 01/14/2009
Juice: 66
Hi drupalmind, I actually

Hi drupalmind,

I actually needed some similar functionality for my site. I found the option to change ALL the 'Add to Cart' buttons but obviously this wasn't good enough.

Here is how I tackled the problem (as Im fairly new to drupal and dont know how to create modules yet)

- Created a node-product.tpl.php file in my template directory to override the default behavior for product nodes.
- In this file checked to see what class of product we were looking at
- After determining what class of product it was I used a case statement/if else to change the value of the 'Add to cart' form using string replace. Something like below:

if ($class == 'your_class')
{
    echo str_replace('Add to Cart', 'Register', $node->content['add_to_cart']["#value"]);
}
else
{
    echo $node->content['add_to_cart']["#value"];
}

It isn't very elegant but it works until my skills improve enough to figure out how to write a module for this Smiling

Hope this helps

drupalmind's picture
Offline
Joined: 01/06/2009
Juice: 46
Re: Hi drupalmind, I actually

It is overriding all the content except the "add to cart" button text.
when i use the code all the text disappear and button show up with text "add to cart" but there would be "register"

splash112@drupal.org's picture
Offline
Joined: 04/01/2008
Juice: 413
Re: Re: Hi drupalmind, I actually

So you just would want to override the "add to cart" text on the button on node pages?

You could try something like:

<?php
function my_module_form_alter($form_id, &$form) {
 
// Normally a switch is used because you may want to alter more than
  // one form and it is easy to add a new case for each form.
  // $output .= dsm($form);
 
switch ($form_id) {
   
// This is our form ID.
   
case 'product_node_form':
     
$form['submit']['#title'] = t('register');
      break;
    case
'other_node_form':
     
$form['submit']['#title'] = t('other');
      break;
  }
}
?>

Use a hook_form_alter to change forms that already exist. This should be in a new module.
Install the devel module and uncomment the "$output .= dsm($form);" part and see which exact name the $form['submit']['#title'] has and so how to change it.

Good luck!

drupalmind's picture
Offline
Joined: 01/06/2009
Juice: 46
Re: Re: Re: Hi drupalmind, I actually

I have made a module to alter the form but i have not got the solution may be i have not got the $node->type or some thing else problem. or i have not got $form['submit']['#title'] it correctly, any how i have tried a couple of methods but problem is still there.

splash112@drupal.org's picture
Offline
Joined: 04/01/2008
Juice: 413
Did you install and enable

Did you install and enable the Devel module?

And ucomment:

<?php
$output
.= dsm($form);
?>

The title for the submit button should be there, dito the form ID stuff. I didn't work with classes before, so I don't know if the form_id will change. You could use node->type or something instead.

drupalmind's picture
Offline
Joined: 01/06/2009
Juice: 46
Re: Did you install and enable

work on the hook alter of uc_product module have worked for me.
i m also pasting a code snap that works for me

$content_type = $form['#parameters'][2]->type;
$uc_form_id = $form_id;
$uc_form_id = explode("_",$uc_form_id);
array_pop($uc_form_id);
$uc_form_id = implode("_", $uc_form_id);
if(($uc_form_id == "uc_product_add_to_cart_form") and ($content_type=="committee_events")){
    $form['submit']['#value'] = t('Register');
}
else if(($uc_form_id == "uc_product_add_to_cart_form") and ($content_type=="product")){
    $form['submit']['#value'] = t('Add to cart');
}

Thx for the contribution of all participants here!

fossle's picture
Offline
Joined: 01/20/2009
Juice: 77
Re: Re: Did you install and enable

I need to do something similar - where does this code go?

Thank you,
Kim

drupalmind's picture
Offline
Joined: 01/06/2009
Juice: 46
Re: different text to "add to carts" button for each class of p

in form alter of uc_product page.

thisidea's picture
Offline
Joined: 05/12/2009
Juice: 2
Re: different text to "add to carts" button for each class of p

Thanks in advance!

ckidow's picture
Offline
Joined: 10/27/2008
Juice: 46
Re: different text to "add to carts" button for each class of p

Nice snippet! Thanks a lot!

wendy's picture
Offline
Joined: 10/14/2009
Juice: 37
Re: different text to "add to carts" button for each class of p

I just want to add that the code given in #3 works great, except that I had to change 'title' to 'value' for it to work.

Thanks a lot for this, it has saved me some time!

SchwebDesign's picture
Offline
Joined: 09/30/2009
Juice: 46
didn't work for me but THANKS...

Thanks for this discussion. it helped me a log... but i had to make some changes for this to work for me. Below is my module code:

function other_add_to_cart_text_form_alter(&$form, &$form_state, $form_id) {
// Normally a switch is used because you may want to alter more than
// one form and it is easy to add a new case for each form.
// $output .= dsm($form);
// $output .= dsm($form_id);

switch ($form_id) {
// This is our form ID.
case 'uc_product_add_to_cart_form_23':
$form['submit']['#value'] = t('Continue to enter custom info');
break;
}
}

-Cameron N. Hess
Web Designer and Developer
Schweb Design, LLC
www.schwebdesign.com

aidan's picture
Offline
Joined: 09/06/2010
Juice: 18
Re: different text to "add to carts" button for each class of p

I've written a module to do this; http://drupal.org/project/uc_product_cta