Post by moosetoga » Fri Jan 16, 2009 9:53 pm

I made some changes to the way the shopping cart is displayed, and wanted to share my code. Not a major change, but maybe someone else will find this useful. You can play with it here (I'm still working on our site so there may be some design changes today, but the cart's pretty much good to go).

I took the cart out of the sidebar and put it into a floated div at top-right. Then I had it display the cart's contents in a list - see cart1.png attached. If there are more than three different products (regardless of the quantity of each), you get cart2.png - this is to keep the list from getting too long and pushing the rest of the page down.

Code: Select all

<?php 

//numtostr function found here - http://www.roscripts.com/snippets/show/53 

function numtostr($num){
    $big = array(    '', 
                    'thousand', 
                    'million', 
                    'billion', 
                    'trillion', 
                    'quadrillion', 
                    'quintillion', 
                    'sextillion', 
                    'septillion');
    $small = array(    '', 
                    'one', 
                    'two', 
                    'three', 
                    'four', 
                    'five', 
                    'six', 
                    'seven', 
                    'eight', 
                    'nine', 
                    'ten', 
                    'eleven', 
                    'twelve', 
                    'thirteen', 
                    'fourteen', 
                    'fifteen', 
                    'sixteen', 
                    'seventeen', 
                    'eighteen', 
                    'nineteen');
    $other = array(    '',
                    '',
                    'twenty', 
                    'thirty', 
                    'fourty', 
                    'fifty', 
                    'sixty', 
                    'seventy', 
                    'eighty', 
                    'ninety');
    $hun = 'hundred';
    $end = array();
    $num = strrev($num);
    $final = array();
    for($i=0; $i<strlen($num); $i+=3){
        $end[$i] = strrev(substr($num, $i, 3));
    }
    $end = array_reverse($end);
    for($i=0; $i<sizeof($end); $i++){
        $len = strlen($end[$i]);
        $temp = $end[$i];
        if($len == 3){
            $final[] = $temp{0} != '0' ? $small[$end[$i]{0}] . ' ' . $hun : $small[$end[$i]{0}];
            $end[$i] = substr($end[$i], 1, 2);
        }
        if($len > 1){
            $final[] = array_key_exists($end[$i], $small) ? $small[$end[$i]] : $other[$end[$i]{0}] . ' ' . $small[$end[$i]{1}];
        }else{
            $final[] = $small[$end[$i]{0}];
        }
        $final[] = $temp != '000' ? $big[sizeof($end) - $i - 1] : '';
    }
    return str_replace(array('  ', '  ', '  ', '  ', '  ', '  ', '  '), ' ', implode(' ',$final));
}

//end numtostr function

?>

<?php 


if ($products) { ?>
<?php 
    
    $howmany = 0;  //how many different products are in the cart (ignoring the quantity of each) - so if 3 of item A, 4 of item B, 2 total
    $howmany_total = 0; //how many total items - so if 3 of item A, 4 of item B, 7 total
    foreach ($products as $product) { 
    	$howmany = $howmany + 1;
        $howmany_total = $howmany_total + $product['quantity'];
    
     } 
     
     if ($howmany > 3) {
     
     	echo "There are " . numtostr($howmany_total) . " total items in your cart.";
     }
     
     else if ($howmany = 1 || $howmany = 2  || $howmany = 3 ) { ?>
<?php 
      
       $prodcounter = 0;
      
      foreach ($products as $product) { 
      
     $prodcounter = $prodcounter + 1;
      
      ?>
<?php if ($prodcounter == 1) { $product_string = ucwords(numtostr($product['quantity'])) . " of <a href=\"" . $product['href'] . "\">" . trim($product['name']) . "</a>"; } else {  $product_string .= ", " . numtostr($product['quantity']) . " of <a href=\"" . $product['href'] . "\">" . trim($product['name']) . "</a>"; } } }
      
      if (!empty($product_string)) { echo $product_string; }
      
       ?>

<br />
<?php echo $text_subtotal; ?> <span style="color:green;"><?php echo $subtotal; ?></span>   <strong><a href="<?php echo $view_cart; ?>"><?php echo $text_view_cart; ?></a></strong>
<?php } else { ?>
<div class="b"><?php echo $text_empty; ?></div>
<?php }
    
     ?>

Attachments

???
cart2.png
???
cart1.png

Newbie

Posts

Joined
Thu Jan 15, 2009 5:18 am
Who is online

Users browsing this forum: No registered users and 3 guests