5
Answers
Vote up!
1
Vote down!

How do I change the quantity widget label for a specific product type?

I want to alter / change the label of the quantity widget for a specific product type from 'Enter Quantity' to some other text.

Screenshot : http://postimg.org/image/t6s4xoh19/

How can I do it using a hook?

Asked by: sibiru
on March 22, 2013

5 Answers

Vote up!
3
Vote down!

You're going to need to use hook_form_alter() to change the Add to Cart form based on what product it currently represents. All Add to Cart forms use the same form callback even though they use separate IDs, so you can still use hook_form_FORM_ID_alter():

<?php
function example_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state) {
 
// Alter code here...
}
?>

When the Add to Cart form is built, it will determine the current product represented on the form and store it in the form state using $form_state['default_product'], so you can look at the product type and alter the quantity form element's label accordingly.

Ryan Szrama
Answer by: Ryan Szrama
Posted: Mar 22, 2013

Comments

Thanks Ryan but
I've tried change Add To Cart text and Quantity Label using randy fay snippet on previous posting:

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 == 'containere') {
    // Change the submit button text to more appropriate "Pay now"
    $form['submit']['#value'] = t('Tilføj til vogn');
    $form['quantity']['#title'] = t('Vælg ant. containere');
  }
}

Add to Cart text successfully changed but 'Enter Quantity' text not change?
What did I miss?

- sibiru on March 22, 2013
Vote up!
0
Vote down!

I solved this problem using jquery & css instead of hook

Answer by: sibiru
Posted: Apr 1, 2013

Comments

Hi sibiru, hi everybody,
Could you explain me how you change the label "Enter Quantity"
thanks jquery and css. Thanks a lot.

- cafesolo on February 7, 2014
Vote up!
0
Vote down!

I solved this problem by form alter.

Lets say form_alter an attribute $form['quantity']['#title'] is altered in another commerce module already. Depending on the weight of the modules your form_alter may get overwritten so the solution is to run it after the form has been built. At this point you want to make sure it runs the alterations in the contributed modules first before your change occurs, then it is best to use #after_build.

Soln :

function modulename_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id) {
$line_item = $form_state['line_item'];
$product = commerce_product_load($line_item->commerce_product[LANGUAGE_NONE][0]['product_id']);
dpm($product);
if ($product->type == '') {
// Change the submit button text to more appropriate "Pay Now"
$form['submit']['#value'] = t('Pay Now');
$form['#after_build'][] = 'modulename_after_build';

}
}

function modulename_after_build($form, &$form_state) {
// Change the quantity title text to more appropriate "Enter your quantites"
$form['quantity']['#title'] = t('Enter your quantities');
return $form;
}

Posted: Jan 7, 2014
Vote up!
0
Vote down!

Small addition tip to make it work

form_alter of $form['quantity']['#title'] is altered in another commerce module already. Depending on the weight of the modules your form_alter may get overwritten so the solution is to run it after the form has been built.

This is where the After Build function comes in handy. It allows you to modify the form after the form is built and all the form_alter's have been run. Here is a simple code snippet on how to change the text.

function modulename_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id) {
$line_item = $form_state['line_item'];
$product = commerce_product_load($line_item->commerce_product[LANGUAGE_NONE][0]['product_id']);
if ($product->type == '') {
// Change the submit button text to more appropriate "Pay now"
$form['submit']['#value'] = t('Pay Now');
$form['#after_build'][] = 'modulename_after_build';

}
}

function modulename_after_build($form, &$form_state) {
// Change the quantity title text to more appropriate "Enter your quanties"
$form['quantity']['#title'] = t('Enter your quanties');
return $form;
}

Posted: Jan 7, 2014