AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Announcements.php 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Storefront;
  3. use App\Models\AutomaticDiscount;
  4. use App\Models\Store;
  5. use Illuminate\Support\Collection;
  6. /**
  7. * The lines in the slim bar above the store's header.
  8. *
  9. * The merchant's own lines come first, then lines the store writes by itself from what is true right now:
  10. * free shipping, and every automatic discount that is running. So the bar never shows an offer that has ended.
  11. */
  12. final class Announcements
  13. {
  14. public const int MAX_OWN = 3;
  15. public const int MAX_LENGTH = 90;
  16. /**
  17. * @param Collection<int, AutomaticDiscount> $offers The automatic discounts running now.
  18. * @return list<string>
  19. */
  20. public static function for(Store $store, Collection $offers): array
  21. {
  22. if (! $store->announcement_enabled) {
  23. return [];
  24. }
  25. return array_merge(self::own($store), self::automatic($store, $offers));
  26. }
  27. /**
  28. * Lines the store writes by itself.
  29. *
  30. * @param Collection<int, AutomaticDiscount> $offers
  31. * @return list<string>
  32. */
  33. public static function automatic(Store $store, Collection $offers): array
  34. {
  35. $language = $store->language->value;
  36. $lines = [];
  37. if ($store->free_shipping_over !== null) {
  38. $lines[] = $store->free_shipping_over === 0
  39. ? StorefrontText::line($language, 'announce_free_shipping_all')
  40. : StorefrontText::line($language, 'announce_free_shipping', [':amount' => 'RM '.number_format($store->free_shipping_over / 100, 2)]);
  41. }
  42. foreach ($offers->take(3) as $offer) {
  43. $lines[] = $offer->name.': '.$offer->describe($language);
  44. }
  45. return $lines;
  46. }
  47. /**
  48. * @return list<string>
  49. */
  50. private static function own(Store $store): array
  51. {
  52. return array_slice(array_values(array_filter(
  53. array_map(fn (mixed $line) => trim((string) $line), $store->announcement_messages ?? []),
  54. fn (string $line) => $line !== '',
  55. )), 0, self::MAX_OWN);
  56. }
  57. }