Re: Re: Re: Re: Re: How can I embed the quantity in cart in node

Posts: 79
Joined: 01/18/2008

I ask a whole lot of questions around here and I really appreciate the support of the members.
I found a solution to my problem and thought I would post the results. If someone is looking for;

how to embed product quantity in your node-product.tpl.php

This code adds your new region

template.php

<?php
function YOURTEMPLATENAME_regions() {

  return array(
   
'left' => t('left sidebar'),
   
'right' => t('right sidebar'),
   
'navbar' => t('navigation bar'),
   
'content_top' => t('content top'),
   
'content_bottom' => t('content bottom'),
   
'inline_author' => t('inline author'),
   
'header' => t('header'),
   
'footer' => t('footer'),
   
'closure_region' => t('closure'),
   
'quantity_region' => t('quantity region'),
  );
}
?>

This code makes sure you can add a region to your node

template.php

<?php
function _phptemplate_variables($hook, $vars = array()) {

// code can go here

 
switch($hook) {
    case
'node' :
      if (
$vars['page'] /* the equivolent of $page in node.tpl.php */ != 0) {
       
$vars['quantity_region']= theme_blocks('quantity_region');
      }
    break;
  }

// more code can go here

 
return $vars;
}
?>

This code loads your new region

node-product.tpl.php

<?php
if ($quantity_region):
?>

<?php
print $quantity_region;
?>

<?php
endif;
?>

Finally, I only wanted the number, no other items of the block. This was a very customized site, which I'll show when it goes public, so here is that final code.

template.php

<?php
// The default function from uc_cart.module.
function phptemplate_uc_cart_block_content() {
  global
$user;

  if (
variable_get('uc_cart_show_help_text', FALSE)) {
   
$output = '<span class="cart-help-text">'
           
. variable_get('uc_cart_help_text', t('Click title to display cart contents.'))
             .
'</span>';
  }

 
$output .= '<div id="block-cart-contents">';

 
$items = uc_cart_get_contents();

 
$item_count = 0;
  if (!empty(
$items)) {
    foreach (
$items as $item) {
      if (
is_array($item->data['attributes']) && !empty($item->data['attributes'])) {
       
$display_item = module_invoke($item->module, 'cart_display', $item);
      }
     
$total += ($item->price) * $item->qty;
     
$item_count += $item->qty;
    }
  }
  else {
   
$output .= ''. t('') .'';
  }

 
$output .= '</div>';

 
$item_text = format_plural($item_count, '1', '@count');
 
$view = '('. l(t('View cart'), 'cart', array('rel' => 'nofollow')) .')';
  if (
variable_get('uc_checkout_enabled', TRUE)) {
   
$checkout = ' ('. l(t('Checkout'), 'cart/checkout', array('rel' => 'nofollow')) .')';
  }
//  $output .= '<table class="cart-block-summary-table"><tbody class="cart-block-summary-tbody">'
//            .'<tr class="cart-block-summary-tr"><td class="cart-block-summary-items">'
//            . $item_text .'</td><td class="cart-block-summary-total">'
//            .'<strong>'. t('Total:') .'</strong> '. uc_currency_format($total) .'</td></tr>';
   
$output .= $item_text;
  if (
$item_count > 0) {
//    $output .= '<tr><td colspan="2" class="cart-block-summary-checkout">'. $view . $checkout .'</td></tr>';
 
}
//  $output .= '</tbody></table>';

 
return $output;
}
?>

The code could be cleaned up a lot, but its a good start.