Answers.Body: <p>To listen to Craft Commerce order save event, you must make a new plugin (or use an existing one) and in it's <code>::init()</code> method, listen to the <code>commerce_orders.onSaveOrder</code> event:</p>
  
  <pre><code>class TestPlugin extends BasePlugin
  {
      public function init()
      {
          parent::init();
  
          craft()-&gt;on(
              "commerce_orders.onSaveOrder",
              function (Commerce_OrderModel $order) {
                  // Create the event
                  $event = new Calendar_EventModel();
  
                  $event-&gt;getContent()-&gt;title = "New Event Title";
                  $event-&gt;slug                = "new-event-title";
  
                  // Any custom fields go through the ::getContent() model
                  $event-&gt;getContent()-&gt;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-&gt;getContent()-&gt;body = "Order total: " . $order-&gt;totalPrice;
  
                  // Mandatory fields
                  $event-&gt;calendarId = 1;
                  $event-&gt;authorId   = craft()-&gt;getUser()-&gt;id;
                  $event-&gt;startDate  = new DateTime();
                  $event-&gt;endDate    = new DateTime();
  
                  $event-&gt;allDay = true;
  
                  // Save the event using Calendar Events service ::saveEvent() method
                  craft()-&gt;calendar_events-&gt;saveEvent($event);
              }
          );
      }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-05T01:09:26.037Z
Answers.Text: Thanks a bunch! This will help me clean up the charge plugin i did as well.
Answers.UserDisplayName: Christopher Healey
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-04T19:46:17.433Z
Answers.LastActivityDate: 2016-11-04T19:46:17.433Z
Answers.UserDisplayName: Solspace
Body: <p>I'd like to create a Solspace Calendar Event on Commerce <code>onOrderComplete</code>. 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). </p>
  
  <p>I'd need to pass some of the fields from the Order Fields such as the Start Date, End Date, Products Purchased.</p>
  
  <p>I know i need to create a plugin for this, i'm just not quite sure how to call the Solspace Calendar plugin <code>onOrderComplete</code> and send it the appropriate data to create said event. Any help (with examples if possible) would be greatly appreciated.</p>
  
  <p>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.</p>
  
Tags: <plugin-development><plugin-craftcommerce><events><plugin-charge><plugin-solspacecalendar>
Title: Create Solspace Calendar Event on Commerce onOrderComplete
ThreadText: # Create Solspace Calendar Event on Commerce onOrderComplete
  
  (Asked by: username_0 on 2016-11-02)
  
  I'd like to create a username_1 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 username_1 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 username_1 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.
  
  **Tags:** plugin-development, plugin-craftcommerce, events, plugin-charge, plugin-solspacecalendar
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-11-04. Score: 2 | Accepted answer
  
  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);
              }
          );
      }
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks a bunch! This will help me clean up the charge plugin i did as well.  
  
  ---
  
  