You are here

function commerce_recurring_rules_set_price in Commerce Recurring Framework 7.2

Action callback to override the listing price by the one in initial price of the recurring framework.

Parameters

$line_item: Commerce line item affected by the price replacement.

$listing_price:

$initial_price:

$recurring_price:

1 string reference to 'commerce_recurring_rules_set_price'
commerce_recurring_rules_action_info in ./commerce_recurring.rules.inc
Implements hook_rules_action_info().

File

./commerce_recurring.rules.inc, line 320
Rules integration for recurring entities.

Code

function commerce_recurring_rules_set_price($line_item, $listing_price, $initial_price, $recurring_price) {

  // If the line item contains a product, we replace the price by the initial
  // price for recurring.
  if (commerce_line_items_quantity(array(
    $line_item,
  ), commerce_product_line_item_types())) {
    $price = array();
    $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
    $product = $line_item_wrapper->commerce_product
      ->value();

    // If the product is recurring we act.
    if (commerce_recurring_product_is_recurring($product)) {
      if (!empty($line_item->order_id)) {
        $order = commerce_order_load($line_item->order_id);
      }
      if (!empty($line_item->data['recurring_entity'])) {
        $recurring_entity = entity_load_single('commerce_recurring', $line_item->data['recurring_entity']);
        $recurring_wrapper = entity_metadata_wrapper('commerce_recurring', $recurring_entity);

        // Allow the prices to be altered by other modules.
        drupal_alter('commerce_recurring_initial_price', $initial_price, $line_item, $recurring_entity);
        drupal_alter('commerce_recurring_recurring_price', $recurring_price, $line_item, $recurring_entity);

        // Case: Recurring price.
        $recurring_orders = $recurring_wrapper->commerce_recurring_order
          ->value();

        // Empty the price components to recalculate them.
        $line_item->commerce_unit_price[LANGUAGE_NONE][0]['data']['components'] = array();
        if (count($recurring_orders) >= 1) {
          $price = array(
            'amount' => $recurring_price['amount'],
            'currency_code' => $recurring_price['currency_code'],
            'data' => $recurring_price['data'],
          );
        }
        else {

          // This would be the first order so we use initial price.
          $price = array(
            'amount' => $initial_price['amount'],
            'currency_code' => $initial_price['currency_code'],
            'data' => $initial_price['data'],
          );
        }
      }
      else {

        // Case: Listing price / Cart price.
        if (empty($line_item->line_item_id) || commerce_cart_order_is_cart($order)) {

          // Empty the price components to recalculate them.
          $line_item->commerce_unit_price[LANGUAGE_NONE][0]['data']['components'] = array();
          $price = array(
            'amount' => $listing_price['amount'],
            'currency_code' => $listing_price['currency_code'],
            'data' => $listing_price['data'],
          );
        }

        //@TODO: // Case: Initial price.

        //@TODO: Fix cart listing when displaying the cart with the product.
      }
      if (!empty($price)) {

        // Alter the base price to the current one.
        $line_item_wrapper->commerce_unit_price->data = commerce_price_component_add($line_item_wrapper->commerce_unit_price
          ->value(), 'base_price', $price, TRUE);
        $line_item_wrapper->commerce_unit_price->amount = $price['amount'];
      }
    }
  }
}