You are here

function commerce_popup_cart_block_view in Commerce Popup Cart 7

Implements hook_block_view().

File

./commerce_popup_cart.module, line 46

Code

function commerce_popup_cart_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'commerce_popup_cart':
      global $user;

      // Default to an empty cart block message.
      $content = '';

      // First check to ensure there are products in the shopping cart.
      if ($order = commerce_cart_order_load($user->uid)) {
        $wrapper = entity_metadata_wrapper('commerce_order', $order);

        // Build the variables array to send to the cart block template.
        $variables = array(
          'order' => $order,
          'contents_view' => commerce_embed_view('commerce_cart_block', 'defaults', array(
            $order->order_id,
          ), $_GET['q']),
        );
        $quantity = 0;
        foreach ($wrapper->commerce_line_items as $line_item) {
          if (!$line_item instanceof EntityMetadataWrapper) {
            $line_item = entity_metadata_wrapper('commerce_line_item', $line_item);
          }
          $types = commerce_popup_cart_selected_line_items();
          if (in_array($line_item->type
            ->value(), $types)) {
            $quantity += $line_item->quantity
              ->value();
          }
        }
        if ($quantity > 0) {
          if (variable_get('commerce_popup_cart_item_suffix', 0)) {
            $quantity = format_plural($quantity, '1 item', '@count items');
          }
          $content = theme('commerce_popup_cart_cart', array(
            'product_count' => $quantity,
            'variables' => $variables,
          ));
        }
        elseif (variable_get('commerce_popup_cart_show_empty_cart', 0) == 1) {
          $content = commerce_popup_cart_block_view_get_empty_cart($variables);
        }
      }
      elseif (variable_get('commerce_popup_cart_show_empty_cart', 0) == 1) {
        $content = commerce_popup_cart_block_view_get_empty_cart($variables = array());
      }

      // If the superfish module is not installed then add hoverintent script
      if (!module_exists('superfish')) {
        drupal_add_js(drupal_get_path('module', 'commerce_popup_cart') . '/js/jquery.hoverIntent.minified.js');
      }
      return array(
        'subject' => t('Shopping cart'),
        'content' => $content,
      );
      break;
  }
  return $block;
}