2

Using D7 commerce coupon/discount modules. I need to make the coupon field mandatory. Every order needs a coupon code entered.

There is no standard "required" checkbox on the field and I tried using conditional fields module to no avail.

enter image description here

  • I'm not sure I understand. Does every order need to be accompanied by a Coupon code? As in, you can't order without a coupon code? Or do you just want to make sure the coupon code field isn't blank when people hit the "Add coupon" button? – Menno van den Heuvel Nov 22 at 12:50
  • They always need to use a coupon code for every order. – Nigel Waters Nov 22 at 15:47
  • Why use the coupon module at all? Could you not simply add a required field to the order type? Or are there all kinds of coupons with all kinds of discounts? – Menno van den Heuvel Nov 22 at 18:40
  • There are multiple affiliates that sell a product through their own systems that issue a user with a coupon code with varying levels of discounts. The coupons and amounts vary too much to go the route you mention. However a coupon code is required for all purchases. – Nigel Waters Nov 22 at 20:22
  • I understand. I'll see if I can come up with something. – Menno van den Heuvel Nov 22 at 20:25
6
+50

You must do this in a hook_form_FORM_ID_alter().

You wouldn't use hook_form_alter() unless you are checking on a dynamic form ID. In this case it's better to use hook_form_FORM_ID_alter().

The reason for this, is that hook_form_alter() is called on every form render and hook_form_FORM_ID_alter() is only called when that specific form is being rendered.

Add this code to your .module file to make the field required:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function MY_MODULE_form_commerce_checkout_form_checkout_alter(&$form, &$form_state, $form_id) {
  if (!empty($form['commerce_coupon']['coupon_code'])) {
    $form['commerce_coupon']['coupon_code']['#required'] = TRUE;
  }
}
  • 1
    This is the better answer, due to the - justified - use of hook_form_FORM_ID_alter – Stefanos Petrakis Nov 28 at 7:56
4

You can do that by Implements hook_form_BASE_FORM_ID_alter but that would allow passing the step if the user enters any random string inside the coupon field.

To avoid this confusion you need to use the form alter to make coupon field required and also add custom validation to check if the coupon is valid.

if the coupon is not valid, then the user will stay in the step and will get the same error message provided by the commerce coupon module.

I assume your custom module name is 'CUSTOM'

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function CUSTOM_form_commerce_checkout_form_checkout_alter(&$form, &$form_state, $form_id) {
  if (!empty($form['commerce_coupon']['coupon_code'])) {
    // Set code as required.
    $form['commerce_coupon']['coupon_code']['#required'] = TRUE;
    $form['buttons']['continue']['#validate'][] = 'my_custom_coupon_validate';
  }
}

/**
 * Custom coupon validate.
 */
function my_custom_coupon_validate(&$form, &$form_state) {
  $code = $form_state['input']['commerce_coupon']['coupon_code'];
  $order = $form_state['order'];

    $coupon = commerce_coupon_redeem_coupon_code($code, $order, $error);
    if ($coupon) {
      // Reload the order so it is not out of date.
      $order = commerce_order_load($order->order_id);
      // Recalculate discounts.
      commerce_cart_order_refresh($order);
    }
    else {
      form_set_error('coupon_code',$error);
    }
}
  • Hook form alter isn’t the preferred solution unless you are checking for dynamic form ID. – Jdrupal Nov 27 at 22:46
  • @Jdrupal thanks for suggesting that, I change the implementation. – Ziftman Dec 3 at 10:35

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.