i try to modify the add to cart button text in commerce so i can have diffrent text in product list and diffrent text in product page.

Is it possible to do this with form_alter?

I tried to use something like node type but no success.

function custom_addtocart_form_alter(&$form, &$form_state, $form_id) {

    if ((substr($form_id, 0, 30) == 'commerce_cart_add_to_cart_form') && $node->type == 'product_display') {
    $form['submit']['#value'] = t('Buy');



If you try to change text of 'Add to cart' button, you should check add to cart form with it's number.

So apply code like:

if(strpos($form_id, 'commerce_cart_add_to_cart_form_') === 0 && $node->type == 'product_display'){
   $form['submit']['#value'] = t('Buy');


  • Hello again! I have to notice that the text switches back to default when i hit the button and AJAX reloads form. I tried with form_commerce_cart_add_to_cart_form_alter as Scott suggested but the same thing happens. – ghapsis Jun 2 '15 at 8:36

You should be able to use $node->type if you add this first

$node = menu_get_object();

Remember to check if $node is NULL and $node->type is set.

Also if you're wanting to use the "add to cart" hook_FORM_ID_alter() then the function name needs to be something like

  • Thank you Scott! I missed that. But does not work as i thought... it changes the text in product list also. I think i have to find another way to distinct the 2 cases and not by content type. Thanks again! – ghapsis Jun 1 '15 at 10:58
  • Maybe you could try distinguishing via one of the properties in $form_state['context'] e.g. i.imgur.com/ipy7YvN.png – Scott Anderson Jun 1 '15 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.