Showing posts with label delete line item. Show all posts
Showing posts with label delete line item. Show all posts

Friday, June 26, 2015

Drupal commerce programmatically delete a line item from the cart by using product id

Programmatically delete a line item from the cart by using product id

function product_delete_cart($product_ids) {
  if (!is_array($product_ids)) {
    $line_item_id = product_in_cart($product_ids); // product_in_cart() function - http://php-developer-informations.blogspot.co.uk/2015/06/drupal-commerce-add-line-item-delete.html
    if ($line_item_id != -1 && $line_item_id != -2) {
      global $user;
      $current_order = commerce_cart_order_load($user->uid);
      commerce_cart_order_product_line_item_delete($current_order, $line_item_id);
    } else
      return -1;
  }
  else {
    $line_item_ids = product_in_cart($product_ids);
    if ($line_item_ids != -1 && $line_item_ids != -2) {
      global $user;
      $current_order = commerce_cart_order_load($user->uid);
      foreach ($line_item_ids as $line_item_id) {
        commerce_cart_order_product_line_item_delete($current_order, $line_item_id);
      }
    }
    else {
      return -1;
    }
  }
}