You are here

function commerce_line_item_unit_price_divide in Commerce Core 7

Rules action: divide the unit price by some amount.

1 string reference to 'commerce_line_item_unit_price_divide'
_commerce_line_item_update_rule_round_mode in modules/line_item/commerce_line_item.install
Given a Rule configuration, checks its element name to see if it is an action that requires a value for the new round_mode setting used in unit price manipulation actions.

File

modules/line_item/commerce_line_item.rules.inc, line 221
Rules integration for line items.

Code

function commerce_line_item_unit_price_divide($line_item, $amount, $component_name, $round_mode) {
  if (is_numeric($amount)) {
    $wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
    $unit_price = commerce_price_wrapper_value($wrapper, 'commerce_unit_price', TRUE);

    // Calculate the updated amount and create a price array representing the
    // difference between it and the current amount.
    $current_amount = $unit_price['amount'];
    $updated_amount = commerce_round($round_mode, $current_amount / $amount);
    $difference = array(
      'amount' => $updated_amount - $current_amount,
      'currency_code' => $unit_price['currency_code'],
      '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 = commerce_price_component_add($wrapper->commerce_unit_price
      ->value(), $component_name, $difference, TRUE);
  }
}