Hi,
I just want to limit the number of coupon in an order.
In the content type "order", i just change the field "coupon" in limited number of value to 1.
But I have an ajax error when I click on the "add coupon" button in the checkout.
fatal error : Call to a member function raw() on a non-object in /.....commerce_coupon_module on line 816

here is the function :

/**
 * Finds out if a given coupon code is present in an order.
 *
 * @param $code
 *   Coupon code to check.
 * @param $order
 *   Commerce order object.
 *
 * @return boolean
 *   Returns TRUE if the coupon is in the order, otherwise return FALSE.
 */
function commerce_coupon_code_is_in_order($code, $order) {
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  foreach ($order_wrapper->commerce_coupon_order_reference as $coupon_wrapper) {
    if (strcasecmp($coupon_wrapper->commerce_coupon_code->raw(), $code) == 0) {
      return TRUE;
    }
  }
  return FALSE;
}

Regards,

Comments

andyg5000’s picture

Title: can not set only one coupon per order » Provide option to limit 1 coupon per order
Version: 7.x-1.0-beta7 » 7.x-2.x-dev
Category: Bug report » Feature request
Issue summary: View changes

Most sites that I deal with only want to allow 1 discount coupon per order. There should be a way to enable this as a setting somewhere or maybe even a disabled rule that can be enabled by site administrators that want this limitation. In the meantime, here's a hook for anyone needing the same thing.


/**
 * Implements hook_commerce_coupon_condition_outcome_alter().
 */
function MYMODULE_commerce_coupon_condition_outcome_alter(&$outcome, $context) {
  // Limit 1 discount coupon per order.
  $order = $context['order'];
  $coupon_wrapper = $context['coupon'];
  if (!empty($order->commerce_coupons)) {
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    foreach ($order_wrapper->commerce_coupons->value() as $coupon) {
      if ($coupon->coupon_id != $coupon_wrapper->coupon_id->value() && $coupon->type == 'discount_coupon') {
        drupal_set_message(t('You can only use 1 coupon per order'), 'error');
        $outcome = FALSE;
      }
    }
  }
}
Sergio Morais’s picture

My php knowledge is limited. I wonder how to modify the code from #1 to consider only one discount type. My example: I have two coupon types: discount_coupon and giftcard_coupon. I want to limit the usage of discount coupons but not the use of giftcards. One should be able to add several giftcards. Could anyone help me? Thank you in advance.

hugronaphor’s picture

One more way would be just to hide the input if there are any added already.

/**
 * Implements hook_form_FORM_ID_alter().
 */
function HOOK_form_commerce_checkout_form_checkout_alter(&$form, &$form_state) {
  
  if (isset($form['commerce_coupon'])) {
    // If there are coupon codes applied, do not allow adding others.
    if (!empty($form_state['order']->commerce_coupons)) {
      $form['commerce_coupon']['coupon_add']['#access'] = FALSE;
      $form['commerce_coupon']['coupon_code']['#access'] = FALSE;
    }
  }
}
arthur.baghdasar’s picture

Code represented on comment #3 worked for me on 1.x version.

Thank you

aramboyajyan’s picture

Code in #3 worked for me as well.

Edit: code above will hide the form even when the coupon code is invalid, which means users won't be able to re-enter the coupon. Use the following code to support that as well:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function YOUR_MODULE_form_commerce_checkout_form_checkout_alter(&$form, &$form_state) {
  if (isset($form['commerce_coupon'])) {
    // If there are coupon codes applied, do not allow adding others.
    if (isset($form_state['order']->commerce_coupons) && !empty($form_state['order']->commerce_coupons[LANGUAGE_NONE])) {
      $form['commerce_coupon']['coupon_add']['#access'] = FALSE;
      $form['commerce_coupon']['coupon_code']['#access'] = FALSE;
    }
  }
}
davislee’s picture

to aramboyajyan
I added the code into commerce_coupon.module

But it not works.

I found this variant($form_state['order']->commerce_coupons) not works ,
after add a coupon, there is no value of $form_state['order']->commerce_coupons, which makes the Coupon Button displays again after add one coupon.
Please help