How to preserve discount set with query parameters into cart? Using Drupal Commerce, I've been experimenting with applying a simple discount to a product using query string parameters. The goal is to allow a user to navigate to a product display page and see a discounted price based on this syntax:

http://example.com/product_display?code=discount_code

Where 'discount_code' represents some kind of discount to apply to the product.

By writing a custom action that's triggered on the event "Calculating the sell price of a product", it's pretty easy to alter the price of a line item with rules with something like:

function mymodule_apply_discount_via_url($line_item) {

  if ((isset($_GET['code'])) {

    // code to figure out if code is valid

    // code to figure out type of discount

    // code to alter the unit price and add difference as a component


Which works fine - the user will see the discounted price on the product display page. This is nice, because the user has access to all product information along with the discounted price.

However, on "add to cart", the product will appear in the cart at the original price. I assume this is because the pricing rules are run again but without the query parameter.

How can I preserve a discount applied this way through the add to cart step?
My current implementation:


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)
applies it to the line item on product landing page
saves code as a session variable

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 seems to work, though it seems a bit rube goldberg. Still looking for a better way to preserve a discount that arrived as a query parameter, and apply it to a cart order, if there is one.