You are here

function commerce_free_shipping_rate_callback in Commerce Free Shipping 7

Override default rate callback of the shipping services.

1 string reference to 'commerce_free_shipping_rate_callback'
commerce_free_shipping_commerce_shipping_service_info_alter in ./commerce_free_shipping.module
Implements hook_commerce_shipping_service_info_alter().

File

./commerce_free_shipping.module, line 113
Implements hook_commerce_shipping_service_info_alter() to change the price of the shipping service, when it's needed.

Code

function commerce_free_shipping_rate_callback($shipping_service, $order) {
  $base = $shipping_service['base'];
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $order_currency_code = $order_wrapper->commerce_order_total->currency_code
    ->value();
  $rate_callback = $shipping_service['callbacks']['original_rate'];
  if (!function_exists($rate_callback)) {
    return FALSE;
  }

  // Call the original rate callback,
  // and keep the normal shipping rate for this service / order.
  $rate = $rate_callback($shipping_service, $order);

  // If callback return false, that means this shipping method is non available.
  // If callback return an amount of 0 for this shipping service do nothing.
  if ($rate === FALSE || is_array($rate) && $rate['amount'] === 0) {
    return $rate;
  }

  // Check if free shipping is enabled for this services.
  if (!variable_get($base . '_free_shipping_enabled', FALSE)) {
    return $rate;
  }

  // Get the total amount of the products.
  $total = 0;
  $price_threshold_currency_code = variable_get($base . '_free_shipping_rate_limit_currency', 'default');
  if ($price_threshold_currency_code == 'default') {
    $price_threshold_currency_code = commerce_default_currency();
  }
  foreach ($order_wrapper->commerce_line_items as $line_item) {
    if (in_array($line_item->type
      ->value(), variable_get($base . '_free_shipping_excluded_line_item_types', array(
      'shipping' => 'shipping',
    )))) {

      // This line item type is configured to be excluded from the calcul.
      continue;
    }
    if (in_array($line_item->type
      ->value(), commerce_product_line_item_types()) && isset($line_item->commerce_product)) {
      if (in_array($line_item->commerce_product->type
        ->value(), variable_get($base . '_free_shipping_excluded_line_item_product_types', array(
        'shipping' => 'shipping',
      )))) {
        continue;
      }
    }

    // Add this line item total amount to the calcul.
    if ($line_item->commerce_total->currency_code
      ->value() == $price_threshold_currency_code) {
      $total += $line_item->commerce_total->amount
        ->value();
    }
    else {

      // Convert the current line item total price to the price
      // threshold currency before adding it to the total.
      $total += commerce_currency_convert($line_item->commerce_total->amount
        ->value(), $line_item->commerce_total->currency_code
        ->value(), $price_threshold_currency_code);
    }
  }

  // Check and apply free shipping.
  $limit_rate = variable_get($base . '_free_shipping_rate_limit', 6000);
  $apply_free_shipping_pricing = FALSE;

  // Test if the total are superior or egal to the threshold rate.
  if ($total >= $limit_rate) {
    $apply_free_shipping_pricing = TRUE;

    // Additionnal rule conditions check.
    if (variable_get($base . '_free_shipping_additionnal_rules', FALSE)) {
      $apply_free_shipping_pricing = rules_invoke_component('commerce_free_shipping_service_' . $base, $order);
    }

    // Apply free shipping price.
    if ($apply_free_shipping_pricing) {
      $rate['amount'] = 0;
      if (!empty($rate['data']['components'])) {
        $rate['data']['components'] = array();
      }

      // Message for the client.
      if (variable_get($base . '_free_shipping_gift_message_display', TRUE)) {
        if ($price_threshold_currency_code != $order_currency_code) {
          $limit_rate = commerce_currency_convert($limit_rate, $price_threshold_currency_code, $order_currency_code);
        }
        $message_rate = commerce_currency_format($limit_rate, $order_currency_code);
        $message = variable_get($base . '_free_shipping_gift_message');
        $message = str_replace('%rate', $message_rate, $message);
        drupal_set_message(filter_xss($message, array(
          'b',
          'strong',
          'i',
          'u',
          'em',
          'a',
        )), 'status', FALSE);
      }
    }
    else {

      // The shipping service are not for free,
      // Because of the rules conditions,
      // tell something to the customer ?
      if (variable_get($base . '_free_shipping_rules_reject_message_display', TRUE)) {
        $message = variable_get($base . '_free_shipping_rules_reject_message');
        drupal_set_message(filter_xss($message, array(
          'b',
          'strong',
          'i',
          'u',
          'em',
          'a',
        )), 'status', FALSE);
      }
    }
  }
  else {

    // The shipping service are not for free,
    // Because of the free shipping total price threshold.
    // tell something to the customer ?
    if (variable_get($base . '_free_shipping_reject_message_display', TRUE)) {

      // Get the amount left to obtain free shipping.
      $message_rate = $limit_rate - $total;
      if ($price_threshold_currency_code != $order_currency_code) {
        $message_rate = commerce_currency_convert($message_rate, $price_threshold_currency_code, $order_currency_code);
      }
      $message_rate = commerce_currency_format($message_rate, $order_currency_code);
      $message = variable_get($base . '_free_shipping_reject_message');
      $message = str_replace('%rate', $message_rate, $message);
      drupal_set_message(filter_xss($message, array(
        'b',
        'strong',
        'i',
        'u',
        'em',
        'a',
      )), 'status', FALSE);
    }
  }
  return $rate;
}