Giter Site home page Giter Site logo

php-shopping-cart's Introduction

How to Use the Cart Class

This class was designed to create a shopping cart using PHP and PDO. The class can store the cart data using session or cookies. It can also include product details by pulling product information from the database.

First, include the Cart class in your PHP file:

  1. Clone the repository or download the zip file.
  2. Create a new database and import the database.sql file located in the db folder.
  3. Configure your database credentials in config/db.php.
  4. Run the project in your local server.
 include_once 'Cart.php'; 

Next, create a new instance of the Cart class, passing the desired parameters:

    /*
     new Cart(useSession, useCookie, taxPercent, dbconnection) 
    */

    $cart = new Cart(true, false, 18);  

If the database is to be used

    $database = new Database(); 
    $conn = $database->getConnection(); 
    $cart = new Cart(true, false, 18, $conn);

Adding an Item to the Cart

You can add an item to the cart using the addItem method:

 $cart->addItem($productId, $quantity, $price);
 $cart->addItem($productId, $quantity, $price, 'Product');

Updating the Quantity of an Item

You can update the quantity of an item in the cart using the updateItemQuantity method:

$cart->updateItemQuantity($productId, $quantity);

Removing an Item from the Cart

You can remove an item from the cart using the removeItem method:

$cart->removeItem($productId);

Clearing the Cart

You can clear the entire cart using the clearCart method:

$cart->clearCart();

Clearing the Cart

You can clear the entire cart using the clearCart method:

$cart->clearCart();

Retrieving the Cart Data

You can retrieve the cart data using the following methods:

$cart->getItems();
$cart->getItemCount();
$cart->getTotalQuantity();
$cart->getSubtotal();
$cart->getTaxAmount();
$cart->getTotal();

Clearing Session and Cookie

You can clear the session and cookie data using the following methods:

$cart->clearSession();
$cart->clearCookie();

Retrieving Product Details

You can retrieve the product details from the database using the getProductDetails method:

$cart->getProductDetails($productId);

Retrieving Cart Details

The getCartDetails() function returns cart details. The returned details include:

  • subtotal
  • tax amount
  • item count
  • total quantity
  • total (including tax)
  • item data
$cart->getCartDetails();

In this way, using the cart class, you can add products, update their quantity, remove them, or completely clean the cart. You can store the cart information in the $cart Data array and use it to display it on the display page.

php-shopping-cart's People

Contributors

abdullahzubeyiryildiz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.