Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comments
Comment #1
nielsonm CreditAttribution: nielsonm commentedPatch included
Comment #2
nielsonm CreditAttribution: nielsonm commentedComment #3
twardnw CreditAttribution: twardnw commentedI 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
Comment #4
pcambraI'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.
Comment #5
nielsonm CreditAttribution: nielsonm commented@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
Comment #6
nielsonm CreditAttribution: nielsonm commentedHere'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.
Comment #7
nielsonm CreditAttribution: nielsonm commentedOops, patch included
Comment #8
pcambraThanks 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
Comment #9
nielsonm CreditAttribution: nielsonm commentedI'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?
Comment #10
pcambraYou'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".
Comment #11
nielsonm CreditAttribution: nielsonm commented@pcambra I'd be happy to help out with that, if you could point me in a direction.
Comment #12
pcambra@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)
Comment #13
googletorp CreditAttribution: googletorp commentedI 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
Comment #14
googletorp CreditAttribution: googletorp commentedI've added a patch to fix the views integration which needs to be applied along with the other patch.
Comment #15
pcambraCould we work with a single patch? also, in #13 other issue is referenced, but there's no code in there.