La classes Cart et les constantes globales qui vont bien :
<?php
session_start();
define("xCART", "cart"); // session cart[ product id index ]
define("xID" , "id"); // session cart[id][ id index ]
define("xREF" , "ref"); // session cart[id][ reference index ]
define("xQTY" , "qty"); // session cart[id][ quantity index ]
define("xNAME" , "nme"); // session cart[id][ name index ]
define("xPRICE" , "prc"); // session cart[id][ price index ]
if(!isset($_SESSION[xCART]) || !is_array($_SESSION[xCART])){
$_SESSION[xCART] = array();
}
define("MIN_CART_QTY", 0);
define("MAX_CART_QTY", 9999);
define("TAXE", 19.6);
/* --------------------------------------- */
class Cart{
/* Add product to cart
*/
static public function addProduct($id, $reference, $quantity, $price, $name){
if(!array_key_exists($id, $_SESSION[xCART])){
$_SESSION[xCART][$id][xID] = $id;
$_SESSION[xCART][$id][xREF] = $reference;
$_SESSION[xCART][$id][xQTY] = $quantity;
$_SESSION[xCART][$id][xPRICE] = $price;
$_SESSION[xCART][$id][xNAME] = $name;
} else {
self::setQuantity($id, $quantity);
}
}
/* delete product from cart (by product id)
*/
static public function deleteProduct($id){
if(array_key_exists($id, $_SESSION[xCART])){
unset($_SESSION[xCART][$id]);
return true;
}
return false;
}
/* set in cart product quantity (add or replace)
add (old qty + new qty) : self::setQuantity("id", 9) : qty = old + 9
replace quantity : self::setQuantity("id", 9, true) : qty = 9
*/
static public function setQuantity($id, $quantity, $replace = false){
if(array_key_exists($id, $_SESSION[xCART])){
if($replace){
$_SESSION[xCART][$id][xQTY] = max($quantity, MIN_CART_QTY);
} else {
$_SESSION[xCART][$id][xQTY] = min($quantity+$_SESSION[xCART][$id][xQTY], MAX_CART_QTY);
}
}
if($_SESSION[xCART][$id][xQTY] == 0){
self::deleteProduct($id);
}
}
/* get total of a product price in cart (qty * price)
*/
static public function getProductTotalPrice($id){
if(array_key_exists($id, $_SESSION[xCART])){
return $_SESSION[xCART][$id][xQTY] * $_SESSION[xCART][$id][xPRICE];
}
return 0;
}
/* get total of ALL products quantities in cart
*/
static public function getCartTotalQuantity($id){
$t = 0;
foreach($_SESSION[xCART] as $k => $product){
$t += $product[xQTY];
}
return $t;
}
/* get total of ALL products prices in cart
*/
static public function getCartTotalPrice(){
$t = 0;
foreach($_SESSION[xCART] as $k => $product){
$t += self::getProductTotalPrice($product[xID]);
}
return $t;
}
/* render cart summary (html)
*/
static public function showCartSummary(){
/*
CSS class :
#cart_summary .empty_cart
#cart_summary .block
#cart_summary .product_row
#cart_summary .product_row dt
#cart_summary .product_row dt span
#cart_summary .product_row dt .p_ref
#cart_summary .product_row dt .p_desc
#cart_summary .product_row dt .p_price
#cart_summary .product_row dt span a
#cart_summary .product_row dt span a:hover
#cart_summary .product_row dd
#cart_summary .product_row dd span
#cart_summary .product_row dd .p_qty
#cart_summary .product_row dd .p_total
#cart_summary .block span
#cart_summary .block
#cart_summary .block .text
#cart_summary .block .c_total
#cart_summary #cart_summary_btn
*/
$tpl = '<div id="cart_summary">';
if(empty($_SESSION[xCART])){
$tpl .= '<p class="empty_cart">Votre panier est vide.</p>';
} else
{
foreach($_SESSION[xCART] as $k => $product){
$tpl .= '<dl class="product_row">'.
'<dt>'.
'<span class="p_ref">'.$product[xREF].'</span>'.
'<span class="p_desc"><a href="link to product" title="'.$product[xNAME].'" target="_blank">'.$product[xNAME].'</a></span>'.
'<span class="p_price">'.$product[xPRICE].' €</span>'.
'</dt>'.
'<dd>'.
'<span class="p_qty">x '.$product[xQTY].'</span>'.
'<span class="p_total"> '.(self::getProductTotalPrice($product[xID])).' €</span>'.
'</dd>'.
'</dl>';
}
$tpl .= '<p class="block">'.
'<span class="text">Total</span>'.
'<span class="c_total">'.(self::getCartTotalPrice()).'</span>'.
'</p>'.
'<p class="block">'.
'<input id="cart_summary_btn" type="submit" value="Commander">'.
'</p>';
}
$tpl .= '</div>';
return $tpl;
}
/* render cart details (html)
*/
static public function showCartDetails(){
/* la flemme */
}
}
?>
Exemple d'utilisation statique dans un HTML (utilisable également en Ajax bien entendus :
<!DOCTYPE html>
<html>
<head>
<style>
*,html{
font-family:verdana;
font-size:10pt;
padding:0;
margin:0;
}
#cart_summary,
#cart_summary .block,
#cart_summary .product_row
{
display:block;
margin:2px 4px;
max-width:400px;
}
#cart_summary .product_row:nth-child(even){
background:#efefef;
}
#cart_summary .product_row:nth-child(odd){
background:#d0d0d0;
}
#cart_summary .product_row dt,
#cart_summary .product_row dd
{
display:inline-block;
}
#cart_summary .product_row dt span,
#cart_summary .product_row dd span
{
display:inline-block;
padding:0 4px;
min-width:40px;
}
#cart_summary .product_row dt .p_ref
{
text-align:left;
}
#cart_summary .product_row dt .p_desc
{
text-align:left;
}
#cart_summary .product_row dt .p_price
{
text-align:right;
}
#cart_summary .product_row dd .p_qty
{
text-align:left;
}
#cart_summary .block span
{
min-width:50px;
}
#cart_summary .block
{
text-align:right;
}
#cart_summary .block .text
{
text-align:right;
font-weight:bold;
}
#cart_summary .product_row dd .p_total,
#cart_summary .block .c_total
{
text-align:right;
font-weight:bold;
}
</style>
</head>
<body>
<?php
Cart::addProduct('0', 'AB-001', 4, 1.59, 'Test 0');
Cart::addProduct('0', 'AB-001', 1, 1.59, 'Test 0');
Cart::addProduct('1', 'AB-002', 6, 3.20, 'Test 1');
Cart::addProduct('2', 'AB-003', 2, 7.80, 'Test 2');
Cart::addProduct('3', 'AB-004', 1, 15.00, 'Test 3');
echo Cart::showCartSummary();
?>
</body>
</html>
Voila ... y'avait un problème ?
________________________________________________________
[ besoin de câbles audio, vidèo, informatique pas cher ?]
12 sept. 2013 à 18:32
12 sept. 2013 à 22:39