Miscellaneous Code
  Home arrow Miscellaneous Code arrow Shopping Cart Class
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS CODE

Shopping Cart Class
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2006-08-03

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    This is a nifty little shopping cart I made. Fully customizable.

    By : voldomazta

    <?php

    /**
    * + Shopping Cart Class
    * + (c) 2006 Ramon Alivio Jr. (http://www.ramon.ph)
    * + on 2006-08-03
    **/

    class Cart extends Site {

    /**
    * The serialized array of items
    * @var string
    **/
    var $items;

    /**
    * The name of the cart
    * @var string
    **/
    var $CART_NAME;

    /**
    * void Cart
    * + Instantiates the class, sets the cart name
    * + to your cart name and sets the items array for use
    * -----
    * @param string : The name of the cart
    **/
    function Cart($cn)
    {
    if (empty($cn)) return false;
    $this->CART_NAME = preg_replace("/([^a-zA-Z|\s]+)/","",$cn);
    if (!empty($_COOKIE[$this->CART_NAME])) {
    $this->items = base64_decode($_COOKIE[$this->CART_NAME]);
    $this->items = unserialize($this->items);
    } else {
    $this->items = array();
    }
    }

    /**
    * void add_item
    * + This function adds the items to the shopping cart array
    * + and serializes them
    * -----
    * @param int : ID of the item
    * @param string : Name of the item
    * @param int : Quantity of the item
    * @param double : Price of the item
    * @param string : Additional Notes
    **/
    function add_item($id, $n, $q, $p, $an='')
    {
    if ($this->is_zero($id, $q, $p) || empty($n)) return;
    if ($this->in_cart($id)) $this->items[$id]['quantity'] += $q;
    else $this->items[$oid] = array('id' => $id, 'name' => $n, 'quantity' => $q, 'price' => $p, 'notes' => $an);
    $this->update_cart();
    }

    /**
    * void remove_item
    * + This function removes an item from the cart with the
    * + specified "id" and "quantity" and updates the cart afterwards
    * -----
    * @param int : ID of the item to be removed
    * @param int : Quantity of the item to be removed
    **/
    function remove_item($id, $q)
    {
    $cq = $this->items[$id]['quantity'];
    if (!$this->in_cart($id)) return;
    if ($q > $cq) $this->items[$id]['quantity'] = 0;
    else $this->items[$id]['quantity'] -= $q;
    if ($cq == 0) unset($this->items[$id]);
    $this->update_cart();
    }

    /**
    * void update_quantity
    * + This function updates the quantity of a given item
    * + with the specified "id" and quantity", if the given
    * + quantity is 0 or less, the item is removed from the cart
    * -----
    * @param int : ID of the item to be updated
    * @param int : New quantity of the item
    **/
    function update_quantity($id, $nq)
    {
    if (!$this->in_cart($id)) return;
    $this->items[$id]['quantity'] = $nq;
    $this->update_cart();
    }

    /**
    * boolean in_cart
    * + This function returns whether the given item exists
    * + on the shopping cart
    * -----
    * @param int : ID of the item to be checked
    **/
    function in_cart($id)
    {
    return is_array($this->items[$id]) ? true : false;
    }

    /**
    * int item_count
    * + This item returns the total number of items in the cart
    * -----
    * @param
    **/
    function item_count()
    {
    $count = 0;
    foreach ($this->items as $v) $count += $v['quantity'];
    return $count;
    }

    /**
    * void update_cart
    * + This function updates the shopping cart, if a certain
    * + quantity for an item is 0, it removes it from the cart
    * -----
    * @param
    **/
    function update_cart()
    {
    foreach ($this->items as $k=>$v) if ($v['quantity'] == 0) unset($this->items[$k]);
    $temp = base64_encode(serialize($this->items));
    setcookie($this->CART_NAME,'',time()-1);
    setcookie($this->CART_NAME,$temp,time()+30*24*60*60);
    }

    /**
    * double total_price
    * + This function returns the total price of all the items
    * + in the cart
    * -----
    * @param
    **/
    function total_price()
    {
    $total = 0;
    foreach ($this->items as $v) $total += $v['quantity']*$v['price'];
    return $total;
    }

    /**
    * void empty_cart
    * + This function empties the shoppig cart of all its items
    * -----
    * @param
    **/
    function empty_cart()
    {
    foreach ($this->items as $k=>$v) unset($this->items[$k]);
    $this->update_cart();
    }

    /**
    * boolean is_zero
    * + This function checks whether there is a 0 value
    * + from all the arguments given to it
    * -----
    * @param mixed : Set of numbers => is_zero(3,0,2)
    **/
    function is_zero()
    {
    foreach (func_get_args() as $arg) if ((int)$arg != 0) return false;
    return true;
    }
    }

    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By Codewalkers

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Calling all CC Power Users – and those that would like to be!

    Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development
    FREE! Go There Now!


    NEW! Download a free trial of WebSphere Business Modeler Advanced V6.1.1

    Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement.
    FREE! Go There Now!


    NEW! Download IBM WebSphere Portal V6.1 beta code

    Download the IBM WebSphere Portal V6.1 beta code and learn more about the rich features and enhancements in IBM WebSphere Portal V6.1. WebSphere Portal provides a composite application or business mashup framework and the advanced tooling needed to build flexible, SOA-based solutions, and scalability to meet the needs of any size organization.
    FREE! Go There Now!


    NEW! Improve your build process with IBM Rational Build Forge, Part 1: Create a continuous build and integration environment

    Learn how to implement a build management system that uses and extends your existing automation technologies. This tutorial shows, step-by-step, how to install and configure IBM Rational Build Forge to manage builds for Jakarta Tomcat from source code.
    FREE! Go There Now!


    NEW! Rational Talks to You: Manage RUP-based CMMI initiatives

    Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered!
    FREE! Go There Now!


    NEW! Rational Testing eKits

    Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing.
    FREE! Go There Now!


    NEW! Run your first CICS application on a PC using TXSeries for Windows

    Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1.
    FREE! Go There Now!


    NEW! Test terminal-based applications with Rational Functional Tester

    Regression testing -- in which code is thoroughly tested to ensure that changes have not produced unexpected results -- is an important part of any development process. But many testing environments neglect the terminal-based applications that still form the backbone of many industries. In this tutorial, you'll learn how the Rational Functional Tester Extension for Terminal-Based Applications works with other Rational Functional Tester to help test terminal-based applications quickly and easily.
    FREE! Go There Now!


    NEW! Trial download: IBM Informix Dynamic Server Express Edition V11.0

    Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data.
    FREE! Go There Now!


    NEW! Webcast: Application security testing and Web compliance

    Join the IBM Watchfire team for an informative discussion on techniques and best practices to proactively manage Web application security and how to effectively build application security testing into the software development lifecycle (SDLC). In this Software Delivery Platform webcast you will learn: How to better understand potential web application security vulnerabilities, best practices and how to effectively integrate application security testing into the software development lifecycle, the importance of detecting and removing software vulnerabilities during application development.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    MISCELLANEOUS CODE ARTICLES

    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...
    - Model Data and Validation Rules for a Generi...
    - Building a Generic Model for the CodeIgniter...
    - upload image to database sql
    - Random Password Generator
    - BCroot, get the root of a number with BC fun...
    - Find pi in a high precision
    - [PHP5] FORMCHECKER : data validation
    - SPL and ITERATOR : examples
    - Xml with Rss Feeds





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT