You are here

function commerce_node_checkout_expire_get_node_last_line_item in Commerce Node Checkout 7

Fetch most current line item associated with a node.

Parameters

$node: The node object.

Return value

The associated line item object, otherwise NULL if none exist.

4 calls to commerce_node_checkout_expire_get_node_last_line_item()
commerce_node_checkout_expire_adjust_expiration_access in commerce_node_checkout_expire/commerce_node_checkout_expire.module
Access callback to adjust a node's expiration date.
commerce_node_checkout_expire_adjust_expiration_form in commerce_node_checkout_expire/commerce_node_checkout_expire.pages.inc
The node expiration adjustment form.
commerce_node_checkout_expire_get_node_expiration in commerce_node_checkout_expire/commerce_node_checkout_expire.module
Get the expiration time for a given node.
commerce_node_checkout_expire_relist_access in commerce_node_checkout_expire/commerce_node_checkout_expire.module
Access callback to check associated node is able to be relisted.
1 string reference to 'commerce_node_checkout_expire_get_node_last_line_item'
CommerceNodeCheckoutExpireTests::getNodeExpiration in commerce_node_checkout_expire/commerce_node_checkout_expire.test
Get a node's expiration date, if one is set.

File

commerce_node_checkout_expire/commerce_node_checkout_expire.module, line 435
Provides core hooks and the like for the module.

Code

function commerce_node_checkout_expire_get_node_last_line_item($node) {
  $items =& drupal_static(__FUNCTION__, array());

  // Check the static cache
  if (!isset($items[$node->nid])) {

    // Initialize the static cache
    $items[$node->nid] = NULL;

    // Build the query
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', 'commerce_line_item');
    $query
      ->entityCondition('bundle', 'commerce_node_checkout');
    $query
      ->fieldCondition('commerce_node_checkout_node', 'target_id', $node->nid);
    $query
      ->fieldOrderBy('commerce_node_checkout_expires', 'value', 'DESC');

    // We only need the one that expires last.
    $query
      ->range(0, 1);

    // Execute the query
    $results = $query
      ->execute();

    // See if we were able to return a line item
    if (!empty($results['commerce_line_item'])) {

      // Load the line item
      $items[$node->nid] = commerce_line_item_load(key($results['commerce_line_item']));
    }
  }
  return $items[$node->nid];
}