putyourlightson / craft-campaign

Send and manage email campaigns, contacts and mailing lists in Craft CMS.
https://putyourlightson.com/plugins/campaign
Other
63 stars 25 forks source link

Integration with Commerce 5 coupon codes #501

Open bartrylant opened 1 month ago

bartrylant commented 1 month ago

Feature Request

In Commerce 5 you can bulk create coupon codes. It would be very nice if there was a way to send individual coupon codes to every mailing list subscriber, so every coupon code can only be used by the person who received the mail (instead of sending out generic coupon codes). So the FR is: make an integration with Commerce 5 coupon codes.

Plugin Version

3.5.2

bencroker commented 1 month ago

This should be relatively straightforward to achieve via a custom plugin/module by listening to one of the events triggered before sending an email. In this example, you would use __COUPON_CODE__ in your campaign template and replace it with a newly created discount code.

use putyourlightson\campaign\events\SendoutEmailEvent;
use putyourlightson\campaign\services\SendoutsService;
use yii\base\Event;

Event::on(SendoutsService::class, SendoutsService::EVENT_BEFORE_SEND_EMAIL,
    function(SendoutEmailEvent $event) {
        $couponCode = $this->createDiscountCode();
        $event->message = str_replace('__COUPON_CODE__', $couponCode, $event->message);
    }
);