You are here

function commerce_registration_item_can_register in Commerce Registration 7

Same name and namespace in other branches
  1. 7.2 commerce_registration.rules.inc \commerce_registration_item_can_register()

Condition callback.

Checks if the line item product has registration capacity left. Capacity is only taken if an order is paid for in full.

Parameters

$line_item: Commerce Line Item to check the product for registration capacity.

Return value

Boolean TRUE if the product has registration capacity.

Related topics

1 string reference to 'commerce_registration_item_can_register'
commerce_registration_rules_condition_info in ./commerce_registration.rules.inc
Implements hook_rules_condition_info().

File

./commerce_registration.rules.inc, line 243
Commerce Registration rules file.

Code

function commerce_registration_item_can_register($line_item) {
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
  $entity = array(
    'id' => $line_item_wrapper->commerce_product->product_id
      ->value(),
    'type' => 'commerce_product',
    'bundle' => $line_item_wrapper->commerce_product->type
      ->value(),
  );
  $settings = registration_entity_settings($entity);
  if (!is_array($settings)) {
    $settings = unserialize($settings);
  }
  $capacity = $settings['settings']['capacity'];
  $startdate = $settings['settings']['open_date'];
  $enddate = $settings['settings']['close_date'];

  // $datesOk checks to make sure we are within the date range of registration.
  $datesOk = FALSE;

  // We first check to see if a date is even set for opening and closing
  // If not, then registrations are open always.
  if ($enddate == 0 && $startdate == 0) {
    $datesOk = TRUE;
  }
  else {
    if (REQUEST_TIME >= $startdate && REQUEST_TIME <= $enddate) {
      $datesOk = TRUE;
    }
  }

  // Next we see if we are within the opening and closing dates, or if they are
  // always open.
  if ($datesOk) {
    if ($capacity != 0) {
      $avail = registration_entity_slots_available($entity);
      if ($avail > 0) {

        // If there are slots available, we see if the quantity puts us beyond
        // the capacity.
        if ($line_item_wrapper->quantity
          ->value() > $avail) {

          // The quantity requested is greater than available slots.
          return FALSE;
        }

        // There are slots available for the requested quantity.
        return TRUE;
      }
      else {

        // There are no slots available.
        return FALSE;
      }
    }
    else {

      // No registration capacity set. Always allow.
      return TRUE;
    }
  }
  return FALSE;
}