We'll need an EFQ query, same as product does.

Comments

pcambra’s picture

Title: When a coupon has been applied to an unfinished order, don't allow to delete it » When a coupon has been applied to an order, don't allow to delete it
pcambra’s picture

Status: Active » Fixed

Fixed!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

foopang’s picture

Status: Closed (fixed) » Needs work

Hi pcambra, I really need it to be able to delete a coupon from an order.
Tried commerce_coupon_remove_all_coupons_from_order() and commerce_coupon_remove_coupon_from_order() didn't work, please help!

pcambra’s picture

Status: Needs work » Closed (fixed)

Please do not reopen closed issues after long time, also your problem don't have anything to do with this one.

foopang’s picture

Sorry about that. Basically what I want to do is to add a remove coupon button if there are existing coupons for the order in the checkout page.
I use a custom submit callback function to handle, but it is not working. Would you please advise how I can fix that? Many thanks for your help!

function mymodule_form_alter(&$form, &$form_state) {
  if (strstr($form_id, 'commerce_checkout_form_')) {
    if (isset($form['commerce_coupon'])) {
      if (($coupons = commerce_coupon_get_coupons_in_order($form_state['order']->order_id))) {
        
        //add remove coupon button if there are existing coupons in the order
        $form['commerce_coupon']['coupon_remove'] = array(
          '#type' => 'submit',
          '#value' => t('Remove coupon'),
          '#name' => 'coupon_remove',
          '#limit_validation_errors' => array(),
          '#submit' => array('mymodule_checkout_form_remove_coupon_submit'),
        );
        
      }
    }
  }
}

function mymodule_checkout_form_remove_coupon_submit($form, &$form_state) {
  $order = $form_state['order'];
  if($coupons = commerce_coupon_get_coupons_in_order($order->order_id)) {
    foreach($coupons as $coupon) {
      commerce_coupon_remove_coupon_from_order($order, $coupon);
    }
  }

  $form_state['rebuild'] = TRUE;
}