I would like to show a custom text on the Add to cart form submit button.

Here is documentation https://drupalcommerce.org/discussions/613/how-change-add-cart-button-bu...
How to change the submit button text based on product type:

<?php
function mymodule_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state) {
  $line_item = $form_state['line_item'];
  $product = commerce_product_load($line_item->commerce_product[LANGUAGE_NONE][0]['product_id']);
  if ($product->type == 'gift_product') {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Give'),
    );
  }
}
?>

How to change the form submit text in general:

<?php
function MYMODULE_form_alter(&$form, &$form_state, $form_id) { 
  if (strpos($form_id, 'commerce_cart_add_to_cart_form') !== FALSE) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Buy now'),
    );
  }
}
?>

How to do that based on the line item type, on D7, latest stable Commerce?

Will this do the work? (not a developer):

<?php
function mymodule_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state) {
  $line_item = $form_state['line_item'];
  if ($line_item->type == 'gift') {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Give'),
    );
  }
  elseif ($line_item->type == 'contribution') {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Contribute'),
    );
  }
}
?>

My usecase:
I have a view with 2 add to cart forms for each product listed (a giftlist).

  • One is using the standard line item type,
  • the other is using a custom one, to let the user input a value (a contribution amount)

I would like a "Give" button and a "Contribute" button.

PS: I could not post a comment on the linked page because the site gives me error when I try to login
PPS: I image this is really simple, but I couldn't find any documentation since custom line items are contrib

Thanks!

Comments

kopeboy’s picture

Issue summary: View changes
kopeboy’s picture

Version: 7.x-1.0-beta2 » 7.x-1.x-dev
Priority: Major » Normal
Status: Active » Reviewed & tested by the community

YES! That exact code worked :)

First custom module and worked at first trial LOL

This is nice documentation for noobs like me, feel free to add it somewhere and close the issue.
..Or if you see any improvements please tell me! :D

nvahalik’s picture

Status: Reviewed & tested by the community » Closed (works as designed)