1

I'd like to create a Solspace Calendar Event on Commerce onOrderComplete. I've done this with Charge where on Charge Success an event was created however I know that I did that in a less than ideal way so hopefully the answer to this allows me to both achieve what this question is asking as well as fix my previous plugin (that works but just seems less than ideal).

I'd need to pass some of the fields from the Order Fields such as the Start Date, End Date, Products Purchased.

I know i need to create a plugin for this, i'm just not quite sure how to call the Solspace Calendar plugin onOrderComplete and send it the appropriate data to create said event. Any help (with examples if possible) would be greatly appreciated.

To clarify: I am not looking for someone to write a plugin for me. I am simply looking to be put on the right track in terms of connecting to Solspace Calendar and sending it the appropriate data on completion of a commerce order. I can write the code myself once pointed in the right direction.

1 Answer 1

2

To listen to Craft Commerce order save event, you must make a new plugin (or use an existing one) and in it's ::init() method, listen to the commerce_orders.onSaveOrder event:

class TestPlugin extends BasePlugin
{
    public function init()
    {
        parent::init();

        craft()->on(
            "commerce_orders.onSaveOrder",
            function (Commerce_OrderModel $order) {
                // Create the event
                $event = new Calendar_EventModel();

                $event->getContent()->title = "New Event Title";
                $event->slug                = "new-event-title";

                // Any custom fields go through the ::getContent() model
                $event->getContent()->body = "This is a custom rich text field";

                // Do whatever you need to do with the order data
                // By using the $order OrderModel
                // For example:
                //
                // $event->getContent()->body = "Order total: " . $order->totalPrice;

                // Mandatory fields
                $event->calendarId = 1;
                $event->authorId   = craft()->getUser()->id;
                $event->startDate  = new DateTime();
                $event->endDate    = new DateTime();

                $event->allDay = true;

                // Save the event using Calendar Events service ::saveEvent() method
                craft()->calendar_events->saveEvent($event);
            }
        );
    }
}
1
  • Thanks a bunch! This will help me clean up the charge plugin i did as well. Commented Nov 5, 2016 at 1:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.