AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CartController.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. $storefront = $this->storefront($request);
  21. $unpaid = $this->cart($request, $storefront->store)->unpaidOrder();
  22. return $this->renderStorefront($request, $storefront, [
  23. 'type' => 'cart',
  24. // An order this shopper placed in this browser and left on the payment page, so they can finish paying.
  25. 'unpaidOrder' => $unpaid === null ? null : [
  26. 'number' => $unpaid->number,
  27. 'public_id' => $unpaid->public_id,
  28. 'expires_at' => $unpaid->payment_expires_at?->toIso8601String(),
  29. ],
  30. ]);
  31. }
  32. /**
  33. * Add a product to the cart.
  34. */
  35. public function add(Request $request): RedirectResponse
  36. {
  37. $store = $this->storefront($request)->store;
  38. $validated = $request->validate([
  39. 'product_id' => ['required', 'integer'],
  40. 'variant_id' => ['nullable', 'integer'],
  41. 'quantity' => ['required', 'integer', 'min:1', 'max:'.Cart::MAX_QUANTITY],
  42. ]);
  43. $product = $this->buyableProduct($store, $validated['product_id']);
  44. $variant = null;
  45. // A product with options must be bought as one of its variants; a simple product never is.
  46. if ($product->hasVariants()) {
  47. $variant = $product->variants()->whereKey($validated['variant_id'] ?? 0)->first();
  48. if ($variant === null) {
  49. throw ValidationException::withMessages(['variant_id' => StorefrontText::line($store->language->value, 'choose_option')]);
  50. }
  51. } elseif (($validated['variant_id'] ?? null) !== null) {
  52. abort(404);
  53. }
  54. if (($variant !== null ? $variant->stock : $product->stock) < 1) {
  55. throw ValidationException::withMessages(['quantity' => StorefrontText::line($store->language->value, 'sold_out')]);
  56. }
  57. $this->cart($request, $store)->add($product, $variant, $validated['quantity']);
  58. return back();
  59. }
  60. /**
  61. * Change how many of a product are in the cart. Zero removes it.
  62. */
  63. public function update(Request $request): RedirectResponse
  64. {
  65. $store = $this->storefront($request)->store;
  66. $validated = $request->validate([
  67. 'quantity' => ['required', 'integer', 'min:0', 'max:'.Cart::MAX_QUANTITY],
  68. ]);
  69. $result = $this->cart($request, $store)->setLine((string) $request->route('line'), $validated['quantity']);
  70. abort_if($result === null, 404);
  71. return back();
  72. }
  73. /**
  74. * Put a discount code in the cart.
  75. */
  76. public function applyDiscount(Request $request): RedirectResponse
  77. {
  78. $store = $this->storefront($request)->store;
  79. $language = $store->language->value;
  80. $validated = $request->validate(['code' => ['required', 'string', 'max:40']], [
  81. 'code.required' => StorefrontText::line($language, 'discount_invalid'),
  82. ]);
  83. $problem = $this->cart($request, $store)->applyCode($validated['code']);
  84. if ($problem !== null) {
  85. throw ValidationException::withMessages(['code' => StorefrontText::line($language, $problem)]);
  86. }
  87. return back();
  88. }
  89. public function removeDiscount(Request $request): RedirectResponse
  90. {
  91. $this->cart($request, $this->storefront($request)->store)->removeCode();
  92. return back();
  93. }
  94. public function remove(Request $request): RedirectResponse
  95. {
  96. $store = $this->storefront($request)->store;
  97. $this->cart($request, $store)->removeLine((string) $request->route('line'));
  98. return back();
  99. }
  100. /**
  101. * A product of this store that shoppers may buy. Anything else gives "not found".
  102. */
  103. private function buyableProduct(Store $store, int $id): Product
  104. {
  105. return $store->products()->active()->whereKey($id)->firstOrFail();
  106. }
  107. }