Problem/Motivation

commerce_cart_order_empty() only removes product line items when emptying the cart but provides a hook to modify the $order. Without implementing this hook, the cart order still contains discount line items.

Proposed resolution

Implement hook_commerce_cart_order_empty($order)

Remaining tasks

User interface changes

API changes

CommentFileSizeAuthor
#3 implement-2465659-3.patch1.06 KBjoelpittet

Comments

joelpittet’s picture

Status: Active » Needs review

Here's my take on how this would look:

/**
 * Implements hook_commerce_cart_order_empty().
 */
function commerce_discount_commerce_cart_order_empty($order) {
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $line_items_to_delete = array();
  foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
    if ($line_item_wrapper->getBundle() == 'commerce_discount') {
      $line_items_to_delete[] = $line_item_wrapper->getIdentifier();
      $order_wrapper->commerce_line_items->offsetUnset($delta);
    }
  }

  // Delete line items.
  commerce_line_item_delete_multiple($line_items_to_delete);
}
bojanz’s picture

Makes sense, can you roll a patch please?

joelpittet’s picture

StatusFileSize
new1.06 KB

@bojanz Sure thing thanks for the review, seems to be working well so here's a patch.

bojanz’s picture

Status: Needs review » Fixed

Thank you for the quick response.

  • bojanz committed 8a01e56 on 7.x-1.x authored by joelpittet
    Issue #2465659 by joelpittet: Implement hook_commerce_cart_order_empty...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.