I use AuthCache and Drupal Commerce in my Drupal 7 site. I am currently building a new Foundation subtheme for the site with the Foundation topbar activated for mobile only.
Now, to show the number of items in the cart I simply replace the default cart block content with this in my template.php:
function mynewtheme_block_view_alter(&$data, $block) {
global $user;
if ($block->module == 'commerce_cart') {
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($quantity){
$data['content'] = ('<a class="varukorg" href="/cart"><i class="fi-shopping-cart"></i>('.$quantity.')</a>');
}else{
$data['content'] = ('<span class="varukorg cart-empty-block"><i class="fi-shopping-cart"></i> (0)</span>');
}
}else{
$data['content'] = ('<span class="varukorg cart-empty-block"><i class="fi-shopping-cart"></i> (0)</span>');
}
}
}
The above shows a basket with the number of items in the cart.
Then, to show the number of items also in the Foundation topbar on mobile devices or narrow windows I use this code in a preprocess_page
hook in template.php:
$cartblock = block_render('commerce_cart', 'cart');
$vars['varukorgen'] = $cartblock;
Followed by this line in page.tpl.php:
<?php print render($varukorgen); ?>
Now to my problem... the number of items shown by the variable $varukorgen
in page.tpl.php is incorrect on smaller window sizes when the topbar is toggled. It think that is shows a cached version of the number.
In the AuthCache settings in the block in question i have set "Never" for the Cache lifetime, Per user and I also enabled Ajax.
So why is the number not showing correctly when it is rendered by means of a custom variable in page.tpl.php?
EDITED: the solution was as suggested below to create a new region in the .info file, place the cart block in that region in the UI and then render that region inside the topbar region in page.tpl.php like this:
<?php if ($page['topplist']): ?>
<div id="topplist" class="basket">
<?php print render($page['topplist']); ?>
</div> <!-- /#topplist -->
<?php endif; ?>
Now the the number of items in the cart is updated as it should on every page by the means of Ajax (enabled in the cart block).