I thought it might be a good idea to create a views handler that showed the number of uses remaining for a given coupon, that way there's an option to see how many uses a coupon has and, by extension, how many times it's been used. Might be usable for A/B testing for email campaigns, coupon sites, etc.

Thanks,
Mike

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nielsonm’s picture

nielsonm’s picture

Priority: Normal » Minor
twardnw’s picture

Status: Active » Needs review

I have applied this patch with no issues, and even used the handler in my own view on #1691672: display 'Number of uses' and times used on coupon list

pcambra’s picture

Status: Needs review » Needs work

I'd say that it would be a better way to do this backwards and provide a property for the number of times that a coupon has been used instead of the "remaining" and calculate the latest

We could expose a new property through the entity API and call commerce_coupon_get_number_of_uses for getting the number, this way it will be available for rules and all the modules that rely on entity API and not only for Views.

I'll give it a try in the next couple of days.

nielsonm’s picture

@pcambra,

If I may, would you mind terribly if I took a crack at it this weekend? I've got a little entity API experience, and I'd love to use this as an opportunity to learn more of it. I wouldn't mind direction as to what you'd like to see in a patch. I figure I opened the issue, and I'd like to help. As always, I defer to your judgement and will gladly accept any decision. This module's awesome and I just wanted to give back.

Thanks,
Mike

nielsonm’s picture

Title: Remaining uses views handler » Times used coupon views handler

Here's a revised patch that adds a `times_used` property to the coupon entity. It displays the number of times the coupon has been used.

nielsonm’s picture

pcambra’s picture

Thanks nielsonm, I went ahead and implemented the property as we actually don't need to save the field in the database, we can just query the order with the EFQ from commerce_coupon_get_number_of_uses.

I'm keeping the issue in 'needs work' as we probably need further work on this to add a filter and an argument for times used in views and check all the Rules integrations.

This is the commit: http://drupalcode.org/project/commerce_coupon.git/commit/910342a

nielsonm’s picture

I'm not sure how we could have the number of times used without a database field. Comment counts on nodes are stored in the node_comment_statistic table and then added to the node object around line 1272 in comment.module. Dynamically calculating the number of uses works for view display, but it probably won't work for views filters or arguments. Does views have a way to calculate counts dynamically pre-query?

pcambra’s picture

You're right, for having filter and arguments we probably need a statistic table, but as I'm saying in other issues, I'd rather focus in getting a Coupons 2.x branch working relying on Commerce Discounts wich already includes a usage submodule.

I'd be happy to push something similar to #7 if it improves what we've got now and includes some sort of "migration".

nielsonm’s picture

@pcambra I'd be happy to help out with that, if you could point me in a direction.

pcambra’s picture

@nielsonm your approach in #7 is the right way as you pointed out in #9, what's missing here is to provide an update path, adding the field to existing installs and populate it with the right values.

Also, instead of creating a field handler, we should rely on Entity API (somehow like the code committed already but without the extra getter)

googletorp’s picture

I added the code needed to store the number of uses on the coupon.

Note it uses the code from #1838216: Make a subclass of the Entity class and use it for the commerce coupon entity

googletorp’s picture

I've added a patch to fix the views integration which needs to be applied along with the other patch.

pcambra’s picture

Could we work with a single patch? also, in #13 other issue is referenced, but there's no code in there.