8 replies [Last post]
Francois's picture
Offline
Joined: 08/19/2008
Juice: 387
Was this information Helpful?

I've been going back and forth for a couple of hours now and I'm just not finding a solution anywhere. Having used snippets from here and Lullabot I've setup my module but this code only changes the "Add to cart" button for 1 specific product. I'd like to change it per product type. What do I need to change?

<?php
 
/**
   * Implementation of hook_form_alter().
   *
   * The function is named modulename_form_alter.
   */
 
function mymodule_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.
   
switch ($form_id) {
     
// This is our form ID.
     
case 'product_add_to_cart_form_39':
     
$form['submit']['#value'] = t('register');
      break;   
    }
  }
?>

The following did not work:

<?php
case 'product_node_form';
?>

Thanks for the help
Francois.

--------
'Twas the end of the world, and you didn't even know it.

Francois's picture
Offline
Joined: 08/19/2008
Juice: 387
Re: Add to Cart text per Product Class.

Can you globally change the ID for product_add_to_cart_form?

--------
'Twas the end of the world, and you didn't even know it.

jrowny's picture
Offline
Joined: 01/08/2009
Juice: 297
Re: Add to Cart text per Product Class.

A switch accepts a a string, in this case, $form_id. If you strip the last part of the form id, and then check for case 'product_add_to_cart_form_', then it will hit that case for every product. You can use substr to do that.

<?php
switch (substr($form_id,0,25)){
case
'product_add_to_cart_form_':
//do stuff
break;
}
?>
Francois's picture
Offline
Joined: 08/19/2008
Juice: 387
Re: Re: Add to Cart text per Product Class.

Aah, nice. I'll give it a try. Thanks.

There's just one thing, the product_add_to_cart is the same for all classes - it doesn't change to class_add_to_cart. How can I isolate a per class button as opposed to per node or all products?

Francois.

--------
'Twas the end of the world, and you didn't even know it.

jrowny's picture
Offline
Joined: 01/08/2009
Juice: 297
Re: Re: Re: Add to Cart text per Product Class.

I've never used product classes, but I assume it is stored somewhere in a variable you can access. Can you access $node in a hook function like that? I really have no idea... you could use node_load() and use subsr to get the node id but that might be slow. I'm actually pretty new to PHP/Drupal API.

Francois's picture
Offline
Joined: 08/19/2008
Juice: 387
Re: Re: Re: Re: Add to Cart text per Product Class.

That makes two of us. I'm going to keep on looking around, if I find a solution I'll post it here. It'll come in pretty useful for the project I'm on right now. Using product classes has also turned out to be a bright idea, it's made organising inventory information and presentation very easy.

--------
'Twas the end of the world, and you didn't even know it.

Francois's picture
Offline
Joined: 08/19/2008
Juice: 387
Advance

I've been able to make some headway, but I still don't know how to get the node type into my code.

<?php
 
/**
   * Implementation of hook_form_alter().
   *
   * The function is named modulename_form_alter.
   */
 
function uc_myform_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.
   
switch (substr($form_id,0,10)) {
     
// This is our form ID.
     
case 'uc_product':
     
$form['form_id']['#value'] = ???; // This line changes the value of the ID but I can't seem to get the node-type in here.
     
break;
    }
  }
?>

Any ideas?

--------
'Twas the end of the world, and you didn't even know it.

kirtimansharma's picture
Offline
Joined: 04/25/2010
Juice: 16
Re: Advance

I m doing a project that requires a similar functionality - different add to cart button text fro different product classes. Its been long time this thread had any comment. I m guessing you guys have reached a solution. can you share it here. I did not find any module till now to do the job. any news

aidan's picture
Offline
Joined: 09/06/2010
Juice: 18
Re: Add to Cart text per Product Class.

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