Location: PHPKode > scripts > sCart > scart/sCart.php
<?php

/*
#  Title: Shopping Cart v0.9b
#  Filename: sCart.php
#  Date: 2002-02-09
#  Developer: Dirk Herling -- hide@address.com
#
#  Functions:
#      void add_item(mixed inumber, string name, int quant, double price, int taxqual[, array descriptions]);
#      void update_item(mixed inumber, int quant);
#      void remove_item(mixed inumber);
#      void clear();
#
#      bool show();
#      array show_next_item();
#      array sum();
#
#  Example:
#  <?php
#      include("sCart.php");
#      $cart = new sCart();
#
#      // register cart to session (optional)
#      // session_start();
#      // session_register("cart");
#
#      // add a new product into the cart
#      $cart->add_item(123, "Productname", 1, 6.95, 16, array("other descriptions"));
#
#      // show all products from the cart
#      if($cart->show())
#      {
#          while($values = $cart->show_next_item())
#          {
#              print_r($values);
#          }
#      }
#
#      // show sum of all products from the cart
#      if($values = $cart->sum())
#      {
#          print_r($values);
#      }
#
#      // update an product of the cart
#      $cart->update_item(123, 10);
#
#      // remove an product of the cart
#      $cart->remove_item(123);
#
#      // clear complete cart
#      $cart->clear();
#  ?>
*/

if(!isset($_sCart_Included) || !$_sCart_Included)
{
    $_sCart_Included = 1;

    class sCartItem
    {
        var $inumber;
        var $name;
        var $quant;
        var $price;
        var $taxqual;
        var $descr;
        var $gprice;
        var $nprice;
        var $gtotprice;
        var $ntotprice;
        var $tax;

        function sCartItem(&$inumber,&$name,&$quant,&$price,&$taxqual,&$descr)
        {
            $this->_init();

            $this->inumber = $inumber;
            $this->name    = $name;
            $this->quant   = sprintf("%d", $quant);
            $this->price   = sprintf("%0.4f", $price);
            $this->taxqual = sprintf("%d", $taxqual);
            if(is_array($descr))
            {
                foreach($descr as $key => $val)
                {
                    $this->descr[$key] = $val;
                }
            }
            $this->_calc();
        }

        function update(&$quant)
        {
            $this->quant = $quant;
            $this->_calc();
        }

        function get_ntotprice()
        {
            return $this->ntotprice;
        }

        function get_gtotprice()
        {
            return $this->gtotprice;
        }

        function get_tax()
        {
            return @array("taxqual" => $this->taxqual, "tax" => $this->tax);
        }

        function get_all()
        {
            $values = array();
            $values["cart.inumber"]   = $this->inumber;
            $values["cart.name"]      = $this->name;
            $values["cart.quant"]     = sprintf("%d", $this->quant);
            $values["cart.price"]     = sprintf("%0.2f", $this->price);
            $values["cart.nprice"]    = sprintf("%0.2f", $this->nprice);
            $values["cart.gprice"]    = sprintf("%0.2f", $this->gprice);
            $values["cart.ntotprice"] = sprintf("%0.2f", $this->ntotprice);
            $values["cart.gtotprice"] = sprintf("%0.2f", $this->gtotprice);
            $values["cart.taxqual"]   = $this->taxqual;
            $values["cart.tax"]       = sprintf("%0.2f", $this->tax);
            foreach($this->descr as $key => $val)
            {
                $values["cart.descr.$key"] = $val;
            }
            return $values;
        }

        function _init()
        {
            $this->inumber   = null;
            $this->name      = "";
            $this->quant     = 0;
            $this->price     = 0.00;
            $this->descr     = array();
            $this->taxqual   = 0;
            $this->gprice    = 0.00;
            $this->nprice    = 0.00;
            $this->gtotprice = 0.00;
            $this->ntotprice = 0.00;
            $this->tax       = 0.00;
        }

        function _calc()
        {
            $this->gprice    = sprintf("%0.4f", $this->price - $this->discqual);
            $this->tax       = sprintf("%0.2f", ($this->gprice * $this->taxqual / (100 + $this->taxqual)) * $this->quant);
            $this->nprice    = sprintf("%0.4f", $this->gprice - ($this->gprice * $this->taxqual / (100 + $this->taxqual)));
            $this->ntotprice = sprintf("%0.4f", $this->nprice * $this->quant);
            $this->gtotprice = sprintf("%0.4f", $this->gprice * $this->quant);
        }
    }

    class sCart
    {
        var $items;
        var $gtotprice;
        var $ntotprice;
        var $tax;

        function sCart()
        {
            $this->_init();
        }

        function add_item($inumber,$name,$quant,$price,$taxqual,$descr=0)
        {
            if(!is_object($this->items[$inumber]))
            {
                 $this->items[$inumber] = new sCartItem($inumber,$name,$quant,$price,$taxqual,$descr);
                 $this->_refresh();
            }
        }

        function update_item($inumber,$quant)
        {
            if(is_object($this->items[$inumber]))
            {
                $this->items[$inumber]->update($quant);
                $this->_refresh();
            }
        }

        function remove_item($inumber)
        {
            if(is_object($this->items[$inumber]))
            {
                unset($this->items[$inumber]);
                $this->_refresh();
            }
        }

        function clear()
        {
            unset($this->items);
            $this->_init();
        }

        function show()
        {
            reset($this->items);
            return count($this->items);
        }

        function show_next_item()
        {
            if($item = each($this->items))
            {
                return $item["value"]->get_all();
            }
        }

        function sum()
        {
            if(count($this->items))
            {
                 $values = array();
                 $values["cart.sum.ntotprice"] = sprintf("%0.2f", $this->ntotprice);
                 $values["cart.sum.gtotprice"] = sprintf("%0.2f", $this->gtotprice);
                 foreach($this->tax as $key => $val)
                 {
                     $values["cart.sum.tax.".$key] = sprintf("%0.2f", $val);
                 }
                 return $values;
            }
            return false;
        }

        function _init()
        {
            $this->items     = array();
            $this->gtotprice = 0.00;
            $this->ntotprice = 0.00;
            $this->tax       = array();
        }

        function _refresh()
        {
            $this->gtotprice = 0.00;
            $this->ntotprice = 0.00;
            $this->tax       = array();
            $count           = 0;
            reset($this->items);
            foreach($this->items as $key => $val)
            {
                $this->gtotprice += $val->get_gtotprice();
                $this->ntotprice += $val->get_ntotprice();
                $tax = $val->get_tax();
                $this->tax[$tax["taxqual"]] += $tax["tax"];
            }
            reset($this->items);
        }
    }
}

?>
Return current item: sCart