Buy-it-link is my wrapper div for the product_reference field in my node.tpl.php file. The element can probably be specified as well by another class or id.
I can see the code but I just can't identify where "MyModule", "$Product","$Product-> type", "$line_item is.
What is the path that I can use to find these items on my own website.
Hi, i tried to use the code suggested by tripper54 and it works, but only for one button per page because the id is hard-coded.
I found the helper function commerce_form_callback to solve that:
function example_form_alter(&$form, $form_state, $form_id) { if (commerce_form_callback($form_id, &$form_state) == "commerce_cart_add_to_cart_form") { $form['submit']['#attributes']['title'] = $form['submit']['#attributes']['value'] = t('Buy Now'); } }
But I keep getting a warning:
Deprecated function: Call-time pass-by-reference has been deprecated; If you would like to pass it by reference, modify the declaration of commerce_form_callback(). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file in phptemplate_init() (line 14 of /home/phyll/drupal/example/themes/engines/phptemplate/phptemplate.engine).
One could "solve" that by editing a value in php.ini, but I'm looking for a more future proof solution which does not use deprecated functions. Does anyone with more php knowlegde than me know solution?
If you're just changing text, use Drupal's excellent translation facilities. If you have the core locale module turned on, you can use it, or if not, use the excellent [String Overrides](http://drupal.org/project/stringoverrides)
That goes for every change in Drupal's UI: If it's wrapped in t(), then use stringoverrides or locale to change it; there's no need to do it with more intrusive techniques.
The String Overrides (or Locale and Content translation) module is still a good way to go. Style the button with a background image using css if you need to.
you don't have to use hook_form_alter, you can use hook_form_form_id_alter(&$form, &$form_state, $form_id) which is a D7 thing I think.
Also instead of overwriting whole form[submit] you can change just what you want to change like
function mymodule_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id) { $form['submit']['#type']='image_button'; $form['submit']['#src']='path/to/your/image'; }
Thank you for the suggestion. Unfortunately, hook_form_form_id_alter() doesn't quite work right either. On my product display pages with multiple products referenced, changing the select list (choosing a different product) does not update the other product fields (price, image, etc.) listed on the page. Maybe it's cache or AJAX related?
Here is a working form alter from a module named commerce_installments. In this case, it is only changing the button text for products that are of type 'installment_payment'.
/** * Implements hook_form_FORMID_alter() to change the Add to Cart button text. */ function commerce_installments_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 == 'installment_payment') { // Change the submit button text to more appropriate "Pay now" $form['submit']['#value'] = t('Pay now'); } }
I tried to save this file in my theme's template directory as commerce-installments.tpl.php but I don't think that's right. What is the correct way to save this file?
For anyone wanting to make the Add to Cart button an image, you can use the following function in a custom module. Special thanks to rfay, Damien Tournoud, and oz_an for the assistance.
<?php /** * Implements hook_form_FORMID_alter() to change the Add to Cart button to an image. */ 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 == 'product') { $form['submit'] = array( '#type' => 'image_button', '#src' => drupal_get_path('module', 'mymodule') .'/addtocartimage.gif', '#weight' => 15, ); } } ?>
By the way I'd be no where if randy fay wouldn't be around. I thank you for all his wonderful tutorials in vimeo!!. My code is not hypothetical it is just works fine for me. I couldn't inspect all the stuff with language_none and others, I think your and randy's addition is adding some conditions to it, like product->type and all.
Thanks all...
I am attempting to change buttons to image buttons. Using the above techniques, it seems to break ajax. For example, when the 'address copy' functionality is implemented, it will not work if the checkout form has an image for a submit button.
// Will break your form: function mjcc_global_form_commerce_checkout_form_checkout_alter(&$form, &$form_state) { $form['buttons']['continue']['#type'] = 'image_button'; $form['buttons']['continue']['#src'] = '/'. drupal_get_path('theme', 'mjcc') . '/images/buttons/next-step.png'; }
I'm trying to add some placeholder text to a custom field on the add to cart form. I'm using the name value of the field which works on other forms such as login but doesn't want to work on this form.
There is a long-standing issue on drupal.org that covers this. A recent patch provided in this issue queue adds the ability to override the default button text, so you can make the text what you want:
Comments
Or can we just replace the
Or can we just replace the "Add to Cart" button to our own button image?
Yes, you can
A simple image replacement. Here is code that I used:
#buy-it-link #edit-submit
{
background:url('../images/try-on--buy-it-sprite.png') no-repeat;
background-position: 0px 0px;
text-indent:-999px;
height: 43px;
width: 125px;
display:block;
border: none;
}
#buy-it-link #edit-submit
{
background:url('../images/try-on--buy-it-sprite.png') -125px 0px no-repeat;
}
Buy-it-link is my wrapper div for the product_reference field in my node.tpl.php file. The element can probably be specified as well by another class or id.
What module are we taking about here?
I can see the code but I just can't identify where "MyModule", "$Product","$Product-> type", "$line_item is.
What is the path that I can use to find these items on my own website.
hook_form_alter
A small module with an implementation of hook_form_alter will do the trick:
/**
* Implements hook_form_alter
*
*/
function YOURMODULE_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'commerce_cart_add_to_cart_form_1') {
$form['submit']['#attributes']['title'] = $form['submit']['#attributes']['value'] = t('Buy Now');
}
}
replace YOURMODULE with the name of your module, and the form id as required.
commerce_form_callback
Hi, i tried to use the code suggested by tripper54 and it works, but only for one button per page because the id is hard-coded.
I found the helper
function commerce_form_callback
to solve that:function example_form_alter(&$form, $form_state, $form_id) {
if (commerce_form_callback($form_id, &$form_state) == "commerce_cart_add_to_cart_form") {
$form['submit']['#attributes']['title'] = $form['submit']['#attributes']['value'] = t('Buy Now');
}
}
But I keep getting a warning:
Deprecated function: Call-time pass-by-reference has been deprecated; If you would like to pass it by reference, modify the declaration of commerce_form_callback(). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file in phptemplate_init() (line 14 of /home/phyll/drupal/example/themes/engines/phptemplate/phptemplate.engine).
One could "solve" that by editing a value in php.ini, but I'm looking for a more future proof solution which does not use deprecated functions. Does anyone with more php knowlegde than me know solution?
Seems to work now, after i
Seems to work now, after i removed the & in "&$form_state".
find Yourmodule and form id
I do I find "yourmodule" and the "form id".
Just changing text? Use String Overrides
If you're just changing text, use Drupal's excellent translation facilities. If you have the core locale module turned on, you can use it, or if not, use the excellent [String Overrides](http://drupal.org/project/stringoverrides)
That goes for every change in Drupal's UI: If it's wrapped in t(), then use stringoverrides or locale to change it; there's no need to do it with more intrusive techniques.
How about changing the add to cart button to an image?
The String Overrides (or
The String Overrides (or Locale and Content translation) module is still a good way to go. Style the button with a background image using css if you need to.
String Overrides doesn't work for me
I've tried this on a couple of sites and it doesn't work for me.
Try this
you don't have to use hook_form_alter, you can use hook_form_form_id_alter(&$form, &$form_state, $form_id) which is a D7 thing I think.
Also instead of overwriting whole form[submit] you can change just what you want to change like
function mymodule_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id) {
$form['submit']['#type']='image_button';
$form['submit']['#src']='path/to/your/image';
}
Thank you for the suggestion.
Thank you for the suggestion. Unfortunately, hook_form_form_id_alter() doesn't quite work right either. On my product display pages with multiple products referenced, changing the select list (choosing a different product) does not update the other product fields (price, image, etc.) listed on the page. Maybe it's cache or AJAX related?
Here is a working form alter
Here is a working form alter from a module named commerce_installments. In this case, it is only changing the button text for products that are of type 'installment_payment'.
/**
* Implements hook_form_FORMID_alter() to change the Add to Cart button text.
*/
function commerce_installments_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 == 'installment_payment') {
// Change the submit button text to more appropriate "Pay now"
$form['submit']['#value'] = t('Pay now');
}
}
How do I know the name of
How do I know the name of "MyModule", "$Product","$Product-> type", "$line_item
Where do you save this file?
I tried to save this file in my theme's template directory as commerce-installments.tpl.php but I don't think that's right. What is the correct way to save this file?
$form['submit']['#value'] = t('Pay now'); has no effect !
I'm using the code suggested by @rfay above. But the "Add to cart string" just won't change. I've added this line at the end of the if statement.
drupal_set_message('The Submit value is: ' . $form['submit']['#value']);
The returned message is: "The Submit value is: Pay now". But still the "Add to cart" button shows "Add to cart".
What am I missing?
Cheers! Saved me a lot of
Cheers! Saved me a lot of time :)
Just modified it for out of stock state
function MYMODULE_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id) {
if($form['submit']['#disabled'] == true) {
$form['submit']['#type']='image_button';
$form['submit']['#src']='sites/default/files/images/out-of-stock.png';
} else {
$form['submit']['#type']='image_button';
$form['submit']['#src']='sites/default/files/images/add2cart.png';
}
}
Add this to your stylesheet
.form-button-disabled {cursor:default;}
Otherwhise the out of stock image will get the pointer cursor too.
EDIT:
You can add an 'alt' attribute just add
$form['submit']['#alt']='add to cart';
Note that the product
Note that the product reference fields on the line items are always non-translatable. Hardcoding LANGUAGE_NONE is the good thing here.
Thanks - edited it.
Thanks - edited it.
For anyone wanting to make
For anyone wanting to make the Add to Cart button an image, you can use the following function in a custom module. Special thanks to rfay, Damien Tournoud, and oz_an for the assistance.
<?php
/**
* Implements hook_form_FORMID_alter() to change the Add to Cart button to an image.
*/
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 == 'product') {
$form['submit'] = array(
'#type' => 'image_button',
'#src' => drupal_get_path('module', 'mymodule') .'/addtocartimage.gif',
'#weight' => 15,
);
}
}
?>
Thanks to rfay
By the way I'd be no where if randy fay wouldn't be around. I thank you for all his wonderful tutorials in vimeo!!. My code is not hypothetical it is just works fine for me. I couldn't inspect all the stuff with language_none and others, I think your and randy's addition is adding some conditions to it, like product->type and all.
Changing submit buttons to images breaks ajax
Thanks all...
I am attempting to change buttons to image buttons. Using the above techniques, it seems to break ajax. For example, when the 'address copy' functionality is implemented, it will not work if the checkout form has an image for a submit button.
// Will break your form:
function mjcc_global_form_commerce_checkout_form_checkout_alter(&$form, &$form_state) {
$form['buttons']['continue']['#type'] = 'image_button';
$form['buttons']['continue']['#src'] = '/'. drupal_get_path('theme', 'mjcc') . '/images/buttons/next-step.png';
}
add to cart form - add placeholder text
I'm trying to add some placeholder text to a custom field on the add to cart form. I'm using the name value of the field which works on other forms such as login but doesn't want to work on this form.
if ($form_id == 'commerce_cart_add_to_cart_form_4') {
$form['line_item_fields[field_name][und][0][value]']['#attributes']['placeholder'] = t( 'Name' );
$form['submit']['#attributes']['title'] = $form['submit']['#attributes']['value'] = t('Buy Now');
}
button-tag?
is it also possible to use the button-tag?
i tried the following without success.
<?php
$form['submit']['#type'] = "markup";
$form['submit']['#markup'] = '<button type="submit" class="form-submit"><span class="form-submit-before"></span>In den Warenkorb <span class="form-submit-after"></span></button>';
?>
Drupal 7
Drupal 7
<?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'),
);
}
}
?>
There is patch that adds this to the Commerce module
There is a long-standing issue on drupal.org that covers this. A recent patch provided in this issue queue adds the ability to override the default button text, so you can make the text what you want:
https://www.drupal.org/node/1147690#comment-9180559
The patch works on Commerce 7-1.11. I haven't tried it on earlier versions.