2
Answers
Adding the price to cart button text
Hy guys,
right now the "add to cart" button and the "price" are two different things. But i want to put the price text also inside the cart button:
Before:
Price: 100$
Button: Add to cart
After:
Button: 100$ | add to cart
I´m right now doing this through js but had some problems with interfering ajax calls.
Is there a way to alter this?
Thx
Comments
Thanks! i´m pretty bad at this hook_form_alter thing... Could you help?
in a custom module, implement something like:
<?php
function MYMODULE_form_alter(&$form, &$form_state, $form_id)
// Get product price using the product object in the $form_state variable. You can use dpm() -- a devel function -- to explore what's in the $form_state variable
// Override the text
$form['submit']['#value'] = $price . t('Add to cart');
}
?>
For more info read here : https://api.drupal.org/api/drupal/modules%21system%21system.api.php/func...
Form API: https://api.drupal.org/api/drupal/developer%21topics%21forms_api_referen...
Devel module: http://drupal.org/project/devel
Hope this helps.
Thx mate, tried to implement and the site crashes... maybe a wrong value?
May I see your code and the error mentioned in your logs? Also, you have to remember to identify the form you're altering by using the $form_id variable.
Ok, my failure i put the code to the template file...
I set up a new module called: commerce_custom_add_to_cart_button
in the .info i added:
name = Custom Add to Cart Button
description = Puts the price inside the Add to Cart Button
core = 7.x
and in the .module:
<?php
/**
* @file
* A module that adds the price to the Add to Cart button
*/
function commerce_custom_add_to_cart_button_form_alter(&$form, &$form_state, $form_id)
// Get product price using the product object in the $form_state variable. You can use dpm() -- a devel function -- to explore what's in the $form_state variable
// Override the text
$form['submit']['#value'] = $price . t('Add to cart');
}
When i try to activate the module it breaks the confirm page and after that checking, the module is not activated...
Maybe i´m doing something really wrong here?
though the files are optional. It's good practice that you declare them. (see https://drupal.org/node/542202)
Also, you copy pasted everything I did without adding any logic. You don't even have the price variable set. :)
ah ok, well i have no practice at this, but of course i understand that i probably should pay somebody to do it instead for asking here!
Thx so far!