Commit 7be19e92 authored by Camille Lochet's avatar Camille Lochet Committed by Dan Polant
Browse files

Issue #2374713 by skyhawk669: Unable to remove coupons from discount edit screen

parent 5c19ec9e
Loading
Loading
Loading
Loading
+17 −8
Original line number Diff line number Diff line
@@ -1512,25 +1512,34 @@ function commerce_coupon_form_attach_coupons(&$form, &$form_state) {
        // attached to no other discounts, delete it completely.
        $remove_coupon = commerce_coupon_load_by_code($original_coupon->code);
        $wrapper = entity_metadata_wrapper('commerce_coupon', $remove_coupon);
        $delete = TRUE;

        if (!empty($form_state['commerce_discount']->discount_id)) {
          foreach ($wrapper->commerce_discount_reference->value() as $discount) {
            if ($discount->discount_id != $form_state['commerce_discount']->discount_id) {
              $delete = FALSE;
          foreach ($wrapper->commerce_discount_reference->value() as $delta => $discount) {
            if ($discount->discount_id == $form_state['commerce_discount']->discount_id) {
              // Break the reference from this coupon to the discount currently
              // being edited in the form. @TODO: why is the wrapper not working
              // here?
              $language = field_language('commerce_coupon', $remove_coupon, 'commerce_discount_reference');
              unset($remove_coupon->commerce_discount_reference[$language][$delta]);
            }
          }
        }

        if ($delete) {
        // If no discounts remain, delete the coupon.
        if (empty($remove_coupon->commerce_discount_reference[$language])) {
          commerce_coupon_delete($original_coupon->coupon_id);
        }
        else {
          // If we are not deleting, save the coupon since we eliminated one or
          // more of its discount references.
          $wrapper->save();
        }

        // Force rules rebuild.
        $rebuild_rules = TRUE;
      }
    }
  }
  }

  // Rebuild the rules again if coupon added or deleted.
  if($rebuild_rules) {