How to embed the cart quantity in node-products.tpl.php

Posts: 77
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.

Posts: 1228
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

I think it's something like..

<?php
$items
= count(uc_cart_get_contents());
?>

I'd say make a conditional to return a message instead of 0.

<?php
if ($items > 0) {
   
$output = "You have ".$items." items in your cart.";
  } else {
   
$output = "Nothing in your cart!";
  }
print
$output;
?>

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 77
Joined: 01/18/2008

Code worked great, thanks.

Posts: 77
Joined: 01/18/2008

Sorry to keep on this, but what would you suggest doing for showing the qty, not just items?

I have a hacked up version of the block:

<?php
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 .= $item_text;
  if (
$item_count > 0) {
  }

  return
$output;
}
?>

But this is not ideal. I would prefer a simple method of adding it directly in my node-products.tpl.php page.

Posts: 1228
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

Yeah I was going to say, I thought the cart block already did this? But are you breaking it out into the individual attributes?

If that's a functionality you need I would say just write a new block... use the code that you have there for it, and then specify the region in your template.php file, and call the block into being in your tpl.php file.

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 77
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.