@@ -51,8 +51,8 @@ private function normalizeOptions($options) {
5151 return $ normalized ;
5252 }
5353
54- public function buildLineKey ($ product_id , $ options = []) {
55- return $ product_id . ': ' . md5 (json_encode ($ this ->normalizeOptions ($ options )));
54+ public function buildLineKey ($ id , $ options = []) {
55+ return $ id . ': ' . md5 (json_encode ($ this ->normalizeOptions ($ options )));
5656 }
5757
5858 public function addProductToCart ($ product_id , $ name , $ price , $ options = []) {
@@ -94,6 +94,45 @@ public function removeProductFromCart($lineKey) {
9494 }
9595 }
9696
97+ public function addMenuToCart ($ menu_id , $ name , $ price , $ selections = []) {
98+ if (!isset ($ _SESSION ['cart ' ]['menus ' ])) {
99+ $ _SESSION ['cart ' ]['menus ' ] = [];
100+ }
101+
102+ $ normalizedSelections = $ this ->normalizeOptions ($ selections );
103+ $ lineKey = $ this ->buildLineKey ($ menu_id , $ normalizedSelections );
104+
105+ if (isset ($ _SESSION ['cart ' ]['menus ' ][$ lineKey ])) {
106+ $ _SESSION ['cart ' ]['menus ' ][$ lineKey ]['quantity ' ]++;
107+ } else {
108+ $ _SESSION ['cart ' ]['menus ' ][$ lineKey ] = [
109+ 'line_key ' => $ lineKey ,
110+ 'menu_id ' => $ menu_id ,
111+ 'name ' => $ name ,
112+ 'quantity ' => 1 ,
113+ 'price ' => $ price ,
114+ 'selections ' => $ normalizedSelections ,
115+ ];
116+ }
117+
118+ return $ lineKey ;
119+ }
120+
121+ public function removeMenuFromCart ($ lineKey ) {
122+ if (!isset ($ _SESSION ['cart ' ]['menus ' ])) {
123+ $ _SESSION ['cart ' ]['menus ' ] = [];
124+ return ;
125+ }
126+
127+ if (isset ($ _SESSION ['cart ' ]['menus ' ][$ lineKey ])) {
128+ if ($ _SESSION ['cart ' ]['menus ' ][$ lineKey ]['quantity ' ] - 1 === 0 ) {
129+ unset($ _SESSION ['cart ' ]['menus ' ][$ lineKey ]);
130+ } else {
131+ $ _SESSION ['cart ' ]['menus ' ][$ lineKey ]['quantity ' ] --;
132+ }
133+ }
134+ }
135+
97136 public function getProductQuantityById ($ productId ) {
98137 if (!isset ($ _SESSION ['cart ' ]['products ' ])) {
99138 return 0 ;
@@ -110,6 +149,22 @@ public function getProductQuantityById($productId) {
110149 return $ quantity ;
111150 }
112151
152+ public function getMenuQuantityById ($ menuId ) {
153+ if (!isset ($ _SESSION ['cart ' ]['menus ' ])) {
154+ return 0 ;
155+ }
156+
157+ $ quantity = 0 ;
158+
159+ foreach ($ _SESSION ['cart ' ]['menus ' ] as $ menu ) {
160+ if ((int ) ($ menu ['menu_id ' ] ?? 0 ) === (int ) $ menuId ) {
161+ $ quantity += (int ) ($ menu ['quantity ' ] ?? 0 );
162+ }
163+ }
164+
165+ return $ quantity ;
166+ }
167+
113168 public function getTotalCount () {
114169 if (!isset ($ _SESSION ['cart ' ]) || !isset ($ _SESSION ['cart ' ]['products ' ]) || !isset ($ _SESSION ['cart ' ]['menus ' ])) {
115170 return 0 ;
@@ -130,28 +185,51 @@ public function getTotalCount() {
130185
131186 public function computeTotal () {
132187 $ total = 0 ;
133- if (!isset ($ _SESSION ['cart ' ]['products ' ])) {
134- return $ total ;
135- }
136188
137- foreach ($ _SESSION ['cart ' ]['products ' ] as $ product ) {
138- $ quantity = (int ) ($ product ['quantity ' ] ?? 0 );
139- $ price = (float ) ($ product ['price ' ] ?? 0 );
189+ // Add products total
190+ if (isset ($ _SESSION ['cart ' ]['products ' ])) {
191+ foreach ($ _SESSION ['cart ' ]['products ' ] as $ product ) {
192+ $ quantity = (int ) ($ product ['quantity ' ] ?? 0 );
193+ $ price = (float ) ($ product ['price ' ] ?? 0 );
140194
141- $ total += $ quantity * $ price ;
195+ $ total += $ quantity * $ price ;
142196
143- if (isset ($ product ['options ' ]) && is_array ($ product ['options ' ])) {
144- foreach ($ product ['options ' ] as $ slot ) {
145- if (!isset ($ slot ['choices ' ]) || !is_array ($ slot ['choices ' ])) {
146- continue ;
197+ if (isset ($ product ['options ' ]) && is_array ($ product ['options ' ])) {
198+ foreach ($ product ['options ' ] as $ slot ) {
199+ if (!isset ($ slot ['choices ' ]) || !is_array ($ slot ['choices ' ])) {
200+ continue ;
201+ }
202+
203+ foreach ($ slot ['choices ' ] as $ choice ) {
204+ $ total += $ quantity * (float ) ($ choice ['priceDelta ' ] ?? 0 );
205+ }
147206 }
207+ }
208+ }
209+ }
210+
211+ // Add menus total
212+ if (isset ($ _SESSION ['cart ' ]['menus ' ])) {
213+ foreach ($ _SESSION ['cart ' ]['menus ' ] as $ menu ) {
214+ $ quantity = (int ) ($ menu ['quantity ' ] ?? 0 );
215+ $ price = (float ) ($ menu ['price ' ] ?? 0 );
148216
149- foreach ($ slot ['choices ' ] as $ choice ) {
150- $ total += $ quantity * (float ) ($ choice ['priceDelta ' ] ?? 0 );
217+ $ total += $ quantity * $ price ;
218+
219+ if (isset ($ menu ['selections ' ]) && is_array ($ menu ['selections ' ])) {
220+ foreach ($ menu ['selections ' ] as $ slot ) {
221+ if (!isset ($ slot ['choices ' ]) || !is_array ($ slot ['choices ' ])) {
222+ continue ;
223+ }
224+
225+ foreach ($ slot ['choices ' ] as $ choice ) {
226+ $ total += $ quantity * (float ) ($ choice ['priceDelta ' ] ?? 0 );
227+ }
151228 }
152229 }
153230 }
154231 }
232+
155233 return $ total ;
156234 }
157235
0 commit comments