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?