It would be great to be able to apply a coupon code through a link, in a format something like this:
example.com/product_page?code=XXXX
So that when a user clicks the link, they end up on a product display page with a discounted price, instead of a cart view. That way, they have all available information about the product and the discounted price in one place.
I had to do this recently and took this approach:
1. Custom action triggered on "Calculating the sell price of a product":
- looks for the query parameter "code" in the url
- verifies code is valid and active (uses commerce_coupon code)
- applies it to the line item on product landing page
- saves code as a session variable
2. Custom action triggered on "After adding a product to the cart"
- looks for code as a session variable
- verifies code is valid and active
- uses 'commerce_coupon_redeem_coupon' to apply to cart order
- clears the session variable
This works but I'm not sure a session variable is a good way preserve a coupon code that arrived as a query parameter.
It would be nice to have this feature in commerce coupon or commerce discount. In the meantime, I'd be interested in any better ways to achieve this.
Remaining tasks
- Create tests
Comment | File | Size | Author |
---|---|---|---|
#9 | provide_a_coupon_code-2191265-9.patch | 9.11 KB | GoZ |
| |||
#9 | interdiff-2191265-6-9.txt | 786 bytes | GoZ |
Comments
Comment #1
caschbre CreditAttribution: caschbre commentedMy project is also looking to do something similar. The session variable is what we're thinking but haven't implemented it yet. I'm making note of this issue in case we get to a point we can create a patch or offer any other approaches.
Comment #2
GoZ CreditAttribution: GoZ at Centarro commentedInstead of using sessions and params, i prefer another way :
Both sessions and coupon in url params are not the best option :
Patch add a commerce_coupon_url submodule.
Needs https://www.drupal.org/node/2263315 patch to allow coupons to be applied without discount.
We also needs patch from issue https://www.drupal.org/node/2697593 to order coupon form fields.
Comment #3
GoZ CreditAttribution: GoZ at Centarro commentedI add with this patch property metadata wrapper to display coupon path + a views handler.
Comment #4
GoZ CreditAttribution: GoZ at Centarro commentedI miss some files (for views)
Sorry
Comment #5
mdupree CreditAttribution: mdupree as a volunteer and at Acro Commerce commentedI was unable to get this module to work without adding the page arguments,
'page arguments' => array(1, 'code')
to the menu hook.
Am I understanding this correctly, but don't you need the page argument in order to pass the coupon code ?
Comment #6
GoZ CreditAttribution: GoZ at Centarro commentedYou are right, i miss page arguments, but only array(1) is needed.
I'll add tests to this patch
Comment #7
mdupree CreditAttribution: mdupree as a volunteer and at Acro Commerce commentedLooks good +1 here
Comment #8
torgosPizzaSome comments:
Is this needed? It seems redundant, and I don't see commerce_coupon_url_code_load() being called anywhere else.
I think 'discount' needs to be 'discount_coupon'.
This is just after a cursory glance. I haven't tested this yet, but can shortly. Until now we've been using code from #2263315: Allow discount_coupon on orders without the discount and some custom code (and I've even managed to utilize Rules Link for some use cases as well), but this solution looks like it could be cleaner.
Comment #9
GoZ CreditAttribution: GoZ at Centarro commented@torgosPizza There is no way to load coupon from coupon code as url argument. The only one available on commerce_coupon is coupon id. So commerce_coupon_url_code_load() is used to load coupon from coupon code %commerce_coupon_url_code in /coupon/%commerce_coupon_url_code.
You are right for 'discount_coupon'. This code was based on existing commerce_coupon_handler_field_coupon_link.inc handler which should be wrong to.
Comment #10
GoZ CreditAttribution: GoZ at Centarro commentedComment #11
torgosPizzaOh, I get it now. Right, I was thinking it would just be passed in as an argument, but if we can use an autoloading URL argument that would be ideal.
Thanks for the re-roll, looks good to me otherwise. And you're right about the handler for link having incorrect coupon type in its check_access() call, that might need to be a new issue.