The cart lists the product name. I'd like it also to list the sku number. What's the best way to accomplish that?
Administer->Store Administration->Configuration->Product Settings->Edit->Product Fields
Check Model, which is the SKU.
Is that what you were looking for?
That's already checked. I think that puts in on the product page, but doesn't affect the cart page.
Ahh...I see. I think in that case you'd want to use hook_table_alter() for the uc_product_table. Here is a start for what you might want. At the very least you'd probably want to add some theming.
<?php
function your_module_table_alter($table_id, $op, $args = null) {
if($table_id == 'uc_product_table') {
switch ($op){
case 'fields':
$fields[] = array('name' => 'model',
'title' => t('SKU'),
'weight' => 1,
'enabled' => true,
);
return $fields;
case 'data':
$obj = new stdClass();
foreach ($args['nids'] as $nid){
$obj->vid = $nid;
$prod = uc_product_load($obj);
$data['model'][] = $prod->model;
}
return $data;
}
}
}
?>After you've added that, you'd be able to toggle that field off and on, change the field label, and also arrange the order of it in Administer › Store administration › Configuration › Table display settings for uc_product_table.
Thanks for your help. That added it to the table admin page, but not to the cart page. How do I do that?
Can you post a screenshot of the page where you'd like the SKU to be added? When I add that code and enable that field in the table admin page I get the SKU to show up on what I would consider the 'cart page'. I think maybe we are having a terminology mixup of sorts.
uc_product_table is only used for the Catalog and Products views, not the cart view. The table you want to modify for the cart view is called, strangely enough, uc_cart_view_table. The code cYu provided above needs to be altered slightly to work with this different table - change the table name in the initial "if" statement then change the "data" case to read as follows:
<?php
case 'data':
$obj = new stdClass();
foreach ($args['#parameters'][1] as $nid) {
$obj->vid = $nid;
$prod = uc_product_load($obj);
$data['model'][] = $prod->model;
}
return $data;
?>See the "Extra colums" contribution from zmove at http://www.ubercart.org/contrib/763 for another example.
Thanks for clearing that up TR. I'm not sure why that wasn't clicking for me. 
Thanks for the help. It's closer, but still not quite working. I've emptied the cart, cleared the cache, and refilled the cart, and the new column is showing, but it's not putting any data in there.
I'm attaching two screenshots -- one showing the cart with the "model" column, but it's blank, and a second one showing the uc_cart_view_table admin page. I'm not sure where that blank row is coming from.
And here's my updated module_table_alter code:
function myModule_table_alter($table_id, $op, $args = null) {
if($table_id == 'uc_cart_view_table') {
switch ($op){
case 'fields':
$fields[] = array('name' => 'model',
'title' => t('SKU'),
'weight' => 1,
'enabled' => true,
);
return $fields;
case 'data':
$obj = new stdClass();
foreach ($args['#parameters'][1] as $nid) {
$obj->vid = $nid;
$prod = uc_product_load($obj);
$data['model'][] = $prod->model;
}
return $data;
}
}
}| Attachment | Size |
|---|---|
| uc_cart.jpg | 16.99 KB |
| uc_cart_table_admin.jpg | 22.97 KB |
Can you try replacing:
case 'data':
$obj = new stdClass();
foreach ($args['#parameters'][1] as $nid) {
$obj->vid = $nid;
$prod = uc_product_load($obj);
$data['model'][] = $prod->model;
}
return $data;with
case 'data':
foreach ($args['#parameters'][1] as $item) {
$data['model'][] = $item->model;
}
return $data;Didn't quite work for me. I am using beta6.
The product name column was thrown off, see screen attachment. I used the function below.
any help is greatly appreciated.
<?php
function mymodule_table_alter($table_id, $op, $args = null) {
if(
$table_id == 'uc_cart_view_table') {
switch ($op){
case 'fields':
$fields[] = array('name' => 'model',
'title' => t('SKU'),
'weight' => 1,
'enabled' => true,
);
return $fields;
case 'data':
foreach ($args['#parameters'][1] as $item) {
$data['model'][] = $item->model;
}
return $data;
}
}
}
?>| Attachment | Size |
|---|---|
| scr.gif | 27.03 KB |
Hello-
I'm looking to also add the SKU to the order view page as well.
What am I missing? When I go to the section listed above, I only have checkboxes to disable what already shows up in the cart view.... how do I add/ change this?
Found the sections in the ubercart files.....doesn't work.
I've managed to add a line so I get a SKU field... but I don't know how to make it retrieve the actual SKU.
Anyone?
Bueler?
Bueler?
Bueler?
jumonjii,
You need to create a custom module to show the sku (this is not as hard as it sounds)
1. In /sites/all/modules/ on your web server create a folder called uc_showsku.
2. In the uc_showsku folder create two text files uc_showsku.info and uc_showsku.module
3. Paste this code into uc_showsku.info
; $Id$
name = Show SKU
description = Add SKU column to Product and Cart tables
dependencies = uc_cart
package = "Ubercart - core (optional)"4. Paste this code into uc_showsku.module
<?php
/**
* Implementation of TAPIr's hook_table_alter().
*/
function uc_showsku_table_alter($table_id, $op, $args = null) {
if($table_id == 'uc_cart_view_table') {
switch ($op){
case 'fields':
$fields[] = array('name' => 'model',
'title' => t('SKU'),
'weight' => 1,
'enabled' => true,
);
return $fields;
case 'data':
foreach ($args['#parameters'][1] as $item) {
$data['model'][] = $item->model;
}
return $data;
}
}
}
5. Enable your module in www.yoursite.com/admin/build/modules
6. Enable "SKU" in www.yoursite.com/admin/store/settings/tables/uc_cart_view_table
THANKS!
So... any time you want to tweak anything, you make a module?
This is a WHOLE new world to me.
For the most part, you can keep adding tweaks to the same module. Most sites will have one eventually.
Right, okay... but for the most part, you leave the main scripts.... or mods alone. Then if you want to change how it does something, you make your own mod?
Anyone have an updated version of this code to add the sku/model to the cart page?
thank you
ITS NOT MY POST..I just change one line...for adding sku to cart table...and delete image column....
function custom_cart_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'uc_cart_view_form') {
//Here I just remove the image column
$form['items']['#columns']['image']['access'] = FALSE;
//Now I am moving around the columns by adjusting the weight
$form['items']['#columns']['model']['weight'] = 0;
$form['items']['#columns']['desc']['weight'] = 1;
$form['items']['#columns']['qty']['weight'] = 2;
$form['items']['#columns']['total']['weight'] = 3;
$form['items']['#columns']['remove']['weight'] = 4;
//There are 6 fields in this array other than the cart items, so I loop through and don't account for those 6... haha elegant!
for($i=0;$i<count($form['items'])-6;$i++){
//So... I don't know if there's another way to get the info from the node other than using node_load but since it comes from cache it's fast right?
$form['items'][$i]['model']['#value'] = node_load($form['items'][$i]['nid']['#value'])->model; // MY OWN STRING :))))))
}
}
}
So basically I'm looping through the cart items in the form and re-assigning the value of the image field to an image in the node that was defined by modifying the product content type.
Background Info
The client designed the cart! So the cart features an image that's 100% different than the product image. So I added a new image to the product and use this code above. Additionally the client wants the image first, remove last, and a few other small modifications which I have yet to make.
How to Use this Code
1. Create a folder in sites/all/modules called "custom_cart"
2. Create a custom_cart.info file:
name = Custom Cart
description = Customizes the ubercart cart display
dependencies[] = uc_cart
package = "Ubercart - core (optional)"
core = 6.x
3. Create a custom_cart.module file
4. paste above code.
5. Add an image field to your product content type (I called mine cart image)
6. Upload some cart imagse
7. Enable the new custom cart module



