@@ -16,6 +16,9 @@ class Cart
1616{
1717 const DEFAULT_INSTANCE = 'default ' ;
1818
19+ const COST_SHIPPING = 'shipping ' ;
20+ const COST_TRANSACTION = 'transaction ' ;
21+
1922 /**
2023 * Instance of the session manager.
2124 *
@@ -37,6 +40,8 @@ class Cart
3740 */
3841 private $ instance ;
3942
43+ private $ extraCosts ;
44+
4045 /**
4146 * Cart constructor.
4247 *
@@ -47,6 +52,7 @@ public function __construct(SessionManager $session, Dispatcher $events)
4752 {
4853 $ this ->session = $ session ;
4954 $ this ->events = $ events ;
55+ $ this ->extraCosts = array ();
5056
5157 $ this ->instance (self ::DEFAULT_INSTANCE );
5258 }
@@ -111,6 +117,30 @@ public function add($id, $name = null, $qty = null, $price = null, array $option
111117 return $ cartItem ;
112118 }
113119
120+ /**
121+ * Sets/adds an additional cost on the cart.
122+ *
123+ * @param $type
124+ * @param $price
125+ * @todo add in session
126+ */
127+ public function setCost ($ name , $ price )
128+ {
129+ $ this ->extraCosts [$ name ] = $ price ;
130+ }
131+
132+ /**
133+ * @param $name
134+ * @param null $decimals
135+ * @param null $decimalPoint
136+ * @param null $thousandSeperator
137+ * @return string
138+ */
139+ public function getCost ($ name , $ decimals = null , $ decimalPoint = null , $ thousandSeperator = null )
140+ {
141+ return $ this ->numberFormat ($ this ->extraCosts [$ name ] ?? 0 , $ decimals , $ decimalPoint , $ thousandSeperator );
142+ }
143+
114144 /**
115145 * Update the cart item with the given rowId.
116146 *
@@ -242,6 +272,10 @@ public function total($decimals = null, $decimalPoint = null, $thousandSeperator
242272 return $ total + ($ cartItem ->qty * $ cartItem ->priceTax );
243273 }, 0 );
244274
275+ foreach ($ this ->extraCosts as $ cost ) {
276+ $ total += $ cost ;
277+ }
278+
245279 return $ this ->numberFormat ($ total , $ decimals , $ decimalPoint , $ thousandSeperator );
246280 }
247281
0 commit comments