2

Can somebody tell me what is wrong in my code, please? I'm trying to replace the regular text for 'add to cart' button for two types of product. I'm using Drupal Commerce module. But something is wrong in my code. Works just first part of code.

function modulename_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state) {

  $product_types = commerce_product_types(); 

  if (isset($product_types['product']))  {
    $product = $form_state['default_product'];
    $price = commerce_product_calculate_sell_price($product);
    $price_display = commerce_currency_format($price['amount'], $price['currency_code'], $product);
    $form['submit']['#value'] = 'Buy this product ' . $price_display;
  }

  if (isset($product_types['product_2']))  {
    $product = $form_state['default_product'];
    $price = commerce_product_calculate_sell_price($product);
    $price_display = commerce_currency_format($price['amount'], $price['currency_code'], $product);
    $form['submit']['#value'] = 'Buy these products just for ' . $price_display;
  }

}
1

try this

/**
 * Implements hook_form_FORM_ID_alter().
 */
function MY_MODULE_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id) {

  //drupal_set_message($form_id);

  $form['submit']['#attributes']['value'] = t('Buy Now');

  /*
  $current_product = $form_state['default_product'];
  if ($current_product->type == 'product') {
    // Make alternations for products...
  }
  */

}
  • I have two types of product. I can not write the correct condition. My new text for button is the same for two products type. – Aca May 12 '15 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.