diff -u b/commerce_discount.module b/commerce_discount.module --- b/commerce_discount.module +++ b/commerce_discount.module @@ -18,14 +18,19 @@ $discount_types = commerce_discount_types(); $discount_types = array_keys($discount_types); foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) { - commerce_discount_remove_discount_components($line_item_wrapper->commerce_unit_price, $discount_types); - commerce_discount_remove_discount_components($line_item_wrapper->commerce_total, $discount_types); - $line_item_wrapper->save(); + $changed = commerce_discount_remove_discount_components($line_item_wrapper->commerce_unit_price, $discount_types); + $changed = $changed || commerce_discount_remove_discount_components($line_item_wrapper->commerce_total, $discount_types); + if ($changed) { + $line_item_wrapper->save(); + } } if (isset($order_wrapper->commerce_order_total)) { - commerce_discount_remove_discount_components($order_wrapper->commerce_order_total, $discount_types); + $changed = commerce_discount_remove_discount_components($order_wrapper->commerce_order_total, $discount_types); + if ($changed) { + $order_wrapper->save(); + } } - $order_wrapper->save(); + } /** @@ -105,12 +110,15 @@ * Wrapped commerce price. * @param array $discount_types_to_remove * An array of discount type strings to remove. + * + * @return bool + * TRUE if at least one price component has been removed, FALSE otherwise. */ function commerce_discount_remove_discount_components($price_wrapper, $discount_types_to_remove = array('order_discount')) { $price = $price_wrapper->value(); // If there are no price or components, there is nothing to remove. if (!$price || empty($price['data']['components'])) { - return; + return FALSE; } $data = (array) $price['data'] + array('components' => array()); @@ -142,7 +150,7 @@ } // Don't alter the price components if no components were removed. if (!$component_removed) { - return; + return FALSE; } // Re-save the price without the discounts (if existed). @@ -151,6 +159,7 @@ // Re-set the total price. $total = commerce_price_component_total($price_wrapper->value()); $price_wrapper->amount = $total['amount']; + return TRUE; } /**