11 replies [Last post]
jrowny's picture
Offline
Joined: 01/08/2009
Juice: 297
Was this information Helpful?

I just figured this out and I'm pretty exited.

function custom_cart_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'uc_cart_view_form') {

//Here I just remove the title of the image column
$form['items']['#columns']['image']['cell'] = '';

//Now I am moving around the columns by adjusting the weight
$form['items']['#columns']['image']['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]['image']['#value'] = '<img src="' . base_path() . node_load($form['items'][$i]['nid']['#value'])->field_cart_image[0]['filepath'] . '" alt="Product Image" />';
}

}
}

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

Pretty cool eh? I'm starting to get the hang of this Ubercart/Drupal thing. This will be my third store and my most custom cart.

wildleaf's picture
Offline
Joined: 04/07/2009
Juice: 8
an improvement

You can get the number of items in the cart by doing the following.

count(uc_cart_get_contents())

so the code would be

for($i=0;$i<count(uc_cart_get_contents());$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]['image']['#value'] = '<img src="' . base_path() . node_load($form['items'][$i]['nid']['#value'])->field_cart_image[0]['filepath'] . '" alt="Product Image" />';
dj_mystic82's picture
Offline
Joined: 11/19/2008
Juice: 48
Re: an improvement

I've successfully managed to customise the /cart page columns using the above example.

Is it possible to alter the table on the /cart/checkout page which shows the "cart contents"?

wildleaf's picture
Offline
Joined: 04/07/2009
Juice: 8
Yes, you can. You need a

Yes, you can.

You need a custom module and put the condition in the form_alter hook.
For example, the following code remove the qantity column.

function YOURMODULE_form_alter(&$form, $form_state, $form_id) {
  switch($form_id){
    case 'uc_cart_view_form':
        //disable qty column:
        unset($form['items']['#columns']['qty'];
        break;
  }
}
kazah's picture
Offline
Joined: 08/04/2010
Juice: 20
Re: Yes, you can. You need a

How can I add sku column?

aneuryzma's picture
Offline
Joined: 07/06/2010
Juice: 196
Re: Re: Yes, you can. You need a

There should be an option in cart settings

samdds's picture
Offline
Joined: 08/17/2010
Juice: 72
Hello, I've tried exactly

Hello,
I've tried exactly what you said, but I get an error on the Modules-page:

This module is not compatible with version 6.16

How can this be solved?

wjaspers's picture
Offline
Joined: 06/30/2010
Juice: 41
Corrections for Drupal 6.16+ Ubercart 2

I doubt this is the right place to use this; but, in my page template file, I've added the following lines.
This methodology will require PHP5.1+ (whatever PHP5 version stdClass was introduced in)

FYI:

<?php
count
(uc_cart_get_contents());
?>

will only return the number of unique items in the cart; not the total items!

EDIT: Accidentally forgot to calculate the extended price of products in-cart. Modify as you see fit.

<?php
  $cart
= uc_cart_get_contents();
 
$itemsInCart = 0;
 
$subTotal = 0;
  foreach(
$cart as $product )
  {
   
// don't bother using the array keys
   
$itemsInCart += $product->qty;
   
$subTotal += ($product->qty * $product->price);
  }
?>

The following was ported from uc_microcart (I can't remember what thread I found this in here; but the module is available at: www.drupal.org/project/uc_microcart ).

<?php
  $label
= ($itemsInCart==1)? 'item':'items';
 
$img = theme_image(drupal_get_path('module', 'uc_microcart') . '/images/cart1.gif');
 
$txt  = sprintf('%d %s $%01.2f', $itemsInCart, $label, $subTotal);
?>

<?php
/************************************************
    Create a micro-cart that displays
    total number of products in-cart
    and total cost.
/************************************************/
?>

<div id="myCart" class="wrap">
<a href="/cart"
title="View your cart."
rel="nofollow"
id="viewMyCart">
  <?php print $img; ?>
  <?php print $txt; ?>
</a>
</div>

Hope this helps. Works in Drupal 6.16+ Ubercart 2.x+

samdds's picture
Offline
Joined: 08/17/2010
Juice: 72
Re: Corrections for Drupal 6.16+ Ubercart 2

Is this for the cart page? or for the little cart?
Because I need a modification of the cart page

I don't really get why the module uc_microcart should be installed

wjaspers's picture
Offline
Joined: 06/30/2010
Juice: 41
Re: Re: Corrections for Drupal 6.16+ Ubercart 2

Microcart doesn't modify the "cart" view. It is an additional block that allows a site developer to create a very simple, yet detailed link to the user's cart. It does nothing more than list the total items in the user's cart, and the total price.

I don't know much about modifying the "cart" view; but my best guess would be that it involves overriding uc_cart_view() and creating a template file that phptemplate will automatically find.

Try these two threads: (I didn't look in depth to see what Ubercart compatibility these may be, but give it a shot).
http://www.ubercart.org/forum/theming_and_design/11296/theming_cart_page
http://www.ubercart.org/forum/support/11025/how_do_i_create_new_theme_fi...

kazah's picture
Offline
Joined: 08/04/2010
Juice: 20
Re: How to customize the cart in Drupal 6/Ubercart 2

Do you know how to display sell price field multiple the qty field for evry product the user select on the checkout page?

I mean i would like to remove the checkout contents and use views for this purpose!
I have two main problems:

1. I can't relate it to the certain user (can't display his cart contents);
2. how to count the selling price in that way: sell price multiply the qty that user select;

Has anybody solved this problem?

guldi's picture
Offline
Joined: 01/31/2008
Juice: 8
Thanks!

Thanks man!
Very helpful!