You are here

function commerce_coupon_discount_extract_coupons in Commerce Coupon 7.2

Fetches the coupons related to a discount from an order.

If the bool parameter is set the function will return TRUE if there are any valid coupons for the discount on the order.

Parameters

EntityMetadataWrapper $order_wrapper: The wrapped order.

string $discount_name: The discount name to look for.

bool $bool: Whether or not to return just a boolean to indicate that there's a valid coupon for the discount on the order.

Return value

array|bool By default a list of valid coupons on the order for the discount is returned. But If the bool parameter is set the function will return a boolean to indicate if there are any valid coupons on the order.

2 calls to commerce_coupon_discount_extract_coupons()
commerce_coupon_discount_coupon_codes_exist_on_order in ./commerce_coupon.rules.inc
Rules condition callback.
commerce_coupon_discount_get_coupons_list in ./commerce_coupon.rules.inc
Assert the list of coupons related to this discount.

File

./commerce_coupon.rules.inc, line 155
Rules definitions for Commerce Coupon.

Code

function commerce_coupon_discount_extract_coupons($order_wrapper, $discount_name, $bool = FALSE) {
  $discount_coupons = array();

  // Create list of coupons related to this discount if the condition matches.
  $discount_wrapper = entity_metadata_wrapper('commerce_discount', $discount_name);

  // It is possible for the order to be null. This happens if we are dealing
  // with a dummy line item created just to run pricing rules. In this case we
  // do not let the discount proceed.
  if (!$order_wrapper
    ->value() || !$discount_wrapper
    ->value() || !commerce_coupon_order_allows_coupons($order_wrapper
    ->value())) {
    if ($bool) {
      return FALSE;
    }
    return $discount_coupons;
  }

  // Determine if this discount uses coupons. If not, this condition passes.
  if (!$discount_wrapper->coupon_count
    ->value()) {
    if ($bool) {
      return TRUE;
    }
    return $discount_coupons;
  }
  if ($order_wrapper->commerce_coupons
    ->value()) {
    foreach ($order_wrapper->commerce_coupons as $coupon_wrapper) {
      if (!$coupon_wrapper
        ->value()) {
        continue;
      }
      $code = $coupon_wrapper->code
        ->value();
      $discount_id = $discount_wrapper->discount_id
        ->value();

      // The condition passes if the code grants the discount being looked at,
      // and if the coupon-specific conditions pass.
      if (commerce_coupon_code_grants_discount($code, $discount_id) && commerce_coupon_evaluate_conditions($coupon_wrapper, $order_wrapper)) {
        $discount_coupons[] = $coupon_wrapper
          ->getIdentifier();
        if ($bool) {
          return TRUE;
        }
      }
    }
  }
  if ($bool) {
    return FALSE;
  }
  return $discount_coupons;
}