Is there a way to hide the block shopping cart when empty.

Thanks

Comments

Alex Dicianu’s picture

For now, there isn't such a functionality without writing custom code.

jenlampton’s picture

Status: Active » Fixed

If you absolutely needed to, you could use PHP code to determine when the cart is empty and hide it via your theme.

You can use this code by either overriding your block--basic-cart.tpl.php file, and wrapping the output in the following check, or better yet, do it in template.php in a themename_preprocess_block function.

Here's how to determine if your cart is empty or not:

  $cart = basic_cart_get_cart();
  $item_count = 0;
  foreach ($cart as $nid => $item) {
    $item_count = $item_count + $item->basic_cart_quantity;
  }
  if ($item_count != 0) {
    // Cart is not empty.
  }

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

brett1’s picture

Hi, I'm trying to achieve this but not sure how to from the instructions - due to my inexperience :>

I've created block--basic-cart.tpl.php in my theme/template folder
and paste

  $cart = basic_cart_get_cart();
  $item_count = 0;
  foreach ($cart as $nid => $item) {
    $item_count = $item_count + $item->basic_cart_quantity;
  }
  if ($item_count != 0) {
    // Cart is not empty.
  }

Obviously it doesn't work but I'm not sure why -- would I add div tags and display none ??
OR did I get the instructions completely wrong?

Thanx

demonde’s picture

I donnot think this is the most elegant way, but it works:

Create block--basic-cart.tpl.php and paste this code with some more code from your block.tpl.php

<?php 
$cart = basic_cart_get_cart();
if (!empty($cart)):
?>

// Here comes the content from the default block.tpl.php

<?php endif; ?>


firfin’s picture

How can you achieve this in the D8 version? I aksed the same question here: #3012638: Hide block when cart empty - D8

StijnBousard’s picture

I used the same PHP code as suggested by jenlampton, added code to hide it on checkout, and used it in the visibility settings of the cart block instead of in the block template file:

Visibility settings > Pages tab > Show block on specific pages: Pages on which this PHP code returns TRUE (experts only):

<?php
$path = $_SERVER['REQUEST_URI'];
$find = 'checkout';
$pos = strpos($path, $find);
$cart = basic_cart_get_cart();
$item_count = 0;
foreach ($cart as $nid => $item)
{
    $item_count = $item_count + $item->basic_cart_quantity;
}
if (($item_count != 0) and ($pos == false))
{
    return true;
}
else
{
    return false;
}
?>