7 replies [Last post]
himagarwal's picture
Offline
Joined: 06/24/2008
Juice: 295
Was this information Helpful?

Earlier I changed the core ubercart uc_cart_block_content() module to fit my website design. But came to know that template.php was the best way to go with it.

So I changed the http://www.ubercart.org/docs/api/theme_uc_cart_block_content to below code and added it to template.php exactly as shown below. I didn't had any problem loading the website but I felt that nothing has changed. Actually the only change that I made (starts from code "if ($item_count > 0)" below) in this file was that I added some tbody and other tags. But when I put below code in the ubercart core module it works. I'm still unaware what went wrong?

<?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)) {
   
$output .= '<table class="cart-block-table">'
             
.'<tbody class="cart-block-tbody">';
    foreach (
$items as $item) {
     
$output .= '<tr class="cart-block-item"><td class="cart-block-item-qty">'. $item->qty .'x</td>'
              
. '<td class="cart-block-item-title">'. l($item->title, 'node/'. $item->nid) .'</td>'
              
. '<td class="cart-block-item-price">'. uc_currency_format($item->price) .'</td></tr>';
      if (
is_array($item->data['attributes']) && !empty($item->data['attributes'])) {
       
$display_item = module_invoke($item->module, 'cart_display', $item);
       
$output .= '<tr><td colspan="3">'. $display_item['options']['#value'] .'</td></tr>';
      }
     
$total += ($item->price) * $item->qty;
     
$item_count += $item->qty;
    }

   

$output .= '</tbody></table>';
  }
  else {
   
$output .= '<p>'. t('There are no products in your shopping cart.') .'</p>';
  }

 

$output .= '</div>';

 

$item_text = format_plural($item_count, '1 Item', '@count Items');
 
$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>';
  if (
$item_count > 0) {
   
$output .= '</tbody></table><table id="cart-block-summary-checkout-table"><tbody><tr><td colspan="2">'. $view . $checkout .'</td></tr>';
  }
 
$output .= '</tbody></table>';

  return

$output;
}
?>
himagarwal's picture
Offline
Joined: 06/24/2008
Juice: 295
Re: template.php: Overriding other theme functions

Looking for some help.

himagarwal's picture
Offline
Joined: 06/24/2008
Juice: 295
Re: Re: template.php: Overriding other theme functions

Please help me. I'm waiting for some reply. Smiling

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: Re: Re: template.php: Overriding other theme functions

It'll still work from a module file, that's just not the right place to put it. It should go in your template.php file with the theme so you don't lose the code when you update your Ubercart version and overwrite that file.

himagarwal's picture
Offline
Joined: 06/24/2008
Juice: 295
Re: Re: Re: Re: template.php: Overriding other theme functions

Yes, I did placed it in the template.php file in the theme directory that I'm using but nothing happened. When I use the above code in module it worked but not in template.php? I'm not getting what is wrong with it?

I don't want to make any changes in ubercart core module for updates and security reasons. Smiling

mikejoconnor's picture
Offline
AdministratorBug FinderGetting busy with the Ubercode.
Joined: 08/07/2007
Juice: 536
Re: Re: Re: Re: Re: template.php: Overriding other theme functio

Have you taken a look @ http://drupal.org/node/11811

The code looks correct to me. The only thing I could consider changing is the name of the function. phptemplate_[theme function name] is the most broad. I've had instances in the past where changing phptemplate to the actual theme name has helped in this case zen_uc_cart_block_content() or what ever your theme name is

himagarwal's picture
Offline
Joined: 06/24/2008
Juice: 295
Re: Re: Re: Re: Re: Re: template.php: Overriding other theme fun

I used the theme name, though it didn't show any problem but it didn't work.

I just copied the "above code" below <?php string and it worked. Didn't know that the placement of the function in template file also affects.

Thanks to Ryan and Mike.

limbovski's picture
Offline
Joined: 09/12/2010
Juice: 32
Re: Re: Re: Re: Re: Re: Re: template.php: Overriding other theme

I'm having a similar issue. I'm new around here and I wonder - since this thread is so old has anything changed in a more recent version that would cause this not to work.

I wanted to theme the cart.
So I added this to my template.php file.
It's not getting called at all.
Am I doing this completely wrong or is there an error in the code?

function phptemplate_uc_cart_block_content(&$variables) {

global $user;

$items = uc_cart_get_contents();

$item_count = 0;
if (!empty($items)) {
foreach ($items as $item) {
$total += ($item->price) * $item->qty;
$item_count += $item->qty;
}
}

$cart_text = format_plural($item_count, '1 item', '@count items');
$price = uc_currency_format($total);
$output = '' . $cart_text . '' . uc_currency_format($total) . '';

$variables['cart_block'] = $output;
}