You are here

function commerce_cart_order_can_refresh in Commerce Core 7

Checks if a cart order should be refreshed based on the shopping cart refresh settings on the order settings form.

Parameters

$order: The cart order to check.

Return value

Boolean indicating whether or not the cart order can be refreshed.

1 call to commerce_cart_order_can_refresh()
commerce_cart_commerce_order_load in modules/cart/commerce_cart.module
Implements hook_commerce_order_load().

File

modules/cart/commerce_cart.module, line 711
Implements the shopping cart system and add to cart features.

Code

function commerce_cart_order_can_refresh($order) {
  $can_refresh = module_invoke_all('commerce_cart_order_can_refresh', $order);

  // We refresh the cart order if both of the following conditions are met:
  // - No modules say to skip the cart refresh.
  // - At least one module allows it.
  // If no module specified either allow or skip, we fall back to the
  // logic below.
  if (in_array(FALSE, $can_refresh, TRUE)) {
    return FALSE;
  }
  elseif (in_array(TRUE, $can_refresh, TRUE)) {
    return TRUE;
  }
  global $user;

  // Force the shopping cart refresh on /cart and /checkout/* paths if enabled.
  if (variable_get('commerce_cart_refresh_force', TRUE) && (current_path() == 'cart' || strpos(current_path(), 'checkout/') === 0)) {
    return TRUE;
  }

  // Prevent refresh for orders that don't match the current refresh mode.
  switch (variable_get('commerce_cart_refresh_mode', COMMERCE_CART_REFRESH_OWNER_ONLY)) {
    case COMMERCE_CART_REFRESH_OWNER_ONLY:

      // If the order is anonymous, check the session to see if the order
      // belongs to the current user. Otherwise just check that the order uid
      // matches the current user.
      if ($order->uid == 0 && !commerce_cart_order_session_exists($order->order_id)) {
        return FALSE;
      }
      elseif ($order->uid != $user->uid) {
        return FALSE;
      }
      break;
    case COMMERCE_CART_REFRESH_ACTIVE_CART_ONLY:

      // Check to see if the order ID matches the current user's cart order ID.
      if (commerce_cart_order_id($user->uid) != $order->order_id) {
        return FALSE;
      }
      break;
    case COMMERCE_CART_REFRESH_ALWAYS:
    default:

      // Continue on if shopping cart orders should always refresh.
      break;
  }

  // Check to see if the last cart refresh happened long enough ago.
  $seconds = variable_get('commerce_cart_refresh_frequency', COMMERCE_CART_REFRESH_DEFAULT_FREQUENCY);
  if (!empty($seconds) && !empty($order->data['last_cart_refresh']) && REQUEST_TIME - $order->data['last_cart_refresh'] < $seconds) {
    return FALSE;
  }
  return TRUE;
}