You are here

function commerce_product_bundle_set_price in Commerce Product Bundle 7

Same name and namespace in other branches
  1. 7.2 commerce_product_bundle.rules.inc \commerce_product_bundle_set_price()

Sets the price of a line item.

Parameters

obj $line_item: The line item object of which we set the price.

float $amount: The value of the new price.

File

./commerce_product_bundle.rules.inc, line 312
Rules integration for product bundle.

Code

function commerce_product_bundle_set_price($line_item, $amount) {
  if (is_numeric($amount)) {
    $wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);

    // Calculate the updated amount and create a price array representing the
    // difference between it and the current amount.
    $current_amount = $wrapper->commerce_unit_price->amount
      ->value();
    $updated_amount = $amount;
    $wrapper->commerce_unit_price->data = commerce_price_component_delete($wrapper->commerce_unit_price
      ->value(), 'base_price');
    $difference = array(
      'amount' => $updated_amount,
      'currency_code' => $wrapper->commerce_unit_price->currency_code
        ->value(),
      'data' => array(),
    );

    // Set the amount of the unit price and add the difference as a component.
    $wrapper->commerce_unit_price->amount = $updated_amount;
    $wrapper->commerce_unit_price->data = array();
    $wrapper->commerce_unit_price->data = commerce_price_component_add($wrapper->commerce_unit_price
      ->value(), 'base_price', $difference, TRUE);
  }
}