You are here

function dc_ajax_add_cart_block_view in Commerce Ajax Add to Cart 7.2

Same name and namespace in other branches
  1. 7 dc_ajax_add_cart.module \dc_ajax_add_cart_block_view()

Implements hook_block_view().

File

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

Code

function dc_ajax_add_cart_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'ajax_shopping_cart':

      // Get the current status of cart and other details.
      $commerce_cart = dc_ajax_add_cart_get_commerce_cart_details();
      $hide_empty_cart = FALSE;
      if ($commerce_cart['order']) {
        $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 (variable_get(DC_AJAX_ADD_CART_HIDE_EMPTY_CART, 0) && $quantity == 0) {
          $hide_empty_cart = TRUE;
        }
      }
      else {
        $line_items = NULL;
        $quantity = 0;
        $total = NULL;
        if (variable_get(DC_AJAX_ADD_CART_HIDE_EMPTY_CART, 0)) {
          $hide_empty_cart = TRUE;
        }
      }

      // Hide cart if it is empty.
      if ($hide_empty_cart) {
        $block['content'] = theme('html_tag', array(
          'element' => array(
            '#tag' => 'div',
            '#value' => '',
            '#attributes' => array(
              'class' => 'ajax-shopping-cart-wrapper',
            ),
          ),
        ));
      }
      else {
        $block['content'] = '<div class="ajax-shopping-cart-wrapper">';
        $block['content'] .= theme('dc_ajax_shopping_cart', array(
          'order' => $commerce_cart['order'],
          'line_items' => $line_items,
          'quantity' => $quantity,
          'total' => $total,
        ));
        $block['content'] .= '</div>';
      }
      drupal_add_library('dc_ajax_add_cart', 'base', TRUE);
      break;
    case 'ajax_shopping_cart_teaser':

      // Get the current status of cart and other details.
      $commerce_cart = dc_ajax_add_cart_get_commerce_cart_details();
      if ($commerce_cart['order']) {
        $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);
      }
      else {
        $line_items = NULL;
        $quantity = 0;
        $total = NULL;
      }
      $block['content'] = '<div class="ajax-shopping-cart-teaser">';
      $block['content'] .= theme('dc_ajax_shopping_cart_teaser', array(
        'order' => $commerce_cart['order'],
        'quantity' => $quantity,
        'total' => $total,
      ));
      $block['content'] .= '</div>';
      drupal_add_library('dc_ajax_add_cart', 'base', TRUE);
      break;
  }
  return $block;
}