Until recently, there was no way to set/customize the text of the Add to cart button in the UI. But thanks to a new patch, this is now possible through a configuration at the field level by content type (see attachment).

Make "Add to cart" button text a display format setting (configurable at the field level by content type)

Unfortunately, this patch breaks and has no effect when the Commerce Stock module is enabled.

I'm not 100% sure, but I think that the problem comes from lines 137 and/or 258 of commerce_stock.module which re-initialize the Add to cart form. Lines 123-160 are printed below for reference.

It would be nice to review commerce_stock code or module weight in order to allow this patch to do its magic.

/**
 * Form validation handler for commerce_cart_add_to_cart_form().
 *
 * For products with options (product dropdown) checks if the add to cart form
 * should be enabled (in stock).
 *
 * @see commerce_cart_add_to_cart_form()
 */
function commerce_stock_cart_state_validate_options($form_id, &$form, &$form_state) {
  $product_id = $form['product_id']['#default_value'];
  $product = commerce_product_load($product_id);
  $qty_ordered = commerce_stock_check_cart_product_level($product_id);

  // Initialize the form.
  $form['submit']['#value'] = t('Add to cart'); // THIS IS LINE 137
  $form['submit']['#disabled'] = FALSE;
  $form['#attributes']['class']['stock'] = 'in-stock';

  // Set global form for stock actions.
  global $stock_cart_check_data;
  $stock_cart_check_data = array(
    'form' => &$form,
  );

  // Integration with rules_form_alter().
  if (module_exists('rules_form_alter')) {
    // make sure rules_form_alter actions work from the stock event.
    global $rules_form_alter_data;
    $rules_form_alter_data = array(
      'id'  => $form_id,
      'form' => &$form,
      'state' => &$form_state,
    );

  }
  // Invoke the stock check event.
  rules_invoke_event('commerce_stock_check_add_to_cart_form_state', $product, $qty_ordered, $form);
}

Comments

guy_schneerson’s picture

Category: Bug report » Task

Thanks bisonbleu for bringing this to my attention.
Hope you don't mind me changing status to a task, as this is to do with a patch that is not yet committed commerce. Will have a look and hopefully can sort this out before it becomes a bug :)

Not directly related but to do with changing text Commerce cart validate shouldn't use triggering_element's '#value'

bisonbleu’s picture

Hello Guy. Setting status to a task is logical.

That being said, here's what @rszrama had to say about this patch 4 months ago.

(...) but I do promise to get a review in prior to the 1.10 release (which shouldn't take near as long as the jump from 1.8 to 1.9).

And just for perspective, it took about 7 months to go from Commerce 1.8 to Commerce 1.9. So this issue might become a bug in a couple of months. ; )

p.s. Unfortunately, I'm a novice when it comes to PHP. But I'll help if I can.

guy_schneerson’s picture

thanks bisonbleu,
Your reading the issues, analysing the situation and reporting it is incredibly useful.
This is a high priority task for me, and I was planning at looking at this on our next Local contrib meetup
Keep up the good work.

guy_schneerson’s picture

Just set myself up and the its the commerce stock api module (commerce_stock.module) that needs updating

guy_schneerson’s picture

needs some more investigation and testing but looks to me I should be getting the button text from the existing form instead of hard coding it, and job done

<?php

function commerce_stock_cart_state_validate_options($form_id, &$form, &$form_state) {
  ..
  // Initialize the form.
  $form['submit']['#value'] = $form['submit']['#value'];
 ...
}
?>
bisonbleu’s picture

Changing lines 137 and 258 of commerce_stock.module (version 7.x-2.0-rc1) as prescribed in #5 to $form['submit']['#value'] = $form['submit']['#value']; appears to fix the problem. The value of Add to cart button text in the product_reference field settings page is honoured without a glitch.

Thanks Guy!

bisonbleu’s picture

And here is a patch that does just what I'm describing in #6. Let me know if the patch is OK as this is my first patch ever : )

lsolesen’s picture

Version: 7.x-2.0-rc1 » 7.x-2.0
Category: Task » Bug report
Priority: Major » Normal
Status: Active » Needs review

@bisonbleu Just remember to set the issue status to Needs Review when posting a patch. I haven't had time to test it out yet.

guy_schneerson’s picture

Thanks @bisonbleu for uploading the patch, I will test with an older version of commerce but don't see a reason for this not to work.
and thanks @lsolesen for updating the issue status.

bisonbleu’s picture

Version: 7.x-2.0 » 7.x-2.x-dev

BTW the patch was made against the latest dev from version control. So setting version to 7.x-2.x.

guy_schneerson’s picture

Status: Needs review » Reviewed & tested by the community

Great work bisonbleu, I tested this patch with both the commerce patch and without and no issues, also searched the code to make sure we haven't missed any other hard coded 'add to cart' and didn't find any.
Will commit

guy_schneerson’s picture

Status: Reviewed & tested by the community » Fixed

@bisonbleu this is now committed and you are now a contributor check you profile projects section :)
This will be available on the dev version in in the next 24 hours, But we need to make sure we have a full release before the commerce patch does so lets keep an eye.
Thanks again

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.