Tags/topics: 
2
Answers
Vote up!
0
Vote down!

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

Asked by: sammyfm
on June 16, 2013

2 Answers

Vote up!
1
Vote down!

implement hook_form_alter

Answer by: akosipax
Posted: Jun 17, 2013

Comments

Thanks! i´m pretty bad at this hook_form_alter thing... Could you help?

- sammyfm on June 17, 2013

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.

- akosipax on June 17, 2013

Thx mate, tried to implement and the site crashes... maybe a wrong value?

- sammyfm on June 18, 2013

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.

- akosipax on June 18, 2013

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?

- sammyfm on June 18, 2013

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. :)

- akosipax on July 9, 2013

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!

- sammyfm on June 19, 2013
Vote up!
0
Vote down!

Put this in a module:

<?php
/**
* Implements hook_form_FORMID_alter() to add the price to the Add to Cart button.
*/
function MODULE_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state) {
 
$lang           = $form_state['default_product']->language;
 
$amount         = $form_state['default_product']->commerce_price[$lang][0]['amount'];
 
$currency_code  = $form_state['default_product']->commerce_price[$lang][0]['currency_code'];
 
$price = commerce_currency_format($amount,$currency_code);

 
$form['submit']['#value'] = $price." " . t('Add to cart');
}
?>
Answer by: immoreel
Posted: Jul 18, 2014