项目作者: ksdev-pl

项目描述 :
OOP PHP Shopping Cart
高级语言: PHP
项目地址: git://github.com/ksdev-pl/shopping-cart.git
创建时间: 2015-08-01T19:11:51Z
项目社区:https://github.com/ksdev-pl/shopping-cart

开源协议:Other

下载


Shopping Cart

Latest Version on Packagist
Software License
Build Status
Coverage Status
Quality Score

Original source: http://www.peachpit.com/articles/article.aspx?p=1962481 by Larry Ullman. See the article for
description and compare source code for changes.

Install

Via Composer

  1. $ composer require ksdev/shopping-cart

Usage

  1. use Ksdev\ShoppingCart\Cart;
  2. use Ksdev\ShoppingCart\Currency;
  3. use Ksdev\ShoppingCart\Item;
  4. $cart = new Cart(new Currency('PLN'));
  5. $tax = '23.00'; // Tax is optional
  6. $item1 = new Item('SKU1', 'Item 1', '100.00', $tax);
  7. $item2 = new Item('SKU2', 'Item 2', '200.00', $tax);
  8. $item3 = new Item('SKU3', 'Item 3', '300.00', $tax);
  9. $cart->addItem($item1);
  10. $cart->addItem($item2);
  11. $cart->addItem($item3);
  12. if (!$cart->isEmpty()) {
  13. foreach ($cart as $arr) {
  14. $item = $arr['item'];
  15. var_dump($item->getSku()); // E.g. string(4) "SKU1"
  16. var_dump($item->getName()); // E.g. string(6) "Item 1"
  17. var_dump($item->getPrice()); // E.g. string(6) "100.00"
  18. var_dump($item->getTax()); // E.g. string(5) "23.00"
  19. var_dump($arr['qty']); // E.g. int(1)
  20. }
  21. }
  22. var_dump($cart->total()); // string(6) "600.00"
  23. var_dump($cart->getCurrency()->getCode()); // string(3) "PLN"
  24. $item4 = new Item('SKU1', 'Item 1', '100.00', $tax); // Same as $item1
  25. $cart->addItem($item4);
  26. var_dump($cart->total()); // string(6) "700.00"
  27. var_dump($cart->count()); // int(4); also: count($cart)
  28. var_dump($cart->countUnique()); // int(3)
  29. $cart->updateItem($item2, 3); // 3 is the new quantity
  30. var_dump($cart->count()); // int(6)
  31. var_dump($cart->countUnique()); // int(3)
  32. $cart->updateItem($item2, 0); // Removes the item from the cart
  33. var_dump($cart->count()); // int(3)
  34. var_dump($cart->countUnique()); // int(2)
  35. $cart->deleteItem($item1); // Removes the item from the cart
  36. var_dump($cart->count()); // int(1)
  37. var_dump($cart->countUnique()); // int(1)
  38. var_dump($cart->getItem('SKU3')); // Get item by Stock Keeping Unit
  39. /*
  40. array(2) {
  41. 'item' => class Ksdev\ShoppingCart\Item#270 (3) {
  42. protected $sku => string(4) "SKU3"
  43. protected $name => string(6) "Item 3"
  44. protected $price => string(6) "300.00"
  45. protected $tax => string(5) "23.00"
  46. }
  47. 'qty' => int(1)
  48. }
  49. */

Testing

  1. $ composer test

Credits

  • Larry Ullman

License

The MIT License (MIT). Please see License File for more information.