You are here

function commerce_recurring_initialise_recurring in Commerce Recurring Framework 7

Rules action: mark an order as a recurring master

Parameters

$order object: The order object

File

./commerce_recurring.rules.inc, line 269

Code

function commerce_recurring_initialise_recurring($order) {

  // Wrap the order for easy access to field data.
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $interval = FALSE;
  foreach ($order_wrapper->commerce_line_items as $line_item) {

    // Load and validate the specified product ID.
    $product = $line_item->commerce_product
      ->value();
    if ($product->type != 'recurring') {

      // We don't need this item
      continue;
    }

    // We've got an interval
    $interval = field_get_items('commerce_product', $product, 'commerce_recurring_interval');
    break;
  }
  if (!empty($interval)) {
    $due_dt = new DateObject('now');
    interval_apply_interval($due_dt, $interval[0]);
    $order->commerce_recurring_next_due[LANGUAGE_NONE][0]['value'] = $due_dt
      ->format('U');

    // Save the updated order.
    commerce_order_save($order);

    // Invoke rules event to notify others that the new order has been created
    rules_invoke_all('commerce_recurring_rules_event_initialise_order', $order);

    // Log it
    watchdog('Commerce Recurring', 'Initialised order @id as new recurring master.', array(
      '@id' => $order->order_id,
    ), WATCHDOG_NOTICE);
  }
}