4

I'm making the cart-content div hidden as the default (in the sidebar shopping cart block), only the title will be visible, when the user hovers on the block-commerce-cart-cart the cart-contents div will be visible to them. How do I show the user how many items the got it their cart when only the block-commerce-cart-cart is visible, which means adding the line item quantity into the header. Can anyone please help?

9
+200

Here's a quick way using hook_block_view_alter():

function MYMODULE_block_view_alter(&$data, $block) {
  if ($block->module == 'commerce_cart' && $block->delta == 'cart') {
    global $user;
    if ($order = commerce_cart_order_load($user->uid)) {
      // Count the number of product line items on the order.
      $wrapper = entity_metadata_wrapper('commerce_order', $order);
      $quantity = commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types());

      // If there are more than 0 product line items on the order...
      if ($quantity > 0) {
        // Use the dynamic menu item title.
        $data['subject'] = format_plural($quantity, 'Shopping cart (1 item)', 'Shopping cart (@count items)');
      }
    }

  }
}

The above borrows code from commerce_cart_menu_item_title() to get the cart quantity, so if that's the string you want to output you can just call that function directly and not bother with re-implementing the code, in which case it would be as simple as:

function MYMODULE_block_view_alter(&$data, $block) {
  if ($block->module == 'commerce_cart' && $block->delta == 'cart') {
    $data['subject'] = commerce_cart_menu_item_title();
  }
}

The default return from commerce_cart_menu_item_title() will be at least 'Shopping Cart' (if the cart is empty) so this method should be pretty bulletproof.

Make sure you set the title of the block as an empty string on the block admin page (so the altered subject is used instead of a custom title) and that should work fine, e.g.:

enter image description here

  • Is it possible to show "0 Items" if the cart is empty? At the moment it shows "Your shopping cart is empty" and doesn't show the title that this hook is setting. – Beebee Aug 6 '12 at 16:24
  • 2
    Ah, you can simply override theme_commerce_cart_empty_block function. – Beebee Aug 6 '12 at 16:32
  • can you see any pitfalls in using the method (Answer) I posted below? If you can please comment on it. Also I will award you the bounty once the cooldown is over (18hours left). +201 for nice answer, cheers. – Beebee Aug 6 '12 at 21:49
  • @Baysaa Nothing wrong with that method at all, they'll both produce the same results and I can't see any reason why your code wouldn't work. I'd advise putting it in a template preprocess function rather than directly in the template file though, just to ensure a good separation of logic and display – Clive Aug 6 '12 at 22:02
0

I just tried this after I posted a bounty, and this works, but it's a very quick and easy (and dirty) way to do this. The trick would be to create a custom template for your view: views-view--commerce-cart-block--default.tpl.php

And on top of that template put this PHP code:

$num = 0;
if (count($view->result)) {
  foreach ($view->result as $view_result) {
    if ($view_result->commerce_line_item_field_data_commerce_line_items_quantity) {
        $num += $view_result->commerce_line_item_field_data_commerce_line_items_quantity;
    }
  }
}

$num should now contain the total of all quantities of all products in the cart.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.