0

I've been working on some custom plugins recently to handle various bits of API functionality for the project I'm working on, and I'm now roughly familiar with Craft's OO approach to carrying out this kind of task, e.g:

$product = new Product();
$product->title = "Sample Product";
// set other properties here
Craft::$app->elements->saveElement($product);

What I'm now trying to do is programatically generate discount codes (i.e. when a user performs a certain action, like signing up to the mailing list). I can hook into that event no problem, and I can even send out an email - it's just the actual creation of a Discount entity that I'm a bit stumped by.

Looking at the Class Reference for Products, which I can create successfully, I can see that what I'm ideally looking for is a Model, however this doesn't seem to exist for Discounts.

I think, therefore, what I'm looking for is the Discount Adjuster as that seems to be what this type of entity is called, however I'm not having much luck creating one. For example I would expect to see "Coupon Code" or similar as a field on this adjuster, but I don't see anything like that.

So the question is - how can I programatically create discounts with coupon codes (and whatever other options I would normally choose in the CP) in my custom plugin's controller action? I appreciate my problem is less to do with Craft and more my general rustiness with OOP... but hopefully this isn't a totally ridiculous request.

Thanks very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.