You are here

function _commerce_coupon_free_shipping_single_discount in Commerce Coupon 7.2

Determine whether a coupon grants free shipping.

Parameters

object $coupon: A coupon entity.

bool $exclusive: If set, this function will return TRUE only if the caller is requesting only coupons that exclusively grant free shipping.

Return value

object|bool|void Either a discount object or FALSE

2 calls to _commerce_coupon_free_shipping_single_discount()
commerce_coupon_commerce_cart_order_refresh in ./commerce_coupon.module
Implements hook_commerce_cart_order_refresh().
commerce_coupon_handler_field_discount_value_display::render in includes/views/handlers/commerce_coupon_handler_field_discount_value_display.inc
Overrides parent::render().

File

./commerce_coupon.module, line 1415
Provides coupon functionality for Drupal Commerce.

Code

function _commerce_coupon_free_shipping_single_discount($coupon, $exclusive = TRUE) {
  $coupon_wrapper = entity_metadata_wrapper('commerce_coupon', $coupon);
  if ($coupon->type == 'discount_coupon') {
    foreach ($coupon_wrapper->commerce_discount_reference as $discount_wrapper) {
      if ($discount_wrapper->commerce_discount_offer
        ->value() && $discount_wrapper->commerce_discount_offer->type
        ->value() == 'free_shipping') {
        $discount = $discount_wrapper
          ->value();
      }
      elseif ($discount_wrapper->status
        ->value() && $exclusive) {

        // If the coupon grants any other kind of enabled discount, the purpose
        // of this function is to return FALSE.
        return;
      }
    }
  }
  return isset($discount) ? $discount : FALSE;
}