You are here

function commerce_pricelist_rate_rules_apply in Commerce Pricelist 7

Rules action: loads and applies a pricelist rate to the given line item.

1 string reference to 'commerce_pricelist_rate_rules_apply'
commerce_pricelist_rules_action_info in ./commerce_pricelist.rules.inc
Implements hook_rules_action_info().

File

./commerce_pricelist.rules.inc, line 87
Rules integration for line items.

Code

function commerce_pricelist_rate_rules_apply($line_item, $user = FALSE) {
  $commerce_pricelist_price_array = array();

  // @todo commerce_product is maybe just one of many possible product types?
  $product_id = $line_item->commerce_product['und'][0]['product_id'];
  if ($product_id) {

    // We have a regular commerce product.
    $quantity = $line_item->quantity;
    $product = commerce_product_load($product_id);
    $sku = $product->sku;

    // Default to current user
    if (!$user) {
      global $user;
    }
    $time = $line_item->created == "" ? FALSE : $line_item->created;
    $pricelist_price = commerce_pricelist_get_user_price($user, $sku, $quantity, $time);

    // If pricelist was applied, return the price array as a new variable for use in
    // subsequent actions.
    if ($pricelist_price) {
      $commerce_pricelist_price_array[] = $pricelist_price;
    }
  }
  return array(
    'commerce_pricelist_price_array' => $commerce_pricelist_price_array,
  );
}