From 47cc6e8c8b03a261ad1d2e3645e6f25a8c79f8d6 Mon Sep 17 00:00:00 2001 From: Ivan Bueno Date: Tue, 9 Jul 2013 11:08:47 -0700 Subject: [PATCH] #1797184 don't load all coupons on checkout by reducing the price component to coupon types --- commerce_coupon.module | 75 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 62 insertions(+), 13 deletions(-) diff --git a/commerce_coupon.module b/commerce_coupon.module index bda80a2..12ce820 100644 --- a/commerce_coupon.module +++ b/commerce_coupon.module @@ -67,8 +67,7 @@ function commerce_coupon_entity_info() { function commerce_coupon_get_properties($coupon, array $options, $name) { switch ($name) { case 'price_component_name': - $coupon_machine_name = commerce_coupon_machine_name_code($coupon); - return $coupon->type . '_' . $coupon_machine_name; + return 'commerce_coupon_' . $coupon->type; break; case 'times_used': return commerce_coupon_get_number_of_uses($coupon->coupon_id); @@ -851,18 +850,13 @@ function commerce_coupon_code_is_active($code) { */ function commerce_coupon_commerce_price_component_type_info() { $components = array(); + $coupon_types = commerce_coupon_get_types(); - $coupons = commerce_coupon_load_multiple(array(), array('is_active' => TRUE)); - - // Add a price component type per each coupon. - foreach ($coupons as $coupon) { - $name = commerce_coupon_type_get_name($coupon->type); - $coupon_machine_name = commerce_coupon_machine_name_code($coupon); - $components[$coupon->type . '_' . $coupon_machine_name] = array( - 'title' => $name, - 'display_title' => $name, - 'coupon_type' => $coupon->type, - 'coupon_code' => $coupon_machine_name, + // Add a price component type per each coupon type. + foreach ($coupon_types as $coupon_type) { + $components['commerce_coupon_' . $coupon_type->type] = array( + 'title' => $coupon_type->label, + 'display_title' => $coupon_type->label, ); } @@ -1119,3 +1113,58 @@ function commerce_coupon_action_get_coupons_for_order($commerce_order) { } return array('order_coupons' => commerce_coupon_get_coupons_in_order($commerce_order->order_id)); } + +/** + * Implementation of hook_commerce_price_formatted_components_alter() + */ +function commerce_coupon_commerce_price_formatted_components_alter(&$components, $price, $entity) { + $coupons = array(); + // Get all coupons attached to the order. + if (count($entity->commerce_coupon_order_reference)) { + foreach ($entity->commerce_coupon_order_reference['und'] as $coupon_id) { + $coupon = entity_metadata_wrapper('commerce_coupon', commerce_coupon_load($coupon_id['target_id'])); + $coupons[$coupon->type->raw()][] = $coupon->commerce_coupon_code->value(); + } + } + + // Alter the price component label of coupons. + if ($coupons && $components) { + foreach ($components as $component_type => &$component_item) { + if (stripos($component_type, 'commerce_coupon_') !== FALSE) { + $coupon_type = preg_replace('/commerce_coupon_/', '', $component_type, 1); + if (isset($coupons[$coupon_type])) { + // Append coupon codes to the label. + $component_item['title'] .= theme('commerce_coupon_price_component_list', + array('content' => $coupons[$coupon_type])); + } + else { + // Display old coupons as individual entries. + $old_coupon_component = explode('_', $component_type); + $component_item['title'] .= 'Coupon: ' . end($old_coupon_component); + } + } + } + } +} + +/** + * Implements hook_theme(). + */ +function commerce_coupon_theme() { + return array( + 'commerce_coupon_price_component_list' => array( + 'variables' => array('content' => NULL), + ), + ); +} + +function theme_commerce_coupon_price_component_list($variables) { + $content = $variables['content']; + $output = ''; + + if ($content) { + $output .= theme('item_list', array('items' => $content)); + } + + return $output; +} \ No newline at end of file -- 1.7.11.msysgit.0