It would be nice to have an additional column displaying the 'per unit' item price in the shopping cart.

Support for this feature was expressed here and in subsequent comments: http://drupal.org/node/529110#comment-2231154

CommentFileSizeAuthor
#1 cart-unit-price.diff2.31 KBSeanA

Comments

SeanA’s picture

Status: Active » Needs review
StatusFileSize
new2.31 KB

Here's a patch against the most recent dev release. Use -p1 when applying.

Cablestein’s picture

Issue tags: +Ubercart, +price, +UC, +item, +cart, +total, +items, +shopping cart

You can achieve this through a different method...

Put the following code into your own module, enable the module, and voila it works.

Originally the code is from this guy, on his webpage... but I'm posting it here as it's more relevant and safer (ie will be here even if his site goes down).
http://pixeljets.com/blog/adding-price-field-ubercart-cart-page

QUESTION: I should know this, but can't the same code just be pasted into template.php ? I don't know. Anyways this works for me and I'm moving on.

QUESTION 2: Now how about the order overview page on the 1st checkout screen? That should also show unit cost too. I'm surprised this stuff hasn't been in place since Ubercart day 1.


function mymodule_tapir_table_alter(&$table, $table_id) {
  if ($table_id != 'uc_cart_view_table') return;
  
  // Register new column to tapir
  $table['#columns']['price_item'] = array(
    'cell' => t('Price'),
    'weight' => 2.5,
  );
  
  foreach (element_children($table) as $key) {
    if (empty($table[$key]['nid']['#value'])) continue;
      
      // This db-expensive node_load call can be easily avoided by using 
      // single SQL select. But, 
      // that's not so important if visitor usually adds 1-5 products to cart.
      $n = node_load($table[$key]['nid']['#value']);
      $p = uc_price($n -> sell_price, array());     // Add data to our new column
      $table[$key]['price_item'] = array(
         '#value' => $p, 
         '#cell_attributes' => array('class' =>'price-item')
      );   
  }
}

 
tr’s picture

Status: Needs review » Closed (won't fix)
Issue tags: -Ubercart, -price, -UC, -item, -cart, -total, -items, -shopping cart

@SeanA: Ubercart lets you alter the tables to add or remove any column you want, that way everyone gets to pick and choose if the defaults don't suit you. Making a unit price part of the defaults like in your patch may suit you but I guarantee someone else will ask how to remove it. So I don't see any need to change the defaults as long as we have a mechanism that lets you alter the table.

@Cablestein: Thank you for the code demonstrating this. (A little excessive on the tags. Please read http://drupal.org/node/1023102. Tags are not needed for search purposes.)

Now that Views is capable of displaying this sort of thing, we've started to use it to take the place of purpose-built code. If the cart tables were rebuilt with Views, then Views will provide a UI for changing the table without coding. That's something that will probably happen in the future.

SeanA’s picture

TR, as Cablestein said: "I'm surprised this stuff hasn't been in place since Ubercart day 1." Displaying the unit price is standard across many carts. It totally makes sense to display the unit prices by default. Whatever.

I need to use a custom mini-module for this? "This db-expensive node_load call can be easily avoided by using..." my hack. Much more efficient.

korayal’s picture

Cablestein's solution worked for me, but is it also possible to make the same update on the cart table in the checkout page?

I tried the same on "uc_cart_review_table" but it didn't work.

windtrader’s picture

Category: feature » support
Status: Closed (won't fix) » Active

>> @SeanA: Ubercart lets you alter the tables to add or remove any column you want, that way everyone gets to pick and choose if the defaults don't suit you. Making a unit price part of the defaults like in your patch may suit you but I guarantee someone else will ask how to remove it. So I don't see any need to change the defaults as long as we have a mechanism that lets you alter the table. <<

@TR - I looked high and low in the Ubercart documents for a way to modify the columns in the shopping cart and just did not find anything official. Can someone point me to an existing feature within UC to do this or do I need to use custom methods like those described in this post?

tr’s picture

Category: support » feature
Status: Active » Closed (won't fix)

@Cablestein described how to do it, in #2.

liestc’s picture

Cablestein's solution worked for me, but is it also possible to make the same update on the cart table in the checkout page?

I tried the same on "uc_cart_review_table" but it didn't work.
_____________________________________________________________________
[url=http://www.eurdresses.com]dresses for a wedding[/url]
[url=http://www.eurdresses.com]bride dresses[/url]

Cablestein’s picture

I still need to get this happening in the cart review table on the Checkout page.

In exploration of this issue I'm simply trying to just affect that 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.

lancewig’s picture

This works fine and great until you add attributes (with prices) to your products. It gets a whole lot more complicated at that point because you have to grab the serialized data field, unserialize it, query the 'uc_attributes_options' table and then add that to the existing price (but only if there is an attribute in the first place). I hope somebody can benefit from me staying up til 3 in the morning fighting this. I can't help but think there is a better way to write this but I got it done. Client was pissed because Ubercart doesn't have a 'Unit Price' column unlike just about every other cart out there. The problem was they had products like cards where say, "a single card" is $3.50 and then "a box of 8" is $16. I had a "sell price" set up as $3.50 and an attribute of $12.50 (to make it $16).

EDIT: I have to wonder if it's as simple and dividing $total by $quantity because your already have those variables to work with.

function mymodule_tapir_table_alter(&$table, $table_id) {
  if ($table_id != 'uc_cart_view_table') return;
  
  // Register new column to tapir
  $table['#columns']['unit_price'] = array(
    'cell' => t('Price'),
    'weight' => 2.5,
  );
    
  foreach (element_children($table) as $key):
    if (empty($table[$key]['nid']['#value'])) continue;
      
      // This db-expensive node_load call can be easily avoided by using 
      // single SQL select. But, 
      // that's not so important if visitor usually adds 1-5 products to cart.
      $n = node_load($table[$key]['nid']['#value']);
      $p = uc_price($n -> sell_price, array());     // Add data to our new column
   
      // Need to find the attribute inside the serialized data string.
      $data = unserialize($table[$key]['data']['#value']);
 
      // Find the Attribute ID and the Option ID
      if(!empty($data["attributes"])):  
        foreach($data["attributes"] as $aid=>$oid): 
          $result = db_result(db_query("SELECT price FROM {uc_attribute_options} ao WHERE ao.aid = %d AND ao.oid = %d", $aid, $oid)); // Run the query
          // We're looking for just one attribute, the one with a price, still we have to find it.
          // We know we have the right one if a price is set.
          if ($result > 0):
            $p = "$" . number_format($n->sell_price + $result, 2);
          endif; 
        endforeach;
	  endif; // End checking for attributes
   
      $table[$key]['unit_price'] = array(
         '#value' => $p, 
         '#cell_attributes' => array('class' =>'price-item')
      );
      // Reset the attribute variable
      $price_add = 0;
  endforeach;
} 
saidoze’s picture

instead of

      $table[$key]['price_item'] = array(
         '#value' => '<span class="price-item-unitprice">' . $p . '</span>',
         '#cell_attributes' => array('class' =>'price-item')
      );  

is use

      $table[$key]['price_item'] = array(
         '#markup' => '<span class="price-item-unitprice">' . $p . '</span>',
         '#cell_attributes' => array('class' =>'price-item')
      );  

#value -> #markup

only then it displays

loparr’s picture

How about displaying custom cck field inside cart table? With availability details for example? Thank you