diff --git a/commerce_coupon.rules.inc b/commerce_coupon.rules.inc index a2453c2..09975e3 100644 --- a/commerce_coupon.rules.inc +++ b/commerce_coupon.rules.inc @@ -103,6 +103,24 @@ function commerce_coupon_rules_action_info() { ), ); + $actions['commerce_coupon_action_get_coupons_for_order'] = array( + 'label' => t('Get coupons for order'), + 'parameter' => array( + 'commerce_order' => array( + 'type' => 'commerce_order', + 'label' => t('Commerce order'), + ), + ), + 'provides' => array( + 'order_coupons' => array( + 'type' => 'list', + 'label' => 'Coupons attached to this order', + ), + ), + 'group' => t('Commerce Coupon'), + 'base' => 'commerce_coupon_action_get_coupons_for_order', + ); + $actions['commerce_coupon_action_create_coupon_line_item'] = array( 'label' => t('Create coupon line item'), 'parameter' => array( @@ -134,7 +152,7 @@ function commerce_coupon_rules_action_info() { 'callbacks' => array( 'execute' => 'commerce_coupon_action_create_coupon_line_item', ), - ); + ); $actions['commerce_coupon_action_get_coupon_uses'] = array( @@ -205,6 +223,11 @@ function commerce_coupon_action_set_granted_amount($commerce_coupon_log, $amount } +function commerce_coupon_action_get_coupons_for_order($commerce_coupon) { + return array('order_coupons' => commerce_coupon_get_coupons_by_order($commerce_coupon->order_number)); +} + + function commerce_coupon_action_create_coupon_line_item($commerce_coupon, $commerce_order, $amount, $currency_code) { if (!($commerce_order instanceof EntityMetadataWrapper)) { diff --git a/modules/basic/commerce_coupon_basic.module b/modules/basic/commerce_coupon_basic.module index c3a2309..9cb52d0 100644 --- a/modules/basic/commerce_coupon_basic.module +++ b/modules/basic/commerce_coupon_basic.module @@ -196,110 +196,3 @@ function commerce_coupon_basic_commerce_coupon_type_configure($bundle, $reset = } } } - -/** - * Implementation of hook_entity_presave() - */ -function commerce_coupon_basic_entity_presave($entity, $type) { - if ($type == 'commerce_order') { - // We need to recalc the percentage amounts. We need to this every time, because - // something could be changed since we add the coupon. Important this hook will - // be invoked before the rule event. It can be that the order needs to be stored - // twice to avoid incorrect calcuations. - commerce_coupon_basic_apply_percentage_amounts($entity); - } -} - -/** - * Apply percentage amounts to an order. - * - */ -function commerce_coupon_basic_apply_percentage_amounts($commerce_order) { - if (!($commerce_order instanceof EntityMetadataWrapper)) { - $commerce_order = entity_metadata_wrapper('commerce_order', $commerce_order); - } - - $order_id = $commerce_order->order_id->value(); - $coupons = commerce_coupon_get_coupons_by_order($order_id); - $components = rules_get_components(FALSE, 'action'); - - if (isset($commerce_order->commerce_order_total->currency_code)) { - $currency_code = $commerce_order->commerce_order_total->currency_code->value(); - } - - // We calculate for each coupon the percentage amount, and update / insert - // a corresponding line item. - foreach ($coupons as $coupon) { - - if (!($coupon instanceof EntityMetadataWrapper)) { - $coupon = entity_metadata_wrapper('commerce_coupon', $coupon); - } - $coupon_id = $coupon->coupon_id->value(); - - $fields = $coupon->getPropertyInfo(); - - $component_name = 'commerce_coupon_apply_' . $coupon->type->type->value() . '_to_item_line'; - if (isset($fields['commerce_coupon_percent_amount']) - && $coupon->commerce_coupon_percent_amount->value() > 0 - && isset($components[$component_name]) - ) { - // Iterate over all line items and calculate the reduaction of each - // line item: - - $coupon_line_item = NULL; - foreach ($commerce_order->commerce_line_items as $line_item) { - - // Find line item associated with this coupon: - if ($line_item->type->value() == 'coupon' && $line_item->commerce_coupon_reference->value() == $coupon_id) { - $coupon_line_item = $line_item; - } - elseif ($line_item->type->value() != 'coupon') { - // Invoke it with the line item. - rules_invoke_component($component_name, $line_item, $coupon); - } - } - - $amounts = &drupal_static('commerce_coupon_basic_apply_percentage_coupon_to_item_line'); - $coupon_amount = array_sum($amounts[$order_id][$coupon_id]); - - $line_item_is_new = FALSE; - - // Create new line item if there is no one. - if ($coupon_line_item == NULL) { - $coupon_line_item = commerce_coupon_line_item_new($coupon->value(), $order_id); - $coupon_line_item->commerce_unit_price = array(); - $line_item_is_new = TRUE; - } - - if (!($coupon_line_item instanceof EntityMetadataWrapper)) { - $coupon_line_item = entity_metadata_wrapper('commerce_line_item', $coupon_line_item); - } - - // Set the unit price on the line item object. - $coupon_line_item->commerce_unit_price = array(); - $coupon_line_item->commerce_unit_price->amount = $coupon_amount; - $coupon_line_item->commerce_unit_price->currency_code = $currency_code; - - if (commerce_price_component_load($coupon_line_item->commerce_unit_price->value(), 'commerce_coupon_' . $coupon->type->value())) { - commerce_price_component_delete($coupon_line_item->commerce_unit_price->value(), 'commerce_coupon_' . $coupon->type->value()); - } - - - $coupon_line_item->commerce_unit_price->data = commerce_price_component_add( - $coupon_line_item->commerce_unit_price->value(), - 'commerce_coupon_' . $coupon->type->value(), - $coupon_line_item->commerce_unit_price->value(), - TRUE, - FALSE - ); - - $coupon_line_item->save(); - if ($line_item_is_new) { - $commerce_order->commerce_line_items[] = $coupon_line_item; - } - - // TODO: Update the $coupon with the new $amount - - } - } -} diff --git a/modules/basic/commerce_coupon_basic.rules.inc b/modules/basic/commerce_coupon_basic.rules.inc index 6087209..9f4afbe 100644 --- a/modules/basic/commerce_coupon_basic.rules.inc +++ b/modules/basic/commerce_coupon_basic.rules.inc @@ -14,6 +14,7 @@ function commerce_coupon_basic_rules_action_info() { $actions = array(); + $actions['commerce_coupon_basic_apply_to_item_line'] = array( 'label' => t('Apply a percentage coupon to a line item'), 'parameter' => array( @@ -36,8 +37,6 @@ function commerce_coupon_basic_rules_action_info() { function commerce_coupon_basic_apply_percentage_coupon_to_item_line($line_item, $coupon) { - $amounts = &drupal_static(__FUNCTION__); - if (!($coupon instanceof EntityMetadataWrapper)) { $coupon = entity_metadata_wrapper('commerce_coupon', $coupon); } @@ -56,37 +55,21 @@ function commerce_coupon_basic_apply_percentage_coupon_to_item_line($line_item, // Calculate the total granted amount for this line item $amount = $line_item_wrapper->commerce_total->amount->value() * $rate * -1; + $price_component = array( + 'amount' => $amount, + 'currency_code' => $line_item_wrapper->commerce_unit_price->currency_code->value(), + 'data' => array(), + ); + + // Update the data array with the tax component. + $line_item_wrapper->commerce_unit_price->data = commerce_price_component_add( + $line_item_wrapper->commerce_unit_price->value(), + 'commerce_coupon_' . $coupon->type->value(), + $price_component, + true, + false + ); - // Save the amount for later use: - $amounts[$line_item->order_id][$coupon->coupon_id->value()][$line_item->line_item_id] = $amount; - - // Sum up all granted amounts for this coupon on this order: - $total_amount = 0; - foreach ($amounts[$line_item->order_id][$coupon->coupon_id->value()] as $line_amount) { - $total_amount += $line_amount; - } - - // Load the coupon log for updating the granted amount - $log = commerce_coupon_log_load_by_order_and_coupon($line_item->order_id, $coupon->coupon_id->value()); - $commerce_coupon_log = entity_metadata_wrapper('commerce_coupon_log', $log); - - // Set the unit price on the line item object. - $commerce_coupon_log->commerce_granted_amount->amount = -1 * $total_amount; - $commerce_coupon_log->commerce_granted_amount->currency_code = $line_item_wrapper->commerce_total->currency_code->value(); - - - // Add the base price to the components array. - if (!commerce_price_component_load($commerce_coupon_log->commerce_granted_amount->value(), 'commerce_coupon_' . $coupon->type->value())) { - $commerce_coupon_log->commerce_granted_amount->data = commerce_price_component_add( - $commerce_coupon_log->commerce_granted_amount->value(), - 'commerce_coupon_' . $coupon->type->value(), - $commerce_coupon_log->commerce_granted_amount->value(), - TRUE, - FALSE - ); - } - - commerce_coupon_log_save($commerce_coupon_log->value()); } } diff --git a/modules/basic/commerce_coupon_basic.rules_defaults.inc b/modules/basic/commerce_coupon_basic.rules_defaults.inc index 8d649f1..1d5b89f 100644 --- a/modules/basic/commerce_coupon_basic.rules_defaults.inc +++ b/modules/basic/commerce_coupon_basic.rules_defaults.inc @@ -13,39 +13,33 @@ function commerce_coupon_basic_default_rules_configuration() { $rules = array(); - // Define rules for percentage amounts - foreach (commerce_coupon_get_types() as $type => $coupon_type) { - - $instance = field_info_instance('commerce_coupon', 'commerce_coupon_percent_amount', $type); - - // Check if a percenatage field is assigned to this type - if (empty($instance)) { - // Skip this type if no percentage field is defined - continue; + $items['rules_apply_percentage_coupons_to_line_item'] = entity_import('rules_config', '{ "rules_apply_percentage_coupons_to_line_item" : { + "LABEL" : "Apply percentage coupons to line item", + "PLUGIN" : "reaction rule", + "REQUIRES" : [ + "commerce_coupon", + "rules", + "commerce_coupon_basic", + "commerce_product_reference" + ], + "ON" : [ "commerce_product_calculate_sell_price" ], + "DO" : [ + { "commerce_coupon_action_get_coupons_for_order" : { + "USING" : { "commerce_order" : [ "commerce-line-item:order" ] }, + "PROVIDE" : { "order_coupons" : { "order_coupons" : "Coupons attached to this order" } } + } + }, + { "LOOP" : { + "USING" : { "list" : [ "order-coupons" ] }, + "ITEM" : { "list_item" : "Current list item" }, + "DO" : [ + { "commerce_coupon_basic_apply_to_item_line" : { "line_item" : [ "commerce-line-item" ], "coupon" : [ "list-item" ] } } + ] + } + } + ] } - - // Create a new rules component. - $rule = rule(array( - 'line_item' => array( - 'type' => 'commerce_line_item', - 'label' => t('Line item'), - ), - 'coupon' => array( - 'type' => 'commerce_coupon', - 'label' => t('Coupon'), - ), - )); - - $rule->label = t('Apply a percentage @title amount', array('@title' => $coupon_type->label)); - - // Add the action to apply the percentage coupons - $rule->action('commerce_coupon_basic_apply_to_item_line', array( - 'line_item:select' => 'line-item', - 'coupon:select' => 'coupon', - )); - $rules['commerce_coupon_apply_' . $type . '_to_item_line'] = $rule; - - } + }');