diff --git a/commerce_flat_rate.module b/commerce_flat_rate.module index 2142b1b..5a11d9b 100644 --- a/commerce_flat_rate.module +++ b/commerce_flat_rate.module @@ -142,10 +142,86 @@ function commerce_flat_rate_commerce_shipping_method_info() { } /** + * Helper function to get the base cache id for a Flat Rate services. + * + * @param string $name + * The Flat Rate service name + * @param boolean $name + * If the wildcard Id should be returned, works only with i18n enabled + * @return string + */ +function commerce_flat_rate_cache_get_id($name, $wildcard = FALSE) { + $use_translation = module_exists('i18n_string'); + $base_cache_id = 'commerce_flat_rate:services:' . $name; + + if ($use_translation) { + if ($wildcard) { + // Widcard is meaningful only when dealing with translations. + $base_cache_id .= ':*'; + } + else { + // If we're using translations, let's cache each translation separately. + global $language; + $base_cache_id .= ':' . $language->language; + } + } + return $base_cache_id; +} + +/** + * Helper function to retrieve a Flat Rate services from cache. + * + * @param $name + * @return array|null + */ +function commerce_flat_rate_cache_get($name) { + $id = commerce_flat_rate_cache_get_id($name); + $cache = cache_get($id); + if ($cache) { + return $cache->data; + } + return NULL; +} + +/** + * Helper function to store a Flat Rate service from cache. + * + * @param string $name + * The Flat Rate service name + * @param array $data + * The Flat Rate service data + * @return mixed + * The stored cache item, from cache_set() + */ +function commerce_flat_rate_cache_set($name, $data) { + $id = commerce_flat_rate_cache_get_id($name); + return cache_set($id, $data, 'cache', CACHE_TEMPORARY); +} + +/** + * Helper function to remove a Flat Rate services from cache. + * @param $name + * The Flat Rate service name + */ +function commerce_flat_rate_cache_delete($name) { + // Avoiding to handle empty service names + if (empty($name)) { + return; + } + // Get the Service ID to be deleted. We're asking for a wildcard suffix in + // case multi-language deletion if needed. + $id = commerce_flat_rate_cache_get_id($name, TRUE); + cache_clear_all($id, 'cache', TRUE); +} + + +/** * Implements hook_commerce_shipping_service_info(). */ function commerce_flat_rate_commerce_shipping_service_info() { + global $language; $services = array(); + $use_translations = module_exists('i18n_string'); // Get the default service name directly to avoid a recursive loop. It's ok in // this function if the service no longer exists. @@ -156,55 +232,76 @@ function commerce_flat_rate_commerce_shipping_service_info() { if (!empty($result)) { foreach ($result as $name => $service) { - // Create a base rate price array for the service - $base_rate = array( - 'amount' => $service['amount'], - 'currency_code' => $service['currency_code'], - 'data' => array(), - ); - // Unserialize the data array for the service. - $data = !empty($service['data']) ? unserialize($service['data']) : array(); + // First try to fetch the service from our cache. + $cached_service = commerce_flat_rate_cache_get($name); + if ($cached_service) { + $services[$name] = $cached_service; + } + else { + // Create a base rate price array for the service + $base_rate = array( + 'amount' => $service['amount'], + 'currency_code' => $service['currency_code'], + 'data' => array(), + ); - $price_component_type = 'flat_rate_' . $name; + // Unserialize the data array for the service. + $data = !empty($service['data']) ? unserialize($service['data']) : array(); - // If the data array specifies an included tax, include it now. - if (module_exists('commerce_tax') && !empty($data['include_tax']) && $tax_rate = commerce_tax_rate_load($data['include_tax'])) { - // Reverse apply the tax. - $tax_amount = $base_rate['amount'] - ($base_rate['amount'] / (1 + $tax_rate['rate'])); - $tax_amount = commerce_tax_rate_round_amount($tax_rate, $tax_amount); + $price_component_type = 'flat_rate_' . $name; - // Add a base price to the data array. - $component = array( - 'amount' => $base_rate['amount'] - $tax_amount, - 'currency_code' => $base_rate['currency_code'], - 'data' => array(), - ); + // If the data array specifies an included tax, include it now. + if (module_exists('commerce_tax') && !empty($data['include_tax']) && $tax_rate = commerce_tax_rate_load($data['include_tax'])) { + // Reverse apply the tax. + $tax_amount = $base_rate['amount'] - ($base_rate['amount'] / (1 + $tax_rate['rate'])); + $tax_amount = commerce_tax_rate_round_amount($tax_rate, $tax_amount); - $base_rate['data'] = commerce_price_component_add($base_rate, $price_component_type, $component, TRUE, FALSE); + // Add a base price to the data array. + $component = array( + 'amount' => $base_rate['amount'] - $tax_amount, + 'currency_code' => $base_rate['currency_code'], + 'data' => array(), + ); - // Add the tax to the data array. - $component['amount'] = $tax_amount; - $component['data']['tax_rate'] = $tax_rate; + $base_rate['data'] = commerce_price_component_add($base_rate, $price_component_type, $component, TRUE, FALSE); - $base_rate['data'] = commerce_price_component_add($base_rate, $tax_rate['price_component'], $component, TRUE); - } + // Add the tax to the data array. + $component['amount'] = $tax_amount; + $component['data']['tax_rate'] = $tax_rate; - // Add the full service array to our return value. - $services[$name] = array( - 'title' => $service['title'], - 'display_title' => !empty($service['display_title']) ? $service['display_title'] : $service['title'], - 'description' => $service['description'], - 'shipping_method' => 'flat_rate', - 'rules_component' => !empty($service['rules_component']), - 'price_component' => $price_component_type, - 'callbacks' => array( - 'rate' => 'commerce_flat_rate_service_rate_order', - ), - 'base_rate' => $base_rate, - 'weight' => $service['weight'], - 'data' => $data, - ); + $base_rate['data'] = commerce_price_component_add($base_rate, $tax_rate['price_component'], $component, TRUE); + } + + $untranslated = array( + 'title' => $service['title'], + 'display_title' => !empty($service['display_title']) ? $service['display_title'] : $service['title'], + 'description' => $service['description'], + ); + + if ($use_translations) { + $service = i18n_object('commerce_flat_rate', $service)->translate($language->language); + } + // Add the full service array to our return value. + $services[$name] = array( + 'title' => $service['title'], + 'display_title' => !empty($service['display_title']) ? $service['display_title'] : $service['title'], + 'description' => $service['description'], + 'shipping_method' => 'flat_rate', + 'rules_component' => !empty($service['rules_component']), + 'price_component' => $price_component_type, + 'callbacks' => array( + 'rate' => 'commerce_flat_rate_service_rate_order', + ), + 'base_rate' => $base_rate, + 'weight' => $service['weight'], + 'data' => $data, + 'untranslated_strings' => $untranslated, + ); + + // Save the newly created service to our cache. + commerce_flat_rate_cache_set($name, $services[$name]); + } // Add the base rate, sort order, and default status to the service's // description on the admin overview page. @@ -219,6 +316,7 @@ function commerce_flat_rate_commerce_shipping_service_info() { $services[$name]['description'] .= '