AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ProductVariant.php 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. /**
  6. * One combination of a product's options, such as "500g / Pedas", with its own price and stock.
  7. * Prices are in sen (1 ringgit = 100 sen).
  8. *
  9. * @property int $id
  10. * @property int $store_id
  11. * @property int $product_id
  12. * @property string $option1
  13. * @property string|null $option2
  14. * @property int $price
  15. * @property int|null $compare_at_price
  16. * @property int $stock
  17. * @property int $position
  18. * @property-read Product $product
  19. */
  20. class ProductVariant extends Model
  21. {
  22. /**
  23. * @return BelongsTo<Product, $this>
  24. */
  25. public function product(): BelongsTo
  26. {
  27. return $this->belongsTo(Product::class);
  28. }
  29. /**
  30. * The option values in order, without empty ones.
  31. *
  32. * @return list<string>
  33. */
  34. public function values(): array
  35. {
  36. return array_values(array_filter([$this->option1, $this->option2], fn (?string $value) => $value !== null && $value !== ''));
  37. }
  38. /**
  39. * "500g / Pedas".
  40. */
  41. public function name(): string
  42. {
  43. return implode(' / ', $this->values());
  44. }
  45. /**
  46. * What the storefront is allowed to see. Prices are in ringgit here.
  47. *
  48. * @return array<string, mixed>
  49. */
  50. public function toStorefront(): array
  51. {
  52. return [
  53. 'id' => $this->id,
  54. 'name' => $this->name(),
  55. 'options' => $this->values(),
  56. 'price' => $this->price / 100,
  57. 'compare_at_price' => $this->compare_at_price !== null ? $this->compare_at_price / 100 : null,
  58. 'in_stock' => $this->stock > 0,
  59. 'stock_left' => $this->stock,
  60. ];
  61. }
  62. /**
  63. * @return array<string, string>
  64. */
  65. protected function casts(): array
  66. {
  67. return [
  68. 'price' => 'integer',
  69. 'compare_at_price' => 'integer',
  70. 'stock' => 'integer',
  71. 'position' => 'integer',
  72. ];
  73. }
  74. }