You are here

function commerce_product_calculate_sell_price in Commerce Core 7

Returns the calculated sell price for the given product.

Parameters

$product: The product whose sell price will be calculated.

$precalc: Boolean indicating whether or not the pre-calculated sell price from the database should be requested before calculating it anew.

Return value

A price field data array as returned by entity_metadata_wrapper().

5 calls to commerce_product_calculate_sell_price()
CommerceTaxUIAdminTest::testCommerceTaxUIApplyVAT in modules/tax/tests/commerce_tax_ui.test
Check if a 'VAT' tax type is correctly applied in a given product.
CommerceTaxUIAdminTest::testCommerceTaxUIApplyVATInclusive in modules/tax/tests/commerce_tax_ui.test
Check if a 'VAT' tax type is correctly applied in a given product.
commerce_cart_add_to_cart_form in modules/cart/commerce_cart.module
Builds an Add to Cart form for a set of products.
commerce_product_pricing_commerce_price_field_formatter_prepare_view in modules/product_pricing/commerce_product_pricing.module
Implements hook_commerce_price_field_formatter_prepare_view().
hook_commerce_price_field_formatter_prepare_view in modules/price/commerce_price.api.php
Functions as a secondary hook_field_formatter_prepare_view() for price fields, allowing modules to alter prices prior to display.
3 string references to 'commerce_product_calculate_sell_price'
commerce_product_pricing_invoke in modules/product_pricing/commerce_product_pricing.module
Invokes the sell price calculation callback configured via the "Price calculation process" option.
commerce_product_pricing_ui_form_commerce_product_pricing_ui_add_pricing_rule_form_alter in modules/product_pricing/commerce_product_pricing_ui.module
Implements hook_form_FORM_ID_alter().
commerce_product_pricing_ui_sell_price_rules in modules/product_pricing/includes/commerce_product_pricing_ui.admin.inc
Builds the product sell price calculation Rules Overview page.

File

modules/product_pricing/commerce_product_pricing.module, line 116
Enables Rules based product sell price calculation for dynamic product pricing.

Code

function commerce_product_calculate_sell_price($product, $precalc = FALSE) {

  // First create a pseudo product line item that we will pass to Rules.
  $line_item = commerce_product_line_item_new($product);

  // Allow modules to prepare this as necessary.
  drupal_alter('commerce_product_calculate_sell_price_line_item', $line_item);

  // Attempt to fetch a database stored price if specified.
  if ($precalc) {
    $module_key = commerce_product_pre_calculation_key();
    $result = db_select('commerce_calculated_price')
      ->fields('commerce_calculated_price', array(
      'amount',
      'currency_code',
      'data',
    ))
      ->condition('module', 'commerce_product_pricing')
      ->condition('module_key', $module_key)
      ->condition('entity_type', 'commerce_product')
      ->condition('entity_id', $product->product_id)
      ->condition('field_name', 'commerce_price')
      ->execute()
      ->fetchObject();

    // If a pre-calculated price was found...
    if (!empty($result)) {

      // Wrap the line item, swap in the price, and return it.
      $wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
      $wrapper->commerce_unit_price->amount = $result->amount;
      $wrapper->commerce_unit_price->currency_code = $result->currency_code;

      // Unserialize the saved prices data array and initialize to an empty
      // array if the column was empty.
      $result->data = unserialize($result->data);
      $wrapper->commerce_unit_price->data = !empty($result->data) ? $result->data : array();
      return $wrapper->commerce_unit_price
        ->value();
    }
  }

  // Invoke the callback specified via the "Product pricing process" option.
  commerce_product_pricing_invoke($line_item);
  return entity_metadata_wrapper('commerce_line_item', $line_item)->commerce_unit_price
    ->value();
}