Currently the feature to add a coupon to an order from the order_ui admin area is broken.

The form provides an 'amount' and 'currency' (based on the shipping module?) that doesn't actually work.

This feature is marked as "TODO" in the code.

The attached patch fixes that issue and provides the ability to add commerce coupon line items from the admin order edit form.


Please review and commit when possible.


P.S Thanks for the useful module!


pcambra’s picture

Status: Needs review » Needs work

I think you must have coded the patch against beta because it doesn't apply to the latest -dev

More things I see in the patch review:

  • An autocomplete, the same way as the product reference does would be very nice.
  • Not all coupon types are supposed to create a line item, we need to tackle that.
davidwhthomas’s picture

Yes, this was coded against the recent beta, it needs rework for the latest -dev

Currently possible perhaps with just commerce_coupon_redeem_coupon($coupon, $order) but raises an Entity Exception as coupon_reference field not quite set correctly... looking into it though.


pcambra’s picture

Marking this one as dependent of #1547440: Kill coupon log entity

After killing the coupon log, we can expose our own form in the order UI, so we don't depend on the line item manager (not all coupons create line items).
Additionally we'll have to filter out all coupons from the line item manager.

davidwhthomas’s picture


Thanks, but can you give an example of when a coupon wouldn't create a line item?

Even if it doesn't adjust the price, it can show as a line item, with no price adjustment ( e.g free gift coupon )

Isn't it always useful to have a line item on the order indicating a coupon has been added to it?


pcambra’s picture

Percentage coupons don't create a line item by design. If we create a line item for % coupons, there would be no way to combine the % with other taxes and discounts and therefore we couldn't decide the order in which the % would be applied.

davidwhthomas’s picture

@pcambra, thanks for the explanation.

Attached is a patch that provides a local action menu item on the admin order view to add a coupon ( as opposed to line item manager ).

It does this

  1. Adds a "local action" type menu item to commerce_coupon_ui.module for the admin order ui page
  2. Adds a form builder to
  3. Adds a form validation handler to
  4. Adds a form submit handler to

The coupon is added to the order in the submit handler via the generic commerce_coupon_redeem_coupon($coupon, $order)

It's working for fixed amount coupons, which add a line item, but for percentage coupons, it adds to the log and shows the correct granted amount, but doesn't adjust the order total. I tried various refresh hook calls and order save/load but the percentage coupon wouldn't adjust the order total.

In any case, it's better than nothing as is working for fixed amount coupons from the admin order ui.

Feedback welcome,



pcambra’s picture

That's great, we've got a good start point here.

Next steps would be:

  • Integrate this form in the commerce order UI same as the line item manager. (Just below?)
  • Remove the coupon line item types from the line item manager.
  • I think this should just expose something like a hook and each coupon type should declare what they do to apply the coupon. Fixed amount would create a line item, % coupons would add it to the order... We're kind on dependent of a change in Commerce for the last one.
davidwhthomas’s picture

Thanks, unfortunately I don't have time on this project to add much finesse other than basic functionality to add coupons to orders from admin.

However, I did get the percentage coupon applying fine by adding commerce_cart_order_refresh to the submit handler.

That function is normally called during checkout when progressing between pages.

Updated function is

 * Form submit callback: Redeem coupon
function commerce_coupon_order_add_form_submit($form, &$form_state) {
  if ($code = $form_state['values']['coupon_code']) {
    $commerce_coupon = commerce_coupon_load_by_code($code);
    commerce_coupon_redeem_coupon($commerce_coupon, $form_state['values']['commerce_order']);



pcambra’s picture

Ok, an update after fixing #1547440: Kill coupon log entity

After this patch coupons are a field in the order, so there's a widget appearing in the order UI form.

All we need to do here now is avoid to display the line item coupon in the line item manager.

ivanbueno’s picture

Status: Needs work » Needs review
4.87 KB

I updated David's patch to work with the latest dev version commerce_coupon_ui. I added the following implementations:
- remove Coupon option in the Add Line Item dropdown
- disable updates on coupons that are listed in the line item manager
- create Coupons tab in the order
- @see screenshot


pcambra’s picture

Status: Needs review » Needs work

Thanks for rescuing this!

I like the addition of blocking the coupon line items and moving the coupons to a different tab might be a good idea.

But commerce_coupon_order_reference field might be enough for this, it should be displayed intentionally at the end of the order edit form with an autocomplete widget... thoughts?

Some minor review stuff.

+++ b/commerce_coupon_ui.module
@@ -141,6 +141,18 @@ function commerce_coupon_ui_menu() {
+  ¶

@@ -447,3 +459,27 @@ function commerce_coupon_ui_validate_coupon_type_unique($type) {
+      ¶
+      // Remove coupon from the options when adding line items to the order. ¶

+++ b/includes/
@@ -317,3 +317,75 @@ function commerce_coupon_ui_coupon_type_delete_form_submit($form, &$form_state)
+  ¶
+  ¶
+  ¶

Watch out for the extra spaces

+++ b/commerce_coupon_ui.module
@@ -141,6 +141,18 @@ function commerce_coupon_ui_menu() {
+    'access arguments'  => array('administer commerce_order entities'),

Maybe this should be a combination of administering orders and coupons?

pcambra’s picture

Issue summary: View changes