PHP Classes

PHP E-commerce Cart Class: Manipulate items in a shopping cart

Recommend this page to a friend!
  Info   View files Example   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 652 This week: 1All time: 4,923 This week: 560Up
Version License PHP version Categories
ecommerce-class 0.8GNU General Publi...5.4PHP 5, E-Commerce
Description 

Author

This class can manipulate items in a shopping cart.

It extends the ArrayObject class to manipulate a collection of items in a shopping cart performing operations like adding, getting, updating and removing items, search items, and compute the cart total price.

A separate product class can be used to define details the items to be added to the cart like price, quantity, description and taxes.

Picture of Nicholas Oliveira
  Performance   Level  
Name: Nicholas Oliveira <contact>
Classes: 4 packages by
Country: Brazil Brazil
Age: 37
All time rank: 70252 in Brazil Brazil
Week rank: 109 Up9 in Brazil Brazil Up

Example

<?php

include "../Product.class.php";
include
"../Cart.class.php";
include
"../Utils.class.php";


class
Produto extends Product {

    public function
NetPrice(){
        return
$this->price - $this->vat;
    }

}




session_start();





if(
$_SERVER['REQUEST_METHOD'] == "GET"){

    if( isset(
$_GET['remover'])){
       
//print "AHU";
       
$_SESSION['cart']->remove($_GET['remover']);
       
header("location: test.php");
    }

    if( isset(
$_GET['limpar_cart'])){

       
$_SESSION['cart']->emptyCart();
        unset(
$_SESSION['cart']);
       
header("location: test.php");
    }
   

}






//comment this code after SESSION IS WROTE






//var_dump($_SESSION['cart']);

//foreach ($cart as $key => $value) {
// var_dump($key, $value);
//}

//var_dump($cart->search_by_index(3));



//var_dump($_SESSION['cart']);

if(!isset($_SESSION['cart']) || empty($_SESSION['cart']) ){
    echo
'Write The Session in <a href="write_session.php">Write Session </a>Before Run';
    die();
}
?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>


<form method="POST">
    <p>This Lists is been indexed by the array Index. but you can change to get only ID from your best flavoured DB</p>
    <a href="write_session.php?force=true">Force Session Write</a>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Nome</th>
                <th>Descrição</th>
                <th>Preço</th>
                <th>Quantidade</th>
                <th>Ações</th>
            </tr>
        </thead>

        <tbody>
            <?php
            $it
= $_SESSION['cart']->getIterator();
           
            if(!
$it->valid()){


                print
"No Products";
                die();

            }else {

                while(
$it->valid()){ ?>
<tr>
                        <td><?php echo $it->current()->id; ?></td>
                        <td><?php echo $it->current()->name; ?></td>
                        <td><?php echo $it->current()->description; ?></td>
                        <td><?php echo Utils::Moneyformat($it->current()->price); ?></td>
                        <td><?php echo $it->current()->quantity; ?></td>
                        <td><a href="<?php print "?remover=".$it->key(); ?>">Remove Product</a> </td>
                    </tr>
            <?php
                    $it
->next();

        }

        }
         
?>

        <tr>
            <td>Total: <?php print sprintf("%01.2f",$_SESSION['cart']->getTotal()); ?></td>
        </tr>
        </tbody>
    </table>
    <a href="?limpar_cart=true">Empty Cart</a>
   
</form>
</body>
</html>

<?php
   
    $d
= $_SESSION['cart']->search_by_index(1);

   
$id = $_SESSION['cart']->getId($d);
    print
var_dump($id)." ^ Show Index will occur error when you delete this ID";



?>



Details

ec-cart ======= Simple E-commerce Cart Class You Can create a cart for your e-commerce easily suing this class its intended to use with sessions can support DB if you add funcionalities. before append the class to the cart you should write the product ID from database then you have a class with db working cause makes the intercation with DB not directly

  Files folder image Files  
File Role Description
Files folder imagetest (2 files)
Plain text file Cart.class.php Class Main Cart Class
Plain text file Product.class.php Class Asbtract Class for your product
Accessible without login Plain text file README.md Data Auxiliary data
Accessible without login Plain text file test.bat Data Auxiliary data
Plain text file Utils.class.php Class Just assets of functions not important neither required to run

  Files folder image Files  /  test  
File Role Description
  Accessible without login Plain text file test.php Example Example script
  Accessible without login Plain text file write_session.php Example Example script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:652
This week:1
All time:4,923
This week:560Up