Advertisement
michaelyuen

Untitled

Mar 2nd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.53 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. // add cart to session
  5. $product_id = 15;
  6. $qty = 1;
  7. $_SESSION['cart'][$product_id] = $qty;
  8.  
  9. // update cart
  10.  
  11. $product_id = 15;
  12. $qty = 2;
  13. $_SESSION['cart'][$product_id] = $qty;
  14.  
  15. // remove item from cart
  16. $product_id = 15;
  17. unset($_SESSION['cart'][$product_id]);
  18.  
  19. // check cart
  20.  
  21. if (!EMPTY($_SESSION['cart'])) {
  22.     $cart = $_SESSION['cart'];
  23.     foreach ($cart as $k => $v) {
  24.         echo 'Product ID: ' .$k;
  25.         echo '<br>';
  26.         echo 'Quantity: ' . $v;
  27.         echo '<br>';
  28.     }
  29. } else {
  30.     echo 'Cart is empty';
  31. }
  32. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement