lib/composer/OrderBundle/src/VisualMedia/OrderBundle/Model/CartModel.php line 17

Open in your IDE?
  1. <?php
  2. /**
  3. * Cart Model
  4. *
  5. * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
  6. */
  7. namespace VisualMedia\OrderBundle\Model;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use VisualMedia\LisaBundle\Component\DataLayer;
  10. use VisualMedia\OrderBundle\Entity\BaseOrderItem;
  11. /**
  12. * Cart Model
  13. */
  14. class CartModel
  15. {
  16. /**
  17. * Items
  18. * @var ArrayCollection
  19. */
  20. protected $items;
  21. /**
  22. * Amount
  23. * @var float
  24. */
  25. protected $amount;
  26. /**
  27. * Subtotal
  28. * @var float
  29. */
  30. protected $subtotal;
  31. /**
  32. * Shipping
  33. * @var float
  34. */
  35. protected $shipping;
  36. /**
  37. * Payment
  38. * @var float
  39. */
  40. protected $payment;
  41. /**
  42. * Total
  43. * @var float
  44. */
  45. protected $total;
  46. /**
  47. * Constructor
  48. */
  49. public function __construct()
  50. {
  51. $this->items = new ArrayCollection();
  52. }
  53. /**
  54. * Get Items
  55. *
  56. * @return ArrayCollection
  57. */
  58. public function getItems(): ArrayCollection
  59. {
  60. return $this->items;
  61. }
  62. /**
  63. * Set Items
  64. *
  65. * @param ArrayCollection $items
  66. *
  67. * @return void
  68. */
  69. public function setItems(ArrayCollection $items): void
  70. {
  71. $this->items = $items;
  72. }
  73. /**
  74. * Add
  75. *
  76. * @param BaseOrderItem $item
  77. *
  78. * @return void
  79. */
  80. public function add(BaseOrderItem $item): void
  81. {
  82. // Fire GA4 "add_to_cart".
  83. DataLayer::pushEcommerce('add_to_cart', $item);
  84. if (null === $existing = $this->contains($item)) {
  85. $this->items->add($item);
  86. }
  87. else {
  88. $existing->setAmount($existing->getAmount() + 1);
  89. }
  90. }
  91. /**
  92. * Contains
  93. *
  94. * @param BaseOrderItem $new
  95. *
  96. * @return BaseOrderItem
  97. */
  98. public function contains(BaseOrderItem $new): ?BaseOrderItem
  99. {
  100. $newHash = $new->getItemHash();
  101. foreach ($this->getItems() as $existing) {
  102. if ($newHash === $existingHash = $existing->getItemHash()) {
  103. return $existing;
  104. }
  105. }
  106. return null;
  107. }
  108. /**
  109. * Update Item Amount
  110. *
  111. * @param string $hash
  112. * @param int $amount
  113. *
  114. * @return void
  115. */
  116. public function updateItemAmount(string $hash, int $amount): void
  117. {
  118. foreach ($this->getItems() as $item) {
  119. if ($item->getItemHash() === $hash) {
  120. $item->setAmount($amount);
  121. }
  122. }
  123. }
  124. /**
  125. * Remove Item
  126. *
  127. * @param string $hash
  128. *
  129. * @return void
  130. */
  131. public function removeItem(string $hash): void
  132. {
  133. foreach ($this->getItems() as $item) {
  134. if ($item->getItemHash() === $hash) {
  135. // Fire GA4 "remove_from_cart".
  136. DataLayer::pushEcommerce('remove_from_cart', $item);
  137. $this->getItems()->removeElement($item);
  138. }
  139. }
  140. }
  141. /**
  142. * Getc Item By Hash
  143. *
  144. * @param string $hash
  145. *
  146. * @return BaseOrderItem|null
  147. */
  148. public function getItemByHash(string $hash): ?BaseOrderItem
  149. {
  150. foreach ($this->getItems() as $item) {
  151. if ($item->getItemHash() === $hash) {
  152. return $item;
  153. }
  154. }
  155. return null;
  156. }
  157. /**
  158. * Get Amount
  159. *
  160. * @return int
  161. */
  162. public function getAmount(): int
  163. {
  164. $amount = 0;
  165. foreach ($this->getItems() as $item) {
  166. $amount += $item->getAmount();
  167. }
  168. return $amount;
  169. }
  170. /**
  171. * @return float
  172. */
  173. public function getSubtotal(): float
  174. {
  175. $this->subtotal = 0;
  176. foreach ($this->getItems() as $item) {
  177. if ($item->isFree()) {
  178. continue;
  179. }
  180. $this->subtotal += $item->getTotal();
  181. }
  182. return $this->subtotal;
  183. }
  184. }