ShoppingCart.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Shop\Carts;
  3. use Gloudemans\Shoppingcart\Cart;
  4. use Gloudemans\Shoppingcart\CartItem;
  5. class ShoppingCart extends Cart
  6. {
  7. public static $defaultCurrency;
  8. protected $session;
  9. protected $event;
  10. public function __construct()
  11. {
  12. $this->session = $this->getSession();
  13. $this->event = $this->getEvents();
  14. parent::__construct($this->session, $this->event);
  15. self::$defaultCurrency = config('cart.currency');
  16. }
  17. public function getSession()
  18. {
  19. return app()->make('session');
  20. }
  21. public function getEvents()
  22. {
  23. return app()->make('events');
  24. }
  25. /**
  26. * Get the total price of the items in the cart.
  27. *
  28. * @param int $decimals
  29. * @param string $decimalPoint
  30. * @param string $thousandSeparator
  31. * @param float $shipping
  32. * @return string
  33. */
  34. public function total($decimals = null, $decimalPoint = null, $thousandSeparator = null, $shipping = 0.00)
  35. {
  36. $content = $this->getContent();
  37. $total = $content->reduce(function ($total, CartItem $cartItem) {
  38. return $total + ($cartItem->qty * $cartItem->priceTax);
  39. }, 0);
  40. $grandTotal = $total + $shipping;
  41. return number_format($grandTotal, $decimals, $decimalPoint, $thousandSeparator);
  42. }
  43. }