Here in the Netherlands we have a extra fee on electronic products so that old products can be disposed of in an environmental friendly way called "verwijderingsbijdrage". these costs are added to the Ubercart module that i am using by a remote module, and i want to show these amounts in the cart itself.
i would like to propose implementing a hook that allows you to add extra information, and read this information in the template (i have commented on the lines where i made changes):

file uc_ajax_cart.theme.inc changes to functi on uc_ajax_cart_preprocess_uc_ajax_cart_block_content:

function uc_ajax_cart_preprocess_uc_ajax_cart_block_content(&$vars) {
  $content = $vars['items'];
  $items = array();

  $context = array(
    'revision' => 'themed',
    'type' => 'price',
  );

  $total = 0;
  $total_items = 0;
  foreach ($content as $item) {
    $display_item = module_invoke($item->module, 'cart_display', $item);
    if (!empty($display_item)) {
          $attributes = $item->data['attributes'];
          $total += $display_item['#total'];
          $total_items += $display_item['qty']['#default_value'];
            $items[] = array(
              'nid'      => $display_item['nid']['#value'],
              'qty'      => t('@qty×', array('@qty' => $display_item['qty']['#default_value'])),
              'title'    => $display_item['title']['#value'],
              'descr'    => isset($display_item['description']['#value']) ? $display_item['description']['#value'] : FALSE,
              'link'     => url('node/' . $item->nid),
              'item'     => $item,
              'image'    => uc_product_get_picture($display_item['nid']['#value'], 'cart'),
              'node'     => node_load($item->nid),
              'total'    => uc_price($display_item['#total'], array(
                'revision' => 'themed-original',
                'type' => 'amount',
              )),
              'extra_info' => array(),  //BEGIN alteration Jasper Pasman 2011-06-22 
              'remove_link' => l(t('Remove product'),
                                  UC_AJAX_CART_REMOVE_CALLBACK,
                                  array(
                                  'attributes' => array(
                                                    'onclick' => "ajaxCartBlockUIRemove(this.href); return false;",
                                                    'class'    => 'remove-cart-link remove-cart-link-' . $display_item['nid']['#value'],
                                                  ),
                                  'query' => array(
                                    'nid' => $display_item['nid']['#value'],
                                    'destination' => $_GET['q'],
                                    'data' => base64_encode($display_item['data']['#value']),
                                    'action' => 'remove',
                                  ),
                                )
                              ),
            );
    }
  }
  //$vars['total'] = uc_price($total, $context);alteration Jasper Pasman: theming of info needs to be done after the hook implementation
  $vars['total'] = $total;  // alteration Jasper Pasman 2011-06-22
  $vars['items'] = $items;
  $vars['item_count'] = $total_items;
  $vars['cart_links'] = theme('uc_ajax_cart_cart_links');
  $vars['items_text'] = format_plural( $total_items , '<span class="num-items">@count</span> Item', '<span class="num-items">@count</span> Items');
  $vars['extra_info'] = array(); //alteration Jasper Pasman 2011-06-22    

  // Invoke hook_ajax_cart_block_content()
  foreach (module_list() as $module) {      
    $func = $module .'_ajax_cart_block_content';
    if (function_exists($func)) { 
        $func($vars);
    }
  }  
  $vars['total'] = uc_price($vars['total'], $context); 

}

changes to file templates/uc_ajax_cart_block_content.tpl.php:

// $Id: uc_ajax_cart_block_content.tpl.php,v 1.1.2.6 2010/05/01 12:42:30 erikseifert Exp $
/**
 * @file
 *
 * Theme file for non empty cart.
 */
?>
<div id="cart-block-contents-ajax">
  <table class="cart-block-items">
    <thead>
      <tr>
        <th colspan="4">
          <?php print t('Products')?>
        </th>
      </tr>
    </thead>
    <tbody>
      <?php foreach ( $items as $item ):?>
      <tr class="odd">
        <td class="cart-block-item-qty">
          <?php print $item['qty'] ?>
        </td>
        <td class="cart-block-item-title">
          <a title="<?php print check_plain($item['descr']); ?>" href="<?php print $item['link'] ?>"><?php print $item['title'] ?><?php print $item['descr'] ?></a>
        </td>
        <td>
          <?php print $item['total'] ?>
        </td>
        <td></td>
      </tr>
      <?php
        // alteration Jasper Pasman 2011-06-22: begin showing item's extra information
        foreach($item['extra_info'] as $extra_info_key => $extra_info_record) {
      ?>
      <tr class="odd">
        <td colspan="4" class="cart-block-item-title">
            <?php print $extra_info_record; ?>
        </td>
      </tr>
      <?php 
        } //END alteration Jasper Pasman 2011-06-22 
      ?>
    <tr class="odd">
        <td colspan="4" class="cart-block-item-desc">
          <?php print $item['remove_link'] ?>
        </td>
      </tr>
      <?php endforeach; ?>
    </tbody>
  </table>
</div>
<table>
  <tbody>
    <?php                                                                                                                                                               
      // alteration Jasper Pasman 2011-06-22: begin showing item's extra information                                                                                    
      foreach($extra_info as $extra_info_key => $extra_info_record) {                                                                                           
    ?>                                                                                                                                                                  
    <tr class="odd">                                                                                                                                                    
      <td colspan="4" class="cart-block-item-title">                                                                                                                    
          <?php print $extra_info_record; ?>                                                                                                                            
      </td>                                                                                                                                                             
    </tr>                                                                                                                                                               
    <?php                                                                                                                                                               
      } //END alteration Jasper Pasman 2011-06-22                                                                                                                       
    ?>  
    <tr>
      <td class="cart-block-summary-items">
        <?php print $items_text; ?>
      </td>
      <td class="cart-block-summary-total">
        <label><?php print t('Total'); ?>: </label><?php print $total ;?>
      </td>
    </tr>
    <tr class="cart-block-summary-links">
      <td colspan="2">
        <?php print $cart_links; ?>
      </td>
    </tr>
  </tbody>
</table>

implementation example of the hook:

/**
 * Implementation of hook_ajax_cart_block_content
 */
function MYMODULE_ajax_cart_block_content(&$vars){
    $verwijderingsbijdrage = 0; 
    $context = array(                                                                                                                                                             'revision' => 'themed',                                                                                                                                                   'type' => 'price',                                                                                                                                                
    ); 
    foreach($vars['items'] as $item_index => $item){
        $item_verwijderingsbijdrage = 1; // i use a personal function here ica_uc_verwijderingsbijdrage_product_get_verwijderingsbijdrage($item['node']->model, $item['qty']); but for the testing code here i have coimmented it, but it shows the idea
        $verwijderingsbijdrage_total = $verwijderingsbijdrage + $item_verwijderingsbijdrage;                                                                                   
        $vars['items'][$item_index]['extra_info'][] = t('verwijderingsbijdrage') .': ' .  uc_price($item_verwijderingsbijdrage, $context);
    }
    $vars['total'] = $vars['total'] + $verwijderingsbijdrage_total;
    $vars['extra_info'][] = '<label>' . t('Total') . ' '. t('verwijderingsbijdrage') . ': </label>' . uc_price($verwijderingsbijdrage_total, $context); 
}

it seems a very usefull function to me and i would love to see it implemented.

Comments

tunic’s picture

Status: Active » Needs work

It's indeed a useful feature. I've tested the posted code and it seems to work ok, but there's a problem: when in checkout added price info is not taken into account, so final price to be paid by customer is different from what cart block says.

So, a mechanism to update real ubercart price is needed. This must be done, I guess, when hook_ajax_cart_block_content is called.

Anyway this must go in 2.1, because a RC cycle is going on.... however I'll be happy to release 2.1 shortly before 2.0 if there are new features ready as this one ;)

koffieschaap’s picture

you are correct, the order itself needs to be update too. this i have indeed done with my sample with the hook hook_order when for example the total is calculated. the only item in the application that i could not alter in a proper way was the ajax cart. therefor i proposed this hook :)

with the hook form_alter on the form_id uc_cart_view_form and uc_order_edit_products_form i have added my own information as well with the use of line items

rhmtts’s picture

Issue tags: +3

subscribing

stewart.adam’s picture

Version: 6.x-2.0-rc1 » 6.x-2.x-dev
Status: Needs work » Postponed

I would approach this a little differently, ideally I would like the solution to maintain compatibility with both the regular cart block and the ajax cart block at the same time. This is much easier to do with the ajax cart block since we have an overridable template file, but what I mean is that hook_cart_item() or hook_uc_cart_alter() should be used to modify the cart items on the fly and then we can simply pass on this information in the cart $items to the template file.

stewart.adam’s picture

Issue tags: -3 +6.x-2.2 blocker

Tagging for 6.x-2.2 blocker.

stewart.adam’s picture

I'd like to get a smaller than planned 6.x-2.2 release out shortly to fix some bugs and unfortunately I do not think I will have enough time to investigate this by then. I am bumping this to 6.x-2.3 blocker.

sumaiyajaved’s picture

sumaiyajaved’s picture

subcribing