diff --git a/src/Controllers/CartController.php b/src/Controllers/CartController.php new file mode 100644 index 0000000..f2058f6 --- /dev/null +++ b/src/Controllers/CartController.php @@ -0,0 +1,90 @@ +view('cart.index', $this->cartViewData()); + } + + public function add(): Response + { + $productId = $this->ctx->request->integer('productId'); + $quantity = max(1, $this->ctx->request->integer('quantity', 1)); + $variantId = $this->ctx->request->integer('variantId', 0) ?: null; + $this->ctx->cart->add($productId, $quantity, $variantId); + $this->ctx->session->flash('success', 'Item added to your cart.'); + return $this->redirect('/cart'); + } + + public function update(): Response + { + $productId = $this->ctx->request->integer('productId'); + $quantity = $this->ctx->request->integer('quantity', 1); + $variantId = $this->ctx->request->integer('variantId', 0) ?: null; + $this->ctx->cart->update($productId, $quantity, $variantId); + return $this->redirect('/cart'); + } + + public function remove(): Response + { + $productId = $this->ctx->request->integer('productId'); + $variantId = $this->ctx->request->integer('variantId', 0) ?: null; + $this->ctx->cart->remove($productId, $variantId); + $this->ctx->session->flash('success', 'Item removed from your cart.'); + return $this->redirect('/cart'); + } + + public function applyCoupon(): Response + { + $code = trim((string) $this->ctx->request->input('code', '')); + $result = $this->ctx->couponService->evaluate($code, $this->ctx->cart->totalCents()); + + if ($result->isValid()) { + $this->ctx->session->put('couponCode', $result->coupon['code']); + $this->ctx->session->flash('success', $this->ctx->translator->get('cart.couponApplied', [ + 'code' => $result->coupon['code'], + ])); + } else { + $this->ctx->session->forget('couponCode'); + $this->ctx->session->flash('error', $result->message()); + } + return $this->redirect('/cart'); + } + + public function removeCoupon(): Response + { + $this->ctx->session->forget('couponCode'); + $this->ctx->session->flash('success', 'Coupon removed.'); + return $this->redirect('/cart'); + } + + private function cartViewData(): array + { + $subtotalCents = $this->ctx->cart->totalCents(); + $discountCents = 0; + $couponCode = (string) $this->ctx->session->get('couponCode', ''); + + if ($couponCode !== '') { + $result = $this->ctx->couponService->evaluate($couponCode, $subtotalCents); + if ($result->isValid()) { + $discountCents = $result->discountCents; + } else { + $this->ctx->session->forget('couponCode'); + $couponCode = ''; + } + } + + return [ + 'lines' => $this->ctx->cart->lines(), + 'subtotalCents' => $subtotalCents, + 'discountCents' => $discountCents, + 'totalCents' => $subtotalCents - $discountCents, + 'couponCode' => $couponCode, + ]; + } +} diff --git a/src/Controllers/WishlistController.php b/src/Controllers/WishlistController.php new file mode 100644 index 0000000..9ac6701 --- /dev/null +++ b/src/Controllers/WishlistController.php @@ -0,0 +1,28 @@ +view('account.wishlist', [ + 'products' => $this->ctx->wishlist->forUser((int) $this->ctx->auth->id()), + ]); + } + + public function toggle(): Response + { + $productId = $this->ctx->request->integer('productId'); + $product = $this->ctx->products->findActive($productId); + if ($product === null) { + return $this->back('/products'); + } + + $saved = $this->ctx->wishlist->toggle((int) $this->ctx->auth->id(), $productId); + $this->ctx->session->flash('success', $saved ? 'Saved to your wishlist.' : 'Removed from your wishlist.'); + return $this->back('/products/' . $product['slug']); + } +}