You are here

function dc_ajax_add_cart_remove_commerce_line_item in Commerce Ajax Add to Cart 7

Same name and namespace in other branches
  1. 7.2 dc_ajax_add_cart.module \dc_ajax_add_cart_remove_commerce_line_item()

Menu callback: Removes the product item from cart.

Parameters

string $ajax: Page argument, ajaxifies the removal of product from cart.

int $line_item_id: Line item ID.

Return value

array Ajax command array for product removal.

See also

dc_ajax_add_cart_menu()

1 string reference to 'dc_ajax_add_cart_remove_commerce_line_item'
dc_ajax_add_cart_menu in ./dc_ajax_add_cart.module
Implements hook_menu().

File

./dc_ajax_add_cart.module, line 120
Ajax add to cart module.

Code

function dc_ajax_add_cart_remove_commerce_line_item($ajax, $line_item_id) {
  global $user;
  $order = commerce_cart_order_load($user->uid);

  // Initially quantity of products in cart will be zero.
  $quantity = 0;

  // Check whether this is AJAX callback.
  $is_ajax = $ajax === 'ajax';

  // Delete the product line item from cart.
  commerce_cart_order_product_line_item_delete($order, $line_item_id);

  // Get the current status of cart and other information.
  $commerce_cart = dc_ajax_add_cart_get_commerce_cart_details();
  $line_items = $commerce_cart['wrapper']->commerce_line_items;
  $quantity = commerce_line_items_quantity($line_items, commerce_product_line_item_types());
  $total = commerce_line_items_total($line_items);
  if ($is_ajax) {

    // Update the cart content.
    $commands = array();

    // Check whether the user has any product in cart.
    if (variable_get(DC_AJAX_ADD_CART_HIDE_EMPTY_CART, 0) && $quantity == 0) {
      $content_cart = '';
    }
    else {
      $content_cart = theme('dc_ajax_shopping_cart', array(
        'order' => $commerce_cart['order'],
        'line_items' => $line_items,
        'quantity' => $quantity,
        'total' => $total,
      ));
    }
    $commands[] = dc_ajax_add_cart_command_html('div.ajax-shopping-cart-wrapper', $content_cart);
    $content_teaser = theme('dc_ajax_shopping_cart_teaser', array(
      'order' => $commerce_cart['order'],
      'quantity' => $quantity,
      'total' => $total,
    ));
    $commands[] = dc_ajax_add_cart_command_html('div.ajax-shopping-cart-teaser', $content_teaser);

    // Ajaxifies the updation of cart.
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }
  else {
    drupal_set_message(t('Product removed from cart'));
    drupal_goto();
  }
}