Advertisement
michaelyuen

Untitled

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