pcambra’s picture

Version:7.x-1.0-beta5» 7.x-1.x-dev

Useful indeed.

I think that the solution would be to provide an views handler for the area form of the cart view.

firfin’s picture

I did something similar using a webform in a block (advanced option for webform since 3.x I think). Capturing the submission with webform_rules. And using rules (with php unfortunately) to process the input. A lot of work, way beyond the scope of this module.

So a views handler seems like the better option :-)
Would be nice to have, postponed?

pcambra’s picture

This is something that we can reuse for the 2.x branch so, it will be a good thing to have.

doliveros’s picture

Status:Active» Needs work
new72.17 KB
new74.71 KB
new4.9 KB

I tried going with a views area handler, but I failed at rendering the add coupon form without interfering with the cart's checkout / update cart form (The form_id's were getting mixed up, disabling the checkout or update buttons).

I finally did it with a simple form alter. I've attached the complete code, in case anyone wants to recreate it, or in case someone more experienced in custom views handlers with inputs wants to use it. I didn't change the hook's names, so don't forget to change "maze_shop" to "[my_module_name]".

asadblinks’s picture

I have the similar situation, where I need to show coupon form on cart page. Let me see and use your code.

distinctgrey’s picture

Code in comment #4 is working as is for me.
Thanks a lot, good job!

pcambra’s picture

I'm up to add something like this, can someone provide a proper patch for this?

anik8z’s picture

Yes a patch for this would be just lovely

TravisJohnston’s picture

I get a Cart error after removing the coupon.

Also, it says it successfully added the coupon, but the price doesn't change. I am using the coupon module along with the fixed price and percentage coupon modules.

GeduR’s picture

Status:Needs work» Needs review
new4.51 KB

Hi! Here is a patch for this.

I've used the commerce_coupon_review_pane (checkout display) to render the loaded coupons, it's working but maybe it's better to define a new preset. What do you think?

The form item #weight is hardcoded because at the present placeholders are not working with views_handler_area, I will post an issue on views module as soon as posible but meanwhile I think this patch could be a good approach to use coupons on the cart form and always we can use form_alter to modify this property.

EDIT: views related form #weight issue

pcambra’s picture

Category:support» task
Status:Needs review» Needs work

Great work! thanks a lot for this

Gave it a try, code looks mostly fine but there are a couple of things that need work.

As soon as I add the element to the cart, I get this warning:

Strict warning: Declaration of commerce_coupon_handler_area_cart_form::render() should be compatible with that of views_handler_area::render() in _registry_check_code() (line 3088 of /webs/contrib/drupalcommerce/includes/
Strict warning: Declaration of commerce_coupon_handler_area_cart_form::render() should be compatible with that of views_handler_area::render() in _registry_check_code() (line 3088 of /webs/contrib/drupalcommerce/includes/

When you override methods you need to keep the signature of the parent, plus, try to develop with PHP notices enabled.

Other review things

+++ b/includes/views/handlers/
@@ -0,0 +1,97 @@
+    // @NOTE: At the present this is not working with views_handler_area so you need to use form #weight property

No need to add this, if it's a views limitation, we need to focus in fixing it in Views

+++ b/includes/views/handlers/
@@ -0,0 +1,97 @@
+    return '<!--form-item-' . $this->options['id'] . '--1-->';

Why "1"? can't you just omit it?

+++ b/includes/views/handlers/
@@ -0,0 +1,97 @@
+      '#weight' => 99, // @NOTE: At the present placeholders are not working with views_handler_area

It might be a good idea to expose this as a form option for the area

+++ b/includes/views/handlers/
@@ -0,0 +1,97 @@
+    $order_id = reset($this->view->args);

No need to do this, check get_result_entities method in Views (example in commerce_coupon_handler_field_coupon_granted_amount)

+++ b/includes/views/handlers/
@@ -0,0 +1,97 @@
+    $coupon_review_view = views_get_view('commerce_coupon_review_pane');

We're missing a default one here

+++ b/includes/views/handlers/
@@ -0,0 +1,97 @@
+  drupal_set_message(t('Coupon code has been successfully redeemed.'));

Let's not add a message here, we need to find an alternate solution for that

GeduR’s picture

Thanks, here is a new patch.


- Adding $value to render() method.

- Delete @NOTE comment.

- Expose weight as a form option for the area

- check get_result_entities method in Views (example in commerce_coupon_handler_field_coupon_granted_amount)
I can't find out how to do this, field_handler_area hasn't have add_additional_fields() method and get_results_entities do a entity_load too, so I have use the commerce_order_handler_area_order_total handler approach, the entity cache should do its job.

- Use de default views display

- Delete drupal_set_message

pcambra’s picture

Status:Needs work» Needs review
pcambra’s picture

Status:Needs review» Fixed

I can't find out how to do this, field_handler_area hasn't have add_additional_fields() method and get_results_entities do a entity_load too, so I have use the commerce_order_handler_area_order_total handler approach, the entity cache should do its job.

Yeah it seems they haven't, I've posted a patch to fix it: #1321018: move add_additional_fields() up to base Views handler class

Meanwhile, I'm copying the basics of the function so we avoid reloading the order at that stage as it is already there.

Also changed the coupon view at the bottom so it's an option of the handler.
$coupon_review_view = views_get_view('commerce_coupon_review_pane');

And some minor fixes to remove the strict warnings.

It's fine for -dev so I've pushed it, thanks everybody, specially GeduR for taking this to the finish line.

If there are feedbacks/followups please open those as a sepparate issue. Thanks

Status:Fixed» Closed (fixed)

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

josebc’s picture

Issue summary:View changes

Thank you for the patch, i need to use the 2.x of the module
should we create a different issue or submit a patch here?

josebc’s picture

patch for 2.x based on previous

andriy1990’s picture

Status:Closed (fixed)» Active
Issue tags:+cannot test

Hi all. I try to use patch for 2x version. The problem is that I cannot test it to work. When I apply patch what must to do, where do I can config cart view with coupons?Can you provide a simple guide for integrate it in site?

josebc’s picture

@andriy1990 you can add it in the cart view footer

caschbre’s picture

I'm testing out the 2.x patch in #17.

When applying a coupon in the cart form I was seeing the following error:

Warning: Missing argument 3 for commerce_coupon_redeem_coupon_code(), called in //commerce_coupon/includes/views/handlers/ on line 125 and defined in commerce_coupon_redeem_coupon_code() (line 708 of //commerce_coupon/commerce_coupon.module).

It appears that in the function commerce_coupon_handler_area_cart_form_submit($form, $form_state) the following needs to change...

commerce_coupon_redeem_coupon_code($code, $order);


commerce_coupon_redeem_coupon_code($code, $order, $error);

I also noticed that after applying a coupon code that I still see the coupon form and am unable to remove the applied coupon.

caschbre’s picture

Version:7.x-1.x-dev» 7.x-2.x-dev
Status:Active» Needs review
Issue tags:-cannot test
new8.81 KB

Attached is an updated / refactored patch to work with commerce_coupon 2.x. This patch is a combination of the patch from #17 and the logic from Features include:

* Ability to choose what View is used for the coupon summary.
* Fixes some of the bugs with the patch in #17.
* Coupon form is AJAX based.
* Includes more error messages (taken from checkout_pane).

caschbre’s picture

Status:Needs review» Needs work

Found an issue the popped up so setting this back to needs work.

Basically on the ajax refresh the order total is being refreshed with any included discounts, but the line items are not. I'm not exactly sure why. Hitting page refresh fixes it. Any insight would be appreciated.

The ajax is also not clearing the coupon code field. Hmm.

I'm also looking at making the ajax functionality happen only when the view is configured to use ajax, otherwise it will use the standard page refresh.

I'll have another patch out today.

caschbre’s picture

Ok, attached is an updated patch.
* Added validate/submit handlers to allow for page refresh/submit of coupons. This seems to be working pretty good.
* Added logic to only use ajax when view is set to use ajax. Note: I also short-circuited ajax callbacks because that is still not working.
* Updated the error handling.

I'm leaving this as needs work since the ajax logic isn't working. I'm stumped.

We could just remove the ajax code and make this needs review.


caschbre’s picture

FYI... commerce_coupon.module had one minor fix to it since it was applying the coupon even if there was an error. But when saving that file, my text editor removed any extra whitespace. So the .module file is showing as a big patch for that when it's only one minor IF condition.

caschbre’s picture

Attached is an updated path to fix an issue where the option to choose the view to render the coupon summary was not being saved.

philipz’s picture

Great to see this idea being worked on.

I have one question though - wouldn't it be more flexible to add this functionality as a block so that I could place such a block anywhere I want (including cart page) and allow for coupon activation even on product or front pages? Would that be even possible? I'm considering to work on such approach.

caschbre’s picture


I think there can be a lot of refactoring to make the coupon form more reusable. Right now the form is created in two places... a checkout pane and the views footer handler. They're slightly different but the I would think that the main part of the form could be generated in a helper function. Then the form would output the same.

Once that is there, then you can easily create a block that simply renders the form from the helper. Add in any extra stuff needed for the block and we should be golden.

I'd also add in that any validation / submit logic could be reusable to avoid lots of extra code / maintenance.

caschbre’s picture

Note: Patch #25 is generating a php warning.

Strict warning: Declaration of commerce_coupon_handler_area_cart_form::options_validate() should be compatible with views_handler::options_validate(&$form, &$form_state) in require_once() (line 11 of /path/to/commerce_coupon/includes/views/handlers/

This is due to a missing & in the options_validate function of patch #25.

function options_validate($form, &$form_state) {

should be (i'm guessing)

function options_validate(&$form, &$form_state) {

philipz’s picture

@caschbre you're absolutely right there could be some improvement regarding reusability.

I've managed to do this block approach (without ajax triggers) for 1.x version of Commerce Coupon. It's simple and works for me. The block has config settings that allows me to add some info text and a redirect path after adding coupon.

Maybe it would be easier to do the same in your patch? I mean dump the ajax stuff and make it a simple block.

caschbre’s picture

@philipz.... the ajax stuff in my patch isn't working and I haven't had a chance to figure out why. Right now it works through form submits.

The coupon form part could be made into a separate function that's called by the checkout pane, the cart view, and a block. But I'm not sure it could just be a block to make it show up on the cart for properly. But a block could be a wrapper to the reusable code part. At this point I don't have any time to take it further than it is.

caschbre’s picture

new33.8 KB

Here is an update to patch #25 that includes the fix mentioned in #28. It's a whole one character difference. :)

jazzdrive3’s picture


I couldn't apply the patch to the latest dev until I deleted a blank line at the top of the commerce_coupon.module file. Other than that, it works fine with normal submission handling (no ajax)

caschbre’s picture

@jazzdrive3 Thanks!

And if you have any ideas on the ajax stuff, I'm at a roadblock with it. Not sure where it's failing.

caschbre’s picture

new11.08 KB

Here's a reroll of #31 that ignores whitespace so it should apply cleanly.

dpolant’s picture

Category:Task» Feature request

Changing this to a feature request.

capellic’s picture

new4.73 KB

Thanks to @doliveros for the code in #4. I'm guessing this is the code needed for 1.x because I had some issues when trying to get it to work with 2.x. I made the necessary updates -- not many -- but some things had changed. Attached.

Also, the user will have to use the Apply button for the coupon, the "update cart" and "checkout" buttons will not apply the code.

capellic’s picture

new4.66 KB

Sorry, my file in comment #36 has a bug in it that causes the coupon to be applied twice. Here's a better one.

skyredwang’s picture

Status:Needs work» Needs review

Change the status, so we can test #34 patch.

edxxu’s picture

The #34 patch causes a warning: Strict warning: Declaration of commerce_coupon_handler_area_cart_form::render() should be compatible with views_handler_area::render($empty = false) in _registry_check_code() (line 3146 of /var/www/newdisk/lelo/public_html/includes/ with PHP 5.4.

edxxu’s picture

Here is the new patch based on #34, which just fixes a Strict warning under php 5.4.

skyredwang’s picture

Status:Needs review» Reviewed & tested by the community

I tested the patch, it works. The ajax part isn't completed yet, but this could be a new feature in the near future.

caschbre’s picture

Looks good edxxu! We should probably make a separate issue to handle the ajax part. I was never able to get that working.

Simon Georges’s picture

I can confirm the patch works as well. It just doesn't apply properly on current -version (the .info files[] addition refuses to apply, the rest is fine though).

Simon Georges’s picture

tilon’s picture

Patch #40 works for me, thanks!

Simon Georges’s picture

Actually, it's totally fine when used with drush make.

Marko B’s picture

I think part of this patch is not right

I keep getting this message when coupon is applied

EntityMetadataWrapperException: Unable to get the data property commerce_unit_price as the parent data structure is not set. in EntityStructureWrapper->getPropertyValue() (line 438 of /Applications/MAMP/htdocs/pht/sites/all/modules/contrib/entity/includes/

Maybe something like this needs to be applied with hook_commerce_line_item_type_info()

Marko B’s picture

Maybe it is this after all so it is just connected but not really a problem of coupons.

joelpittet’s picture

Status:Reviewed & tested by the community» Needs work
+++ b/includes/views/handlers/
@@ -0,0 +1,257 @@
+  if ($errors = form_get_error('coupon_code')) {
+    drupal_set_message('testing');
+  }

Was this meant to be left in there?

maciej.zgadzaj’s picture

Joel, it was mentioned couple of times above that the ajax part isn't completed yet.

Anyway, attached are re-rolled patches for the latest 7.x-2.x, and additionally for 7.x-2.0-beta4, if someone needs it for project.make (like me :)).

Sebastien @Actualys’s picture

Hi maciej
I've encountered an issue when trying to apply your patch (2.0-beta4) on the ".info" file.
It's breaking my "project.make" script.
I haven't tested the "2.x" release, but it seems to be good.
Many thanks

maciej.zgadzaj’s picture

Hi Sebastien!

You're right, I've created the new patch based on git clone, which doesn't have that extra "information added by ..." at the end of the file - hence the patch fails to apply.

It applies cleanly to git clone though:

projects[commerce_coupon][type] = "module"
projects[commerce_coupon][download][type] = "git"
projects[commerce_coupon][download][url] = ""
projects[commerce_coupon][download][tag] = "7.x-2.0-beta4"
projects[commerce_coupon][patch][] =