kezoponk / robustcart.php

Create a complete shoppingcart system with a couple lines of code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Robustcart.php

Create a complete shoppingcart system with a few lines of code.

Usage

Argument Description

First

Dictionary array with structure:
"form element name value" => "desired variable name/key used later when retreiving value"
See examples bellow
[
username_key What your $_SESSION['thisvalue'] is for your username system, if you have one.
save_dir Folder to save the users shoppingcart
encrypt Encrypt shoppingcart file, if false: users shoppingcart file will be named as the users username
]

$_SESSION['cart'] = new Cart($nameToVariable, [Options for saving cart]);
Simply leave options empty if you do not want cart saved in a file!


Examples

<?php include('robustcart.php') ?>
<html>
...
<!-- Optional: Create an iframe and add target="votar" in the form
     to prevent page from refreshing each time the user adds an item to their cart -->
<iframe name="votar" style="display:none;"></iframe>
...
<form method="POST" target="votar">
  <p value="1034" name="articlenumber"> 1034 </p>
  <h1 value="Nuke" name="label"> Nuke </h1>
  <img value="img/uranB12_nuke.jpg" src="img/uranB12_nuke.jpg" name="image">
  <p value="Can be used as a toy or deadly weapon" type="text" name="description"> Can be used as a toy or deadly weapon </p>
  <button type="submit" name="add_to_cart"> Add To Cart </button>
</form>
$values = array(
  "articlenumber" => "articlenmb", 
  "label" => "label",
  "image" => "img",
  "description" => "desc"
);
Example
$_SESSION['cart'] = new Cart($values, ["username_key" => "username", "save_dir" => "users", "encrypt" => TRUE]);
  • $_SESSION['username'] is the username variable
  • Shoppingcart is stored in session and saved in "users" folder as their encrypted username.json

Example
$_SESSION['cart'] = new Cart($values, []);
  • Shoppingcart is not saved in file
  • The customers cart is stored only in session which is the only option if you don't have accounts.
    This is not the worse option, but the customers shopping cart is cleared when session cookie run out

Example
$_SESSION['cart'] = new Cart($values, ["username_key" => "user", "save_dir" => "shoppingcarts", "encrypt" => FALSE);
  • $_SESSION['user'] is the username variable
  • Shoppingcart is saved in "shoppingcarts" folder as their username.json



Retreiving and removing items

Can be done with a POST request containing a element with name=rfc (Remove-from-cart) and value=cart_index of item to remove

foreach($_SESSION["shopping_cart"] as $keys => $values)
{
  // Echo value of the description element in example form
  echo $values['desc'];

  // Items can be removed only with post
  echo '<form method="POST"> <button type="submit" name="rfc" value="'.$values['cart_index'].'"> Remove </button> </form>';
}

Outputs "Can be used as a toy or deadly weapon" with a remove button

About

Create a complete shoppingcart system with a couple lines of code

License:MIT License


Languages

Language:PHP 100.0%