You are here

function uc_price in Ubercart 6.2

The heart of the price modification/displaying system, this function handles price alteration, formatting, theming, and caching.

Parameters

$price_info: Either a simple price to alter/format or an associative array containing the following keys:

  • 'price' => the price per item
  • 'qty' => a quantity
  • 'placeholder' => a placeholder string to use as the formatted price value in lieu of actually formatting the altered price value

$context: An associative array containing information about where the function call came from. This function will at least add an 'account' key to the array if it is not already set with the value being the current user object. A revision may also be specified, accepted values follow:

'revision': Default: 'themed'

  • 'original' => Original price value,
  • 'altered' => Original price passed through the alterer(s),
  • 'formatted-original' => Original price passed through the formatter,
  • 'formatted' => Altered price passed through the formatter,
  • 'themed-original' => Formatted original price passed through the theme layer,
  • 'themed' => Formatted altered price passed through the theme layer.

$options: An associative array containing options that will be passed through to any alteration/formatting/theming functions implemented. A list of accepted options follows.

'sign': Default: variable_get('uc_currency_sign', '$') The sign to use when formatting this price.

'sign_after': Default: variable_get('uc_sign_after_amount', FALSE) If set to TRUE, the sign will come after the price.

'prec': Default: variable_get('uc_currency_prec', 2) Precision to round the price to.

'dec': Default: variable_get('uc_currency_dec', '.') Decimal separator.

'thou': Default: variable_get('uc_currency_thou', ',') Thousand separator.

'label': Default: TRUE If set to TRUE, themed prices will include any prefixes and suffixes.

90 calls to uc_price()
hook_cart_display in docs/hooks.php
Controls the display of an item in the cart.
hook_uc_payment_entered in docs/hooks.php
Takes action when a payment is entered for an order.
op_products_customer_table in uc_order/uc_order.order_pane.inc
Builds the order customer's view products table.
op_products_view_table in uc_order/uc_order.order_pane.inc
Builds the order view products table.
theme_cart_review_table in uc_cart/uc_cart_checkout_pane.inc
Formats the cart contents table on the checkout page.

... See full list

3 string references to 'uc_price'
uc_product_handler_field_price::options_form in uc_product/views/uc_product_handler_field_price.inc
Overrides views_handler::options_form().
uc_product_handler_field_price::option_definition in uc_product/views/uc_product_handler_field_price.inc
Overrides views_handler::option_definition().
uc_product_handler_field_price::render in uc_product/views/uc_product_handler_field_price.inc
Overrides views_handler_field::render().

File

uc_store/includes/uc_price.inc, line 64
Price handling functions and hooks.

Code

function uc_price($price_info, $context = array(), $options = array()) {
  global $user;

  // If we're passed just a number for price, we'll set the quantity to 1.
  if (is_numeric($price_info)) {
    $price_info = array(
      'price' => $price_info,
      'qty' => 1,
    );
  }
  elseif (!is_array($price_info)) {
    $price_info = array(
      'price' => 0,
      'qty' => 1,
    );
  }

  // Initialize the context.
  $context += array(
    'revision' => 'themed',
    'type' => 'amount',
  );

  // Clamp to allowed revisions.
  $revisions = array(
    'original',
    'altered',
    'formatted-original',
    'formatted',
    'themed-original',
    'themed',
  );
  if (!in_array($context['revision'], $revisions)) {
    $context['revision'] = 'themed';
  }

  // Calculate the original price.
  $original = $price_info['price'] * $price_info['qty'];

  // Exit early if the original price was requested.
  if ($context['revision'] == 'original') {
    return $original;
  }

  // Get all the active handlers.
  $handlers = _uc_price_get_handlers($options);
  $formatter = $handlers['formatter'];

  // Use the global user if none was passed in.
  if (!isset($context['account'])) {
    $context['account'] = $user;
  }

  // Merge any incoming options, giving them precedence.
  $options += $handlers['options'];

  // Exit early if no alterations are needed.
  switch ($context['revision']) {
    case 'formatted-original':
      return $formatter($original, $options);
    case 'themed-original':
      return theme('uc_price', $formatter($original, $options), $context, $options);
  }

  // Alter the price, context, and options.
  foreach ($handlers['alterers'] as $alterer) {
    $alterer($price_info, $context, $options);
  }
  $altered = $price_info['price'] * $price_info['qty'];
  if ($context['revision'] == 'altered') {
    return $altered;
  }

  // Use a price placeholder if specified.
  if (isset($price_info['placeholder'])) {
    $formatted = $price_info['placeholder'];
  }
  else {
    $formatted = $formatter($altered, $options);
  }

  // Return the requested revision.
  switch ($context['revision']) {
    case 'formatted':
      return $formatted;
    case 'themed':
      return theme('uc_price', $formatted, $context, $options);
  }
}