Adding a discount with the Drupal commerce module

2.6k views Asked by At

I am trying to add a discount on the product price depending on where the users are from "filled in the registration form".

I am thinking about fetching the data "state" via hook, and altering the discount depending on the fetched data.

I can't figure out where I can set the value of the price.
I don't think the rules will be much of a help, since all be adding a lot of conditions to it.

1

There are 1 answers

2
Tal V. On BEST ANSWER

I still believe Pricing Rules is the way to go.

Obviously you can't set a condition for each state, as that would result in chaos. What I would suggest is setting up a new table with two columns: State and Discount. Fill the table with the data you need for your project.

Set up a new Pricing Rule (Store -> Configuration -> Product pricing Rules -> List -> Add). Skip the Conditions part. In the Actions part, fetch the user's State data and make it available as a variable. Then add a PHP action to query the table for the relevant discount. Use a third action to set the price to the retrieved discount, or if no results were returned, to the original value.

It might be good practice to wrap up this logic in a tidy module with the following:

  1. A UI for managing the discounts table.
  2. A Rules action for querying the table for a given State.
  3. A Rules condition for checking if there is a discount for a given State.

(You can then use item #3 above in the conditions of the Pricing Rule, to change the price only if a discount is found.)

Hope that helps. Good luck!