You are here

function commerce_discount_get_discounts_applied_to_price in Commerce Discount 7

Returns a list of discounts applied.

Returned list used to make a particular price as determined by its price components.

Parameters

array $price: A price field value array including a data array with price components.

string $type: Optional. A discount type to filter the return array by so that only discounts of a matching type are returned.

Return value

array Array of applied discounts.

3 calls to commerce_discount_get_discounts_applied_to_price()
CommerceDiscountConditionsTest::testCommerceProductContainsProducts in tests/commerce_discount_conditions.test
Test commerce_product_contains_products() condition.
CommerceDiscountTestBase::discountAppliedToOrder in tests/commerce_discount_base.test
Determines whether or not a discount has been applied to an order.
_commerce_discount_check_compatibility in ./commerce_discount.rules.inc
Performs a discount compatibility check for the given price field.

File

./commerce_discount.module, line 999
Defines the discount and discount offer entities, bundles and functionality.

Code

function commerce_discount_get_discounts_applied_to_price($price, $type = '') {
  $applied_discounts = array();

  // Return early if the price has no components.
  if (empty($price['data']['components'])) {
    return array();
  }

  // Loop over the price components looking for known discount components.
  foreach ($price['data']['components'] as $component) {
    if (strpos($component['name'], 'discount|') === 0) {

      // Load the discount entity represented by this component.
      $applied_discount_name = substr($component['name'], 9);
      $applied_discount = entity_load_single('commerce_discount', $applied_discount_name);

      // Add it to the list of applied discounts keyed by ID to prevent
      // duplicates.
      if (!empty($applied_discount)) {

        // Only add the discount to the return value if type filtering was not
        // requested or the type matches.
        if (empty($type) || $applied_discount->type == $type) {
          $applied_discounts[$applied_discount->discount_id] = $applied_discount_name;
        }
      }
    }
  }
  return $applied_discounts;
}