On Commerce Kickstart 2.x we are trying to #2245341: Actually create the discounts stated on the frontpage of Demo Store. I am looking into "SAVE 25% Purchases made between June 5 - 12 will be discounted". It is easy enough to just create it in the interface and export using features. But I want to create the discount programmatically so it can be installed during the install process like all the other demo content.
How can this be achieved?
Comments
Comment #1
lsolesen commentedRelated to #2174581: Creating a commerce discount programmatically
Comment #2
mglamanLooks like you'd have to create the entity object and just save it. On entity insert it flushes the Rules config.
In rules_defaults.inc you have the below which adds the proper rules.
Comment #3
discipolo commentedi think the question was precisely how to create and save commerce_discount objects in a profile or module .install file (while installing the profile or enabling a module. it seems discounts exported with features dont get saved completely - although they do get created - the corresponding rules only get created by resaving them manually)
Comment #4
tobiasbComment #5
andrews501 commentedI've been trying to create discounts programatically using rules/components (new product creates its discount) but it seems the option for creating a commerce discount entity doesnt exist. At the moment it seems that creating a discount can be done manually only as it needs filling : admin title, name, choose discount type, Order/Product discount conditions, choose offer type, discount dates and coupon conditions.
The nearest option is the current discount offer entity (fills in choose offer type only) but lacks of many of the previous fields. Also the commerce product entity has the option of adding the discount programatically as Discount Price but it is as a fixed amount.
Any suggestions, please?
Comment #6
briantes commentedHello. I could do it with the next code for percentage discount. You can change it for other discounts types:
Hope it can help you.
Comment #7
smichel commented@briantes, this code works really well, thanks.
You can add conditions to the discount like this, where I have a product family taxonomy, and different discounts apply to different products:
And I also added a coupon with this:
In my case, I'm importing discounts from Salesforce, and have this working now. Thanks again.
Comment #8
joelpittetA great source of how to write programmatically is through the simpletests. They try to be succinct to show a certain bit of functionality to ensure it continues to work. They are recently working and enabled on commerce_discount and I'd implore you to use them.
Comment #10
hiramanpatilHi briantes,
I was trying your code to create new fixed discount. Below is the updated code for fixed discount.
But its not working. is there anything wrong with second line in above code?
Thanks
Comment #11
Anonymous (not verified) commented@hiramanpatil
If you're setting up a fixed amount discount - it uses different field indexes, not value to store its data.
Try this out.
Comment #12
hiramanpatilThank you @rsmylski
I have used ['amount'] instead of ['value'] and it works.
Comment #13
sardis commented@smichel, so i've been trying to add conditions to the coupon same way as you do with discounts:
but it returns me an error after i try to save coupon:
Unsupported operand types in sites/all/modules/contrib/commerce_coupon/commerce_coupon.module on line 592
I know, it's not quite correct place to ask, but could you help me with this?
Comment #14
kiss.jozsef commentedHey @sardis regarding your issue https://www.drupal.org/node/2784885#comment-11516675