17 replies [Last post]
cakka's picture
Offline
Joined: 01/15/2009
Juice: 98
Was this information Helpful?

Hi, how to add column item price in cart page?
Thanks

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: Insert item price column in Cart page

Using TAPIr through hook_form_alter(): http://www.ubercart.org/docs/developer/7512/tapir_tables_api

cakka's picture
Offline
Joined: 01/15/2009
Juice: 98
Re: Re: Insert item price column in Cart page

Thanks, i will check it.

cakka's picture
Offline
Joined: 01/15/2009
Juice: 98
It's very confusing

Ryan, thanks for your help.
But, i am very confuse about that Laughing out loud
Can you tell me how the way step by step ?
Thanks

mlle.yotnottin's picture
Offline
Joined: 11/22/2008
Juice: 23
Try this out

I recently needed the same thing and finally came up with this. I'm not sure if this is the correct way of doing it but it seems to work for me.

I placed this in a custom module called "uc_custom" located under the modules/ubercart directory.

function uc_custom_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'uc_cart_view_form') {
$form['items']['#columns']['unit_price'] = array(
       'cell' => t('Price'),
       'weight' => 4,
'enabled' => true,
);
$form['items']['#columns']['qty']['weight'] = 5;
$form['items']['#columns']['total']['weight'] = 6;

$i = 0;
foreach ($form['#parameters'][2] as $item) {
$form['items'][$i]['unit_price']['#value'] = uc_currency_format($item->price);
$i = $i + 1;
}
}
}

cakka's picture
Offline
Joined: 01/15/2009
Juice: 98
how to alter table

@mlle.yotnottin
Thank you so much for your help.

I have do that, and print the unit price in table... but it was located not in the right place.. i think i must alter the table... how to alter the table...

Are that is using hook_table_alter or how ?

I want to display the table like :

Remove Products Price Qty Total
(checkbox) product_name 12,000 2 24,0000

or you can check for the capture image on http://img410.imageshack.us/img410/8617/cartpageilustrationzw9.jpg

Thanks

mlle.yotnottin's picture
Offline
Joined: 11/22/2008
Juice: 23
Re: how to alter table

Use the weight fields for each column to establish your column order.

I wanted the unit_price column to come before the qty and total columns so I set it's weight to 4. I then set the qty weight to be 5 and the weight for the total column to be 6 to make sure they displayed in the table where I wanted them to be.

Quick warning: this line: foreach ($form['#parameters'][2] as $item) { accesses the $form['#parameters'] array with an absolute index of 2. This array contains the data for each line item in the order but it may (or may not) actually show up at some index number other than 2. This part of the code needs some error checking to make sure you actually have the line item array and not something else.

Good luck!

Mlle.Yotnottin

cakka's picture
Offline
Joined: 01/15/2009
Juice: 98
I write the wrong codes

Ehm.. i try to modify the uc_cart_view_table for the correct input in table...
But i getting false... I don't know what the wrong...
Sorry for my acknowledgement , thanks

function custom_ubercart_form_alter(&$form, &$form_state, $form_id){
if ($form_id == 'uc_cart_view_form'){
$forms['items']['#column']['desc']['weight'] = 0;
$forms['items']['#column']['price'] = array(
'title' => t('Price(Rp)'),
'weight' => 1,
'enabled' => true,
);
$forms['items']['#column']['qty']['weight'] = 2;
$forms['items']['#column']['total']['weight'] = 3;

$i = 0;
foreach ($form['#parameters'][2] as $item) {
$form['items'][$i]['unit_price']['#value'] = uc_currency_format($item->price);
$i = $i + 1;
}
}

}

function custom_ubercart_table_alter(&$table, $table_id) {
if ($table_id == 'uc_cart_view_table'){
$table['#columns'] = array(
    'desc' => array(
      'cell' => '',
      'weight' => 0,
    ),
    'price' => array(
      'cell' => t('Price(Rp.)'),
      'weight' => 1,
    ),
    'qty' => array(
      'cell' => t('Qty.'),
      'weight' => 2,
    ),
    'total' => array(
      'cell' => t('Total'),
      'weight' => 3,
    ),
  );

  foreach (element_children($table) as $i) {
    $subtotal += $table[$i]['#total'];

    $table[$i]['desc']['#cell_attributes'] = array('width' => '100%', 'class' => 'desc');
    $table[$i]['price']['#cell_attributes'] = array('class' => 'price');
    $table[$i]['qty']['#cell_attributes'] = array('class' => 'qty');
    $table[$i]['total']['#cell_attributes'] = array('nowrap' => 'nowrap', 'class' => 'price');
    $table[$i]['#attributes'] = array('valign' => 'top');
  }
  $table[] = array(
    'total' => array(
      '#value' => '<strong>'. t('Subtotal:') .'</strong> '. uc_currency_format($subtotal),
      '#cell_attributes' => array(
        'colspan' => 'full',
        'align' => 'right',
        'nowrap' => 'nowrap',
        'class' => 'subtotal',
      ),
    ),
  );

  return $table;
}
}

cakka's picture
Offline
Joined: 01/15/2009
Juice: 98
They ask change

They ask some change, this is the capture that they are want to get.
http://img144.imageshack.us/img144/7781/carthbid8.jpg
Thanks

AttachmentSize
cart-hb.jpg 17.65 KB
cakka's picture
Offline
Joined: 01/15/2009
Juice: 98
I get it

Ok, i get it Laughing out loud
Thanks for your help... but the question.. how to disabled the column that not used ?? i have add 'disabled' => true but it's still displayed Laughing out loud
ehm...

cakka's picture
Offline
Joined: 01/15/2009
Juice: 98
I get it

IC, i get it.. it was solved. Laughing out loud
The las problem.. ehmm how to change qty column from text field to regular text ??
Thanks

attiks@drupal.org's picture
Offline
Joined: 10/08/2007
Juice: 16
cakka wrote:IC, i get it..
cakka wrote:

IC, i get it.. it was solved. Laughing out loud
The las problem.. ehmm how to change qty column from text field to regular text ??
Thanks

Based on uc_restrict code (and not tested)

/**
* Implementation of hook_form_alter().
*/
function uc_restrict_qty_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'uc_cart_view_form') {
    for ($i = 0, $j = count(uc_cart_get_contents()); $i < $j; $i++) {
      $form['items'][$i]['qty']['#type'] = 'value';
      $form['items'][$i]['qty']['#theme'] = 'restrict_qty_field';
    }
  }
}
PaulW's picture
Offline
Joined: 05/23/2008
Juice: 148
Removing column from cart table
Quote:

...how to disabled the column that not used ?? i have add 'disabled' => true but it's still displayed

You might try replacing

<?php
$table
[$i]['qty']['#cell_atributes'] = array ('class' => 'qty');
?>

with

<?php
$table
[$i]['qty']['#type'] = 'hidden';
?>

in your override of uc_cart_view_table.

OR

in your hook_form_alter(&$form, $form_state, $form_id), add

<?php
//Replace column header w/ nothing:
$form['items']['#columns']['qty']['cell'] = '';

foreach (

$form['#parameters'][2] as $item) {
 
//...other stuff here

  //Hide the 'qty' field in each row:
 

$form['items'][$i]['qty']['#type'] = 'hidden';

 

//...more stuff here
}
?>
attiks@drupal.org's picture
Offline
Joined: 10/08/2007
Juice: 16
Product kit

I needed the unit price as well, but I'm using product kits, so I changed it a bit. Depending on the type of product kit we have to adapt the code.

function attiks_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'uc_cart_view_form') {
    $form['items']['#columns']['unit_price'] = array(
      'cell' => t('Price'),
      'weight' => 4,
      'enabled' => true,
    );
    $form['items']['#columns']['qty']['weight'] = 5;
    $form['items']['#columns']['total']['weight'] = 6;

    // For product kits we can only set the unit price on the first item
    $product_kit_price = array();
    foreach ($form['#parameters'][2] as $i => $item) {
      if ($item->data['module'] == 'uc_product_kit') {
        if (!isset($product_kit_price[$item->data['unique_id']])) {
          // Load product kit to get the sell price
          $vid->vid = $item->data['kit_id'];
          $p = uc_product_kit_load($vid);
          switch ($p->mutable) {
            case -1: // As unit, no details
              $product_kit_price[$item->data['unique_id']]['id'] = $i;
              $product_kit_price[$item->data['unique_id']]['unit_price'] = $p->sell_price;
              break;
            case 0: // As unit, with list
              $product_kit_price[$item->data['unique_id']]['id'] = $i;
              $product_kit_price[$item->data['unique_id']]['unit_price'] = $p->sell_price;
              break;
            case 1: // As separate products
              $form['items'][$i]['unit_price']['#value'] = uc_currency_format($item->price);
              break;
          }
        }
      }
      else {
        $form['items'][$i]['unit_price']['#value'] = uc_currency_format($item->price);
      }
    }
    foreach ($product_kit_price as $item) {
      $form['items'][$item['id']]['unit_price']['#value'] = uc_currency_format($item['unit_price']);
    }
  }
}

arielgold's picture
Offline
Joined: 02/14/2010
Juice: 63
Re: Product kit

When I followed instructions here, the item price showed up, but the labels didn't line up with data. I have no idea why fiddling with the labels worked, but it did. (I also removed the remove column)

<?php
function capcart_form_alter(&$form, &$form_state, $form_id) {
  if (
$form_id == 'uc_cart_view_form') {
   
$form['items']['#columns']['price'] = array(
      
'cell' => t('Price'),
      
'weight' => 3,
      
'enabled' => TRUE,
      );
   
$form['items']['#columns']['desc']['cell'] = t('Price');
   
$form['items']['#columns']['qty']['cell'] = t('Total');
   
$form['items']['#columns']['total']['cell'] = '';
   
$form['items']['#columns']['price']['cell'] = t('Qty.');
   
$form['items']['#columns']['remove']['access'] = FALSE;

   

$form['items']['#columns']['products']['weight'] = 1;
   
$form['items']['#columns']['desc']['weight'] = 2;
   
$form['items']['#columns']['qty']['weight'] = 4;
   
$form['items']['#columns']['total']['weight'] = 5;
   
$i = 0;
    foreach (
$form['#parameters'][2] as $item) {
     
$form['items'][$i]['price']['#value'] = uc_currency_format($item->price);
     
$i = $i + 1;
    }
  }
}
?>
ek.senthil@gmail.com's picture
Offline
Joined: 07/21/2010
Juice: 3
How to display item price in cart page?

Hi Cakka,
I try custom_cart its not work for me . please help

shv_rk's picture
Offline
Joined: 08/16/2010
Juice: 112
hello, I need to add unit

hello,

I need to add unit price to my cart page too.
I was unable to do the method above...

any help?

Hobokoto's picture
Offline
Joined: 04/28/2010
Juice: 100
Re: Insert item price column in Cart page

The snippets people have been posting on this thread, and elsewhere, work great for the regular cart page.

But what about cart review table on the Checkout page? I'm still trying to get that going.

In exploration of this issue I'm simply trying to just affect that cart review table in SOME way but after going through 5 functions I still can't seem to be able to touch it. Like changing the title from "Cart Contents" to anything else. Am I missing something? I'm clear cache on the Performance page, and refreshing admin and/or module pages for the hell of it.