2 replies [Last post]
hybride's picture
Offline
Joined: 01/02/2011
Juice: 7
Was this information Helpful?

Hi everyone,

This is the first time I've actually developed/enhanced Ubercart, so am very excited (and nervous!). I know there were previous topics on the forum that asked about updating quantity on checkout, and I have an almost perfect version so far for Ubercart 1.x (the system am working on is on Drupal 5 for now, before we upgrade).

I have two questions, one for the updating and the other for a separate part of ubercart.

1. Am modifying the code in uc_cart/uc_cart_checkout_pane.inc, one of the last functions "function theme_cart_review_table". The following is my code:

<?php
function theme_cart_review_table($show_subtotal = FALSE) {
$items = uc_cart_get_contents();
 
$subtotal = 0;
 
$output = '<table class="cart-review"><thead>'
          
.'<tr class="first last odd"><td class="first odd qty">'. t('Qty')
           .
'</td><td class="even products">'. t('Products')
           .
'</td><td class="last odd price">'. t('Price')
           .
'</td></tr></thead><tbody>';

 

$row = 1;
  for (
$i = 0; $i < count($items); $i++) {
   
$item = $items[$i];

   

$rows = array();
    foreach (
$item->options as $option) {
     
// $rows[] = $option['attribute'] .': '. $option['name'];
     
$rows[] = t('@attribute: @option', array('@attribute' => $option['attribute'], '@option' => $option['name']));
     }
   
$desc = check_plain($item->title) . theme('item_list', $rows, NULL, 'ul', array('class' => 'product-options'));
   
$total = ($item->qty) ? $item->qty * $item->price : $item->price;
   
$subtotal += $total;
   
$qty = ($item->qty) ? $item->qty : "";
   
$qty2 = $qty;
   
$tr_class = ($i % 2 == 0) ? 'even' : 'odd';
    if (
$show_subtotal && $i == count($items)) {$tr_class .= ' last';}
   
// this is for the output on the checkout page
   
$output .= '<tr class="'. $tr_class .'"><td class="qty"><input type="text" size="2" class="qty" name="qty2" value="'.
           
$qty .'" /></td><td class="products">'
            
. $desc .'</td><td class="price">'. uc_currency_format($total)
              .
'</td></tr>';
  }
   if (
$show_subtotal) {
   
$tr_class = ($tr_class == 'even') ? 'odd' : 'even';
   
$output .= '<tr class="'. $tr_class .' last"><td class="subtotal" '
             
.'colspan="4"><span id="subtotal-title">'. t('Subtotal:')
              .
'</span> '. uc_currency_format($subtotal) .'</td></tr>';
  }
  
$output .= '<tr><td><input type="submit" name="update" value="Update Quantity"></td></tr></tbody></table>';
  
$qty2 = $_POST['qty2'];
  if(isset(
$_POST['update'])) {
      
cache_clear_all();
    
uc_product_update_cart_item($item->nid, $item->data, $qty2, $item->cart_id); 
  }
  return
$output;
}
?>

The issue I have is this: the update button works, BUT despite trying to clear the cache initially and have the quantity update, when you press update, you actually have to refresh the page to see the update. So, if I have originally quantity 2, and I change it to 5, then press submit, the page changes, the quantity stays at 2 UNTIL I refresh the page again, and then it correctly shows 5. If I submit/review the order to the next page, the quantity actually changes to 5 the first time. Am thinking it's Javascript that I need to do an automatic update the first time around, but am not very good with that. I tried also doing the clear all, update, and then a forced refresh but that didn't work. Any ideas?

2. How would I show that billing address for only users who select credit card or Paypal checkout payments? I also use checks as a method of payment and do not need it to show for those users.

Thank you!

hybride's picture
Offline
Joined: 01/02/2011
Juice: 7
Re: Updating quantity on checkout (almost complete) + modifying

I figured out so far the update issue.

I changed:

<?php
if(isset($_POST['update'])) {
      
cache_clear_all();
    
uc_product_update_cart_item($item->nid, $item->data, $qty2, $item->cart_id);
  }
?>

to

<?php
if(isset($_POST['update'])) {
    
uc_product_update_cart_item($item->nid, $item->data, $qty2, uc_cart_get_id());
    
header("Location: /cart/checkout");
  }

?>

which effectively removes the update issue. Maybe not the best way, but it works for me. For anyone needing the update on the checkout page, please don't hesitate to use the previous code. Complete code is below:

<?php
function theme_cart_review_table($show_subtotal = FALSE) {
$items = uc_cart_get_contents();
 
$subtotal = 0;
 
$output = '<table class="cart-review"><thead>'
          
.'<tr class="first last odd"><td class="first odd qty">'. t('Qty')
           .
'</td><td class="even products">'. t('Products')
           .
'</td><td class="last odd price">'. t('Price')
           .
'</td></tr></thead><tbody>';

 

$row = 1;
  for (
$i = 0; $i < count($items); $i++) {
   
$item = $items[$i];

   

$rows = array();
    foreach (
$item->options as $option) {
     
$rows[] = t('@attribute: @option', array('@attribute' => $option['attribute'], '@option' => $option['name']));
    }
   
$desc = check_plain($item->title) . theme('item_list', $rows, NULL, 'ul', array('class' => 'product-options'));
   
$total = ($item->qty) ? $item->qty * $item->price : $item->price;
   
$subtotal += $total;
   
$qty = ($item->qty) ? $item->qty : "";
   
$qty2 = $qty;
   
$tr_class = ($i % 2 == 0) ? 'even' : 'odd';
    if (
$show_subtotal && $i == count($items)) {$tr_class .= ' last';}
   
// this is for the output on the checkout page
   
$output .= '<tr class="'. $tr_class .'"><td class="qty"><input type="text" size="2" class="qty" name="qty2" value="'.
           
$qty2 .'" /></td><td class="products">'
            
. $desc .'</td><td class="price">'. uc_currency_format($total)
              .
'</td></tr>';
  }
   if (
$show_subtotal) {
   
$tr_class = ($tr_class == 'even') ? 'odd' : 'even';
   
$output .= '<tr class="'. $tr_class .' last"><td class="subtotal" '
             
.'colspan="4"><span id="subtotal-title">'. t('Subtotal:')
              .
'</span> '. uc_currency_format($subtotal) .'</td></tr>';
  }
  
$output .= '<tr><td><input type="submit" name="update" value="Update Quantity"></td></tr></tbody></table>';
  
$qty2 = $_POST['qty2'];
  if(isset(
$_POST['update'])) {
    
uc_product_update_cart_item($item->nid, $item->data, $qty2, uc_cart_get_id());
    
header("Location: /cart/checkout");
  }
  return
$output;
}
?>
jazzdrive3's picture
Offline
Joined: 03/29/2009
Juice: 221
Re: Re: Updating quantity on checkout (almost complete) + modify

Thanks for the hints you've provided here. They helped.

However, I would add a theme override function in your template.php file instead of changing the uc_cart file itself. It will make things easier in the long run.