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).
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);
}
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | commerce_stock-fix-add-to-cart-button-text-2291359-7.patch | 958 bytes | bisonbleu |
| Add to cart button text.png | 44.27 KB | bisonbleu |
Comments
Comment #1
guy_schneerson commentedThanks 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'
Comment #2
bisonbleu commentedHello Guy. Setting status to a task is logical.
That being said, here's what @rszrama had to say about this patch 4 months ago.
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.
Comment #3
guy_schneerson commentedthanks 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.
Comment #4
guy_schneerson commentedJust set myself up and the its the commerce stock api module (commerce_stock.module) that needs updating
Comment #5
guy_schneerson commentedneeds 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
Comment #6
bisonbleu commentedChanging 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!
Comment #7
bisonbleu commentedAnd 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 : )
Comment #8
lsolesen commented@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.
Comment #9
guy_schneerson commentedThanks @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.
Comment #10
bisonbleu commentedBTW the patch was made against the latest dev from version control. So setting version to 7.x-2.x.
Comment #11
guy_schneerson commentedGreat 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
Comment #13
guy_schneerson commented@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