AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CartController.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Commerce\Cart;
  4. use App\Http\Controllers\Concerns\RendersStorefront;
  5. use App\Models\Product;
  6. use App\Models\Store;
  7. use App\Storefront\StorefrontText;
  8. use Illuminate\Http\RedirectResponse;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Validation\ValidationException;
  11. use Inertia\Response;
  12. /**
  13. * The shopper's cart for one store. Used for both the live store and the owner's preview.
  14. */
  15. class CartController extends Controller
  16. {
  17. use RendersStorefront;
  18. public function show(Request $request): Response
  19. {
  20. return $this->renderStorefront($request, $this->storefront($request), ['type' => 'cart']);
  21. }
  22. /**
  23. * Add a product to the cart.
  24. */
  25. public function add(Request $request): RedirectResponse
  26. {
  27. $store = $this->storefront($request)->store;
  28. $validated = $request->validate([
  29. 'product_id' => ['required', 'integer'],
  30. 'variant_id' => ['nullable', 'integer'],
  31. 'quantity' => ['required', 'integer', 'min:1', 'max:'.Cart::MAX_QUANTITY],
  32. ]);
  33. $product = $this->buyableProduct($store, $validated['product_id']);
  34. $variant = null;
  35. // A product with options must be bought as one of its variants; a simple product never is.
  36. if ($product->hasVariants()) {
  37. $variant = $product->variants()->whereKey($validated['variant_id'] ?? 0)->first();
  38. if ($variant === null) {
  39. throw ValidationException::withMessages(['variant_id' => StorefrontText::line($store->language->value, 'choose_option')]);
  40. }
  41. } elseif (($validated['variant_id'] ?? null) !== null) {
  42. abort(404);
  43. }
  44. if (($variant !== null ? $variant->stock : $product->stock) < 1) {
  45. throw ValidationException::withMessages(['quantity' => StorefrontText::line($store->language->value, 'sold_out')]);
  46. }
  47. $this->cart($request, $store)->add($product, $variant, $validated['quantity']);
  48. return back();
  49. }
  50. /**
  51. * Change how many of a product are in the cart. Zero removes it.
  52. */
  53. public function update(Request $request): RedirectResponse
  54. {
  55. $store = $this->storefront($request)->store;
  56. $validated = $request->validate([
  57. 'quantity' => ['required', 'integer', 'min:0', 'max:'.Cart::MAX_QUANTITY],
  58. ]);
  59. $result = $this->cart($request, $store)->setLine((string) $request->route('line'), $validated['quantity']);
  60. abort_if($result === null, 404);
  61. return back();
  62. }
  63. /**
  64. * Put a discount code in the cart.
  65. */
  66. public function applyDiscount(Request $request): RedirectResponse
  67. {
  68. $store = $this->storefront($request)->store;
  69. $language = $store->language->value;
  70. $validated = $request->validate(['code' => ['required', 'string', 'max:40']], [
  71. 'code.required' => StorefrontText::line($language, 'discount_invalid'),
  72. ]);
  73. $problem = $this->cart($request, $store)->applyCode($validated['code']);
  74. if ($problem !== null) {
  75. throw ValidationException::withMessages(['code' => StorefrontText::line($language, $problem)]);
  76. }
  77. return back();
  78. }
  79. public function removeDiscount(Request $request): RedirectResponse
  80. {
  81. $this->cart($request, $this->storefront($request)->store)->removeCode();
  82. return back();
  83. }
  84. public function remove(Request $request): RedirectResponse
  85. {
  86. $store = $this->storefront($request)->store;
  87. $this->cart($request, $store)->removeLine((string) $request->route('line'));
  88. return back();
  89. }
  90. /**
  91. * A product of this store that shoppers may buy. Anything else gives "not found".
  92. */
  93. private function buyableProduct(Store $store, int $id): Product
  94. {
  95. return $store->products()->active()->whereKey($id)->firstOrFail();
  96. }
  97. }