You are here

function rooms_booking_manager_create_order in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7

Creates a Commerce Order for a booking.

Parameters

DateTime $start_date: start date.

DateTime $end_date: end date.

array $booking_parameters: Specify the people we are looking to accommodate.

RoomsUnit $unit: Unit being booked.

RoomsBooking $booking: Booking to attach the new order.

int $profile_id: Customer profile ID for the new order.

Return value

object A commerce order related to the booking.

2 calls to rooms_booking_manager_create_order()
FeatureContext::createBookingsForUnit in test/features/bootstrap/FeatureContext.php
rooms_booking_manager_rooms_booking_edit_form_submit in modules/rooms_booking_manager/rooms_booking_manager.module
Submit handler for the 'rooms_booking_edit' form.

File

modules/rooms_booking_manager/rooms_booking_manager.module, line 2981
Rooms Booking Manager brings together all the pieces required to find a room and book it - including the DrupalCommerce integration

Code

function rooms_booking_manager_create_order($start_date, $end_date, $booking_parameters, $unit, $booking, $profile_id) {
  global $user;
  module_load_include('inc', 'rooms_booking_manager', 'rooms_booking_manager.commerce');
  $uid = isset($booking->uid) ? $booking->uid : $user->uid;
  $order = commerce_order_new($uid, 'pending');
  commerce_order_save($order);
  $price_modifiers = array();

  // Convert Price options to Price modifiers.
  foreach (rooms_unit_get_unit_options($unit) as $option) {
    $opt_name = rooms_options_machine_name($option['name']);
    if (isset($booking->{$opt_name}) && $booking->{$opt_name}) {
      $price_modifiers[$opt_name] = array(
        '#name' => $option['name'],
        '#type' => ROOMS_DYNAMIC_MODIFIER,
        '#op_type' => $option['operation'],
        '#amount' => $option['value'],
        '#quantity' => $option['quantity'],
      );
    }
  }
  $agent = new AvailabilityAgent($start_date, $end_date, $booking_parameters, 1, array());
  $agent
    ->setValidStates(variable_get('rooms_valid_availability_states', array(
    ROOMS_AVAILABLE,
    ROOMS_ON_REQUEST,
  )));

  // Create line item.
  $line_item = rooms_create_line_item(array(
    'unit' => $unit,
    'state' => ROOMS_AVAILABLE,
  ), $agent, $booking_parameters, $price_modifiers);
  $line_item->rooms_booking_reference[LANGUAGE_NONE][0]['target_id'] = $booking->booking_id;
  commerce_line_item_save($line_item);

  // Add the line item to the order.
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $order_wrapper->commerce_line_items[] = $line_item;

  // Add customer profile id to store billing information.
  $profile_object = array(
    LANGUAGE_NONE => array(
      array(
        'profile_id' => $profile_id,
      ),
    ),
  );
  $order->commerce_customer_billing = $profile_object;

  // Save the order again to update its line item reference field.
  commerce_order_save($order);
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
  $line_item_wrapper->order_id = $order->order_id;
  $line_item_wrapper
    ->save();
  commerce_cart_order_refresh($order);
  $line_item_wrapper->commerce_unit_price->amount = $booking->price;
  $data = $line_item_wrapper->commerce_unit_price->data
    ->value();
  $data['components'][0]['price']['amount'] = $booking->price;
  $line_item_wrapper->commerce_unit_price->data = $data;
  $line_item_wrapper
    ->save();
  $order_wrapper->commerce_order_total->amount = $booking->price;
  $data = $order_wrapper->commerce_order_total->data
    ->value();
  $data['components'][0]['price']['amount'] = $booking->price;
  $order_wrapper->commerce_order_total->data = $data;
  $order_wrapper
    ->save();
  return $order;
}