As I mentioned in my last post spree-extend, how to create custom promotion rule(s) while working with Spree based e-Commerece website. Last weekend, I faced a situation where I need to update the existing promotion rule to handle some specific condition.
Scenario
For example, when I select “Product(s)” spree default rule, it gives us the option to choose products manually or choose by product group and its working as expected. Later, my client told me he wants, when he selects by product_group he can actually select some Taxons/Tags so that only the products belongs to that product_group and has taxon(s) listed there are eligible for this promotion.
Solution
For updating any existing promotion rules in spree there are few generic steps we need to follow,
- Extend the corresponding spree->promotion->rules model class. Here, I created a file
app/models/promotion/rules/product_decorator.rb
|
|
- Create the attribute accessors/ accessibles for new attributes you needed.
|
|
- update the corresponding View file for the same rule in admin/promotions/rules/_product.html.haml or .erb if you are using erb. Add the required code for handling the same. In My case I added one TokenInput object to get multiple taxons selection. Add code something like this… where-ever you think is more suitable for you.
|
|
There are 2 different ways of adding this to view file. One, you can write a decorator and tell before/after which object you want to put this code. Second, you can re-write the complete file and do whatever you need. Upto you.
Also, taxon_picker_field
is a helper method which I created for creating the tokenInput/tokenizer
object. Create the AdminBaseHelper
decorator file, if not exists and put the following code or something like that,
|
|
- Update the eligible? and/or eligible_products methods as per your rule updations. Here in my case I just need to change the eligible_products method. Add the following code in
app/models/promotion/rules/product_decorator.rb
|
|
Or something like that as per your requirements
And yes, We are good to go and the new rule criteria will be applied from now on-wards. Good luck, In case of any query, feel free to ask here or email me at Sandeep Kumar