Remove Cart Block When Empty
Hi there
I have been looking for a way to remove the cart block when it's empty. I have looked though all the options in the cart block view but was unable to find it. I also looked in the block configuration but still not luck?
Maybe I am going blind and just missed it?
Any ideas how to accomplish this?
Comments
A solution
I ran into this very problem, and found this very thread.
I asked on Drupal.org and Ryan came up with a good solution.
If you look at your list of views, there's already a view for the shopping cart. Add a black to that view and it will exhibit the usual block behaviour of disappearing when it returns no results. If you use this block instead of the commerce core cart block, it will show the same information as the core block, but disappear when empty.
It should be noted that my testing through up a potential problem here.
You MUST make sure you add Order Status = Shopping Cart to the view filters, or any given user will see everything they've ever ordered in their cart and the remove from cart buttons won't work for those old line items.
I hope that helps.
DarkSnow.
Nice. This answer saved me
Nice. This answer saved me time.
You probably want to put both: Shopping cart and Checkout. Otherwise the items disappear during the checkout and you want the items to be removed after the order has been completed.
Additional Tweak
Hey darksnow and ryan! Thanks for the tip.
However, in attempting this myself I needed to make an additional tweak.
On the contextual filters there's: Commerce Order: Order ID Make sure it's set to:
Provide default value > current user's cart order ID
Mine defaulted to: Display all results for the specified field
That had me confused for a few minutes. Hope it helps!
Re: Additional Tweak
Thanks @sleepingmonk, that contextual filter spec was just what I needed.
Thanks for the tip!
Here's a View that incorporates the ideas above.
<?php
$view = new view();
$view->name = 'commerce_cart_block';
$view->description = 'Display a list of line items added to cart.';
$view->tag = 'commerce';
$view->base_table = 'commerce_order';
$view->human_name = 'Shopping cart block';
$view->core = 0;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
/* Display: Defaults */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->display->display_options['title'] = 'Shopping cart';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['query']['options']['query_comment'] = FALSE;
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'none';
$handler->display->display_options['style_plugin'] = 'table';
$handler->display->display_options['style_options']['columns'] = array(
'quantity' => 'quantity',
'commerce_display_path' => 'commerce_display_path',
'line_item_title' => 'line_item_title',
'commerce_total' => 'commerce_total',
);
$handler->display->display_options['style_options']['default'] = '-1';
$handler->display->display_options['style_options']['info'] = array(
'quantity' => array(
'sortable' => 0,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
),
'commerce_display_path' => array(
'sortable' => 0,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
),
'line_item_title' => array(
'align' => '',
'separator' => '',
),
'commerce_total' => array(
'sortable' => 0,
'default_sort_order' => 'asc',
'align' => 'views-align-right',
'separator' => '',
),
);
/* Footer: Commerce Line Item: Line item summary */
$handler->display->display_options['footer']['line_item_summary']['id'] = 'line_item_summary';
$handler->display->display_options['footer']['line_item_summary']['table'] = 'commerce_line_item';
$handler->display->display_options['footer']['line_item_summary']['field'] = 'line_item_summary';
$handler->display->display_options['footer']['line_item_summary']['links'] = array(
'view_cart' => 'view_cart',
'checkout' => 'checkout',
);
$handler->display->display_options['footer']['line_item_summary']['info'] = array(
'quantity' => 'quantity',
'total' => 'total',
);
/* Relationship: Commerce Order: Referenced line item */
$handler->display->display_options['relationships']['commerce_line_items_line_item_id']['id'] = 'commerce_line_items_line_item_id';
$handler->display->display_options['relationships']['commerce_line_items_line_item_id']['table'] = 'field_data_commerce_line_items';
$handler->display->display_options['relationships']['commerce_line_items_line_item_id']['field'] = 'commerce_line_items_line_item_id';
$handler->display->display_options['relationships']['commerce_line_items_line_item_id']['required'] = TRUE;
/* Field: Commerce Line Item: Quantity */
$handler->display->display_options['fields']['quantity']['id'] = 'quantity';
$handler->display->display_options['fields']['quantity']['table'] = 'commerce_line_item';
$handler->display->display_options['fields']['quantity']['field'] = 'quantity';
$handler->display->display_options['fields']['quantity']['relationship'] = 'commerce_line_items_line_item_id';
$handler->display->display_options['fields']['quantity']['label'] = '';
$handler->display->display_options['fields']['quantity']['precision'] = '0';
$handler->display->display_options['fields']['quantity']['suffix'] = ' × ';
/* Field: Commerce Line item: Display path */
$handler->display->display_options['fields']['commerce_display_path']['id'] = 'commerce_display_path';
$handler->display->display_options['fields']['commerce_display_path']['table'] = 'field_data_commerce_display_path';
$handler->display->display_options['fields']['commerce_display_path']['field'] = 'commerce_display_path';
$handler->display->display_options['fields']['commerce_display_path']['relationship'] = 'commerce_line_items_line_item_id';
$handler->display->display_options['fields']['commerce_display_path']['label'] = '';
$handler->display->display_options['fields']['commerce_display_path']['exclude'] = TRUE;
/* Field: Commerce Line Item: Title */
$handler->display->display_options['fields']['line_item_title']['id'] = 'line_item_title';
$handler->display->display_options['fields']['line_item_title']['table'] = 'commerce_line_item';
$handler->display->display_options['fields']['line_item_title']['field'] = 'line_item_title';
$handler->display->display_options['fields']['line_item_title']['relationship'] = 'commerce_line_items_line_item_id';
$handler->display->display_options['fields']['line_item_title']['label'] = '';
$handler->display->display_options['fields']['line_item_title']['alter']['make_link'] = TRUE;
$handler->display->display_options['fields']['line_item_title']['alter']['path'] = '[commerce_display_path]';
/* Field: Commerce Line item: Total */
$handler->display->display_options['fields']['commerce_total']['id'] = 'commerce_total';
$handler->display->display_options['fields']['commerce_total']['table'] = 'field_data_commerce_total';
$handler->display->display_options['fields']['commerce_total']['field'] = 'commerce_total';
$handler->display->display_options['fields']['commerce_total']['relationship'] = 'commerce_line_items_line_item_id';
$handler->display->display_options['fields']['commerce_total']['label'] = '';
$handler->display->display_options['fields']['commerce_total']['element_class'] = 'price';
$handler->display->display_options['fields']['commerce_total']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['commerce_total']['click_sort_column'] = 'amount';
$handler->display->display_options['fields']['commerce_total']['type'] = 'commerce_price_formatted_amount';
/* Sort criterion: Commerce Line Item: Line item ID */
$handler->display->display_options['sorts']['line_item_id']['id'] = 'line_item_id';
$handler->display->display_options['sorts']['line_item_id']['table'] = 'commerce_line_item';
$handler->display->display_options['sorts']['line_item_id']['field'] = 'line_item_id';
$handler->display->display_options['sorts']['line_item_id']['relationship'] = 'commerce_line_items_line_item_id';
/* Contextual filter: Commerce Order: Order ID */
$handler->display->display_options['arguments']['order_id']['id'] = 'order_id';
$handler->display->display_options['arguments']['order_id']['table'] = 'commerce_order';
$handler->display->display_options['arguments']['order_id']['field'] = 'order_id';
$handler->display->display_options['arguments']['order_id']['default_action'] = 'default';
$handler->display->display_options['arguments']['order_id']['default_argument_type'] = 'commerce_cart_current_cart_order_id';
$handler->display->display_options['arguments']['order_id']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['order_id']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['order_id']['summary_options']['items_per_page'] = '25';
/* Filter criterion: Commerce Line Item: Line item is of a product line item type */
$handler->display->display_options['filters']['product_line_item_type']['id'] = 'product_line_item_type';
$handler->display->display_options['filters']['product_line_item_type']['table'] = 'commerce_line_item';
$handler->display->display_options['filters']['product_line_item_type']['field'] = 'product_line_item_type';
$handler->display->display_options['filters']['product_line_item_type']['relationship'] = 'commerce_line_items_line_item_id';
$handler->display->display_options['filters']['product_line_item_type']['value'] = '1';
/* Filter criterion: Commerce Order: Order status */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'commerce_order';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = array(
'cart' => 'cart',
'checkout_checkout' => 'checkout_checkout',
);
/* Display: Shopping Cart Block */
$handler = $view->new_display('block', 'Shopping Cart Block', 'block_1');
$handler->display->display_options['defaults']['hide_admin_links'] = FALSE;
$handler->display->display_options['block_description'] = 'Shopping Cart Block (not when empty)';
?>