Location: PHPKode > projects > Caleb's Code > Cafe_Cart/classes/cart.lib.php
<?
$auth_str = $ModName . "::";

if($pnconfig[Version_Sub] == "Rogue")
{
    if (!authorised(0, $auth_str, "::", ACCESS_READ)) die ("Permission denied.");
    if (!defined("LOADED_AS_MODULE")) die ("You can't access this file directly...");
}
else
{
    if (!eregi("modules.php", $PHP_SELF)) die ("Access Denied");
}
/////////////////////////
sessionInit();
$table = $pntable[cp_cart];
$time = date("Ymdhis");
$debug = $cpconfig[debug];
/////////////////////////
function updateProduct($product_id,$quantity,$size)
{ 
    global $table,$time,$session_id,$debug;
    $update = "";
    $cart_update_query = "UPDATE $table 
                          SET
                          `size`       = '$size',
                          `quantity`   = '$quantity',
                          `time`       = '$time'
                          WHERE 
                          `session_id` = '$session_id'
                          AND
                          `product_id` = '$product_id'";

    if($debug) $update .= $cart_update_query . "<BR><BR>\n";
    if(!mysql_query ($cart_update_query)):
        $update .= "Updating Product Query Failed<BR><BR>" . mysql_error();
    else:
        $update .= "Shopping Cart Updated";
    endif;
    
    return $update;
}
/*
$id = "1";
$quantity = "3";
$size = "3";

print updateProduct($id,$quantity,$size);
*/
//////////////////////////
function viewCart()
{
    global $session_id,$table,$pntable,$ModName,$debug,$product_size_names,$cpconfig;
    $gui = $cpconfig[gui];
    $product_query = "SELECT `prod`,`name`,`ptype`,`charge`,`store`,`description`,`def_view`,`active` FROM `$pntable[cp_products]` WHERE 1";
    $product_stmt = mysql_query($product_query) or die("Product Query Failed");
    while($p = mysql_fetch_array($product_stmt)):
        extract($p,EXTR_PREFIX_ALL,"product");
        $prod_count++;
        $product_desc_array[$product_prod] = $product_description;
        $product_price_array[$product_prod] = $product_charge;
    endwhile;
    $cart = "<!-- BEGIN CART OUTPUT -->";
    $cart_query = "SELECT product_id,size,quantity FROM $table WHERE session_id='$session_id' AND `quantity` > 0";
    $cart_items = mysql_query($cart_query) or die("Shopping Cart Query Failed");
    if($debug) $cart .= $cart_query . "<BR><BR>\n";
    $q = mysql_num_rows($cart_items);
    $cart .= "<form method=post>";
    $cart .= "\n <table border=0 cellpadding=3 cellspacing=1 bgcolor=\"$bgcolor2\" width=\"100%\">\n";
    $cart .= "<tr valign=\"top\">\n<td colspan='4' align=\"right\" valign=\"top\">";
    $cart .= "<a href=\"$cpconfig[storeURL]\"><b>Back To Store</b></a>&nbsp;&#149;&nbsp;";
    $cart .= "<a href=\"$cpconfig[helpURL]\" target=\"_blank\">";
    if($gui):
        $cart .= "<img alt=\"Customer Service\" border=0 src=\"modules/$ModName/images/help.gif\">";     
    else:
        $cart .= "<b>Help</b>";
    endif;
    $cart .= "</a>&nbsp;&nbsp;";
    $cart .= "<a href=\"$cpconfig[checkURL]\" target=\"_blank\">";
    if($gui):
        $cart .= "<img alt=\"Pay by Check or Money Order\" border=0 src=\"modules/$ModName/images/check_icon.gif\">";     
    else:
        $cart .= "<b>Check/Money&nbsp;Order</b>";
    endif;
        $cart .= "</a>&nbsp;&nbsp;";
    $cart .= "<a href=\"$cpconfig[privacyURL]\" target=\"_blank\">";
    if($gui):
        $cart .= "<img alt=\"About Privacy\" border=0 src=\"modules/$ModName/images/security.gif\">";
    else:       
        $cart .= "<b>About&nbsp;Privacy</b>";
    endif;
    $cart .= "</a></td>\n</tr>";
    if($q == 0):
        $cart_empty = true;
        $cart .= "<tr>\n<td colspan='4'>No Products Found</td>\n</tr>";
    else:
        $cart .="  <tr style=\"font-weight: bold;\">\n"
              ."   <td align=\"center\" width=\"10%\"><b>Quantity</b></td>\n"
              ."   <td align=\"left\" width=\"60%\"><b>Description&nbsp;/&nbsp;Size</b></td>\n"
              ."   <td align=\"right\" width=\"20%\"><b>Price</b></td>\n"
              ."   <td align=\"center\" width=\"5%\" align=center>&nbsp;</td>\n"
              ."  </tr>\n";
    endif;
    while ($r = mysql_fetch_array($cart_items)): 
        extract($r,EXTR_PREFIX_ALL,"cart"); 
        $count++;

        $base_price = $product_price_array[$cart_product_id];
        if($cart_size == "8" || $cart_size == "9" || $cart_size == "10"):        
            $item_price = ($base_price + "3.00");
        else:
            $item_price = $base_price;
        endif; 
        $item_price = sprintf("%01.2f",$item_price);
        $price = sprintf ("%01.2f",($item_price * $cart_quantity));
        $total_price = sprintf ("%01.2f",($total_price + $price));

        if($count % 2):
            $cart .= "<tr class=list>\n";
        else:
            $cart .= "<tr>\n";
        endif;

        $updateURL = "modules.php?op=modload&file=index&cpop=show_detail"
                  . "&name=" . $ModName
                  . "&product=" . $cart_product_id
                  . "&selected_size=" . $cart_size
                  . "&quantity=" . $cart_quantity;

        $cart .= "<td align=\"center\" valign=\"top\"><a href=\"$updateURL\">$cart_quantity</a></td>";
        $cart .= "<td align=\"left\" valign=\"top\"><a href=\"$updateURL\">" . $product_desc_array[$cart_product_id] . "</a>";
        if($cart_size != "0") $cart .= "<BR><i>Size:&nbsp;" . $product_size_names[$cart_size] . "</i>";
        $cart .= "</td>";
        $cart .= "<td align=\"right\" valign=\"top\">\$" . $item_price . "</td>\n";
        $cart .= "<td align=\"center\" valign=\"top\"><a href=\"$updateURL\">";
        if($gui):
            $cart .= "<img src=\"modules/$ModName/images/admin_show.gif\" border=0 alt=\"Update\">";
        else:
            $cart .= "Update";
        endif;
        $cart .= "</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        $removeURL = "modules.php?op=modload&file=cart&cpop=deleteProduct"
                   . "&name=" . $ModName
                   . "&product_id=" . $cart_product_id
                   . "&size=" . $cart_size;
        $checkoutURL = "modules.php?op=modload&file=cart&cpop=checkout"
                   . "&name=" . $ModName;
        $cart .= "<a href=\"$removeURL\" valign=\"top\">";
        if($gui):
            $cart .="<img src=\"modules/$ModName/images/trash.gif\" border=0 alt=\"Remove\">";
        else:
            $cart .= "Remove";
        endif;
        $cart .= "</a></td>\n";
    endwhile;
    $cart .= "</tr>";
    if(!$cart_empty):
        $cart .="  <tr style=\"font-weight: bold;\">\n"
              ."   <td align=\"right\" colspan=\"3\"><b>Total:&nbsp;\$$total_price</b></td>\n"
              ."   <td align=\"right\"><a href=\"$checkoutURL\">";
        if($gui):
            $cart .= "<img alt=\"Secure Checkout\" border=0 src=\"modules/$ModName/images/checkout.gif\">";
        else:
            $cart .= "<B>CHECKOUT</B>";
        endif;
        $cart .= "</a></td>\n"
              ."  </tr>\n";
    endif;
    $cart .= "</table></form>";
    $cart .= "<!-- END CART OUTPUT -->";

    return $cart;
}
/*
print viewCart();
*/
///////////////////////////
function addProduct($product_id,$quantity,$size)
{
    global $session_id,$time,$table,$debug,$cpconfig;
    if(!isset($size) || empty($size)) $size = 0;
    $product = "";
    $cart_product_query = "SELECT `product_id` FROM $table WHERE `session_id`='$session_id' AND `size`='$size' AND `product_id`='$product_id' GROUP BY product_id";
    $cart_items = mysql_query($cart_product_query) or die("Shopping Cart Query Failed - [add cart]");
    if($debug) $product .= "Add Product SELECT Query:<BR>\n" . $cart_product_query . "<BR><BR>\n";
    $q = mysql_num_rows($cart_items);
    if($q > 0) 
        $product_in_cart = true;
    else
        $product_in_cart = false;
     
    $cart_add_query = "INSERT into $table (`product_id`,`size`,`quantity`,`session_id`,`time`) 
                       VALUES ('$product_id','$size','$quantity','$session_id','$time')";
    if($debug) $product .= "Add Product ADD Query:<BR>\n" . $cart_add_query . "<BR><BR>\n";

    $cart_update_query = "UPDATE $table 
                          SET
                          `quantity`   = `quantity` + '$quantity',
                          `time`       = '$time'
                          WHERE 
                          `product_id` = '$product_id'";
    if($debug) $product .= "Add Product UPDATE Query:<BR>\n" . $cart_update_query . "<BR><BR>\n";   
 
    if($product_in_cart)
    {
        if(!mysql_query ($cart_update_query)):
            $product .= "Updating Product Query Failed<BR><BR>" . mysql_error();
        else:
            $product .= "Product Updated";
        endif;
    }
    else
    {
        if(!mysql_query ($cart_add_query)):
            $product .= "Adding Product Query Failed<BR><BR>" . mysql_error();
        else:
            if($cpconfig[alert]) doAlert("Product Added");
            else $product .= "Product Added";
        endif;
    }

    return $product;
}
/*
$product_id = "1920321";
$quantity = "3";
$size = "3";

print addProduct($product_id,$quantity,$size);
*/
//////////////////////////////
function deleteProduct($product_id,$size)
{
    global $table,$session_id,$debug;
    $death_string = "Deleting Project Query Failed";
    if($debug) $death_string .= "<BR><BR>" . mysql_error();
    $cart_delete_query = "DELETE FROM $table 
                          WHERE `product_id`='$product_id'
                          AND
                          `size`='$size'
                          AND 
                          `session_id` = '$session_id'";
    $cart_delete_stmt  = mysql_query($cart_delete_query) or die($death_string);
    if(mysql_affected_rows() > 0):
        $delete = "Product Deleted";
    else:
        $delete = "Product Not Found in Cart";
    endif;
    return $delete;
}
/*
$id = "1907603";
print deleteProduct($product_id);
*/
//////////////////////////////
function checkOut()
{
    global $table,$session_id,$pnconfig,$cpconfig,$storeURL;
    $checkout_query = "SELECT product_id,size,quantity FROM $table WHERE session_id='$session_id' AND `quantity` > 0";
    $checkout_items = mysql_query($checkout_query) or die("Checkout Query Failed");
    if($debug) $checkout .= $checkout_query . "<BR><BR>\n";
    $q = mysql_num_rows($checkout_items);
    if($q > 0) $cart_empty = false;
    else $cart_empty = true;

    $checkoutURL = $cpconfig[checkout_url] 
                 . "?refid=" . $cpconfig[cart_account]
                 . "&returnpage=" . $cpconfig[returnpage]
                 . "&keepshopping=" . $cpconfig[keepshopping];
    while ($r = mysql_fetch_array($checkout_items)):
        extract($r,EXTR_PREFIX_ALL,"checkout");
        $count++;
        $checkoutURL .= "&" . "cp2_" . $checkout_product_id . "_" . $checkout_size . "=" . $checkout_quantity;
    endwhile;

    header("Location: $checkoutURL");
}

function printThanks()
{
    global $session_id,$pnconfig,$cpconfig,$table;

    $form = "<form method=\"POST\" action=\"\">";
    $form .= "</form>";
    echo $form;
}
?>
Return current item: Caleb's Code