diff --git a/app/Actions/Shop/CreateStripeCheckoutSession.php b/app/Actions/Shop/CreateStripeCheckoutSession.php index 371ea0bd..4c1a0cdc 100644 --- a/app/Actions/Shop/CreateStripeCheckoutSession.php +++ b/app/Actions/Shop/CreateStripeCheckoutSession.php @@ -1,118 +1,65 @@ user + return $cart->user ->allowPromotionCodes() ->checkout( - $this->formatCartItems($cart->items), - [ - 'customer_update' => [ - 'shipping' => 'auto', - ], - 'shipping_address_collection' => [ - 'allowed_countries' => ['US'], - ], - 'success_url' => route('checkout-status').'?session_id={CHECKOUT_SESSION_ID}', - 'cancel_url' => route('cart'), - 'metadata' => [ - 'user_id' => $cart->user->id, - 'cart_id' => $cart->id, - ] - ] - ); + $this->formatCartItems($cart->items), + [ + 'customer_update' => [ + 'shipping' => 'auto', + ], + 'shipping_address_collection' => [ + 'allowed_countries' => ['US'], + ], + 'success_url' => route('checkout-status').'?session_id={CHECKOUT_SESSION_ID}', + 'cancel_url' => route('cart'), + 'metadata' => [ + 'user_id' => $cart->user->id, + 'cart_id' => $cart->id, + ], + ] + ); } - private function formatCartItems(Collection $items) + private function formatCartItems(Collection $items): array { return $items->loadMissing('product', 'variant')->map(function (CartItems $item) { - $descriptionItems = []; - - if (!is_null($item->variant->size)) { - $descriptionItems[] = "Size: {$item->variant->size}"; - } + $descriptionItems = $this->generateDescriptionItems($item); - if (!is_null($item->variant->color)) { - $descriptionItems[] = "Color: {$item->variant->color->getLabel()}"; - } - - if (!is_null($item->variant->corecount)) { - $descriptionItems[] = "Core count: {$item->variant->corecount->getLabel()}"; - } - - if (!is_null($item->variant->material)) { - $descriptionItems[] = "Material: {$item->variant->material->getLabel()}"; - } - - if (!is_null($item->variant->enginevolume)) { - $descriptionItems[] = "Engine volume: {$item->variant->enginevolume->getLabel()}"; - } - - if (!is_null($item->variant->dstorage)) { - $descriptionItems[] = "Digital storage: {$item->variant->dstorage->getLabel()}"; - } - - if (!is_null($item->variant->graphiccardtype)) { - $descriptionItems[] = "Graphic card type: {$item->variant->graphiccardtype->getLabel()}"; - } - - if (!is_null($item->variant->memorysize)) { - $descriptionItems[] = "Memory size: {$item->variant->memorysize->getLabel()}"; - } - - if (!is_null($item->variant->processortype)) { - $descriptionItems[] = "Processor type: {$item->variant->processortype->getLabel()}"; - } - - if (!is_null($item->variant->style)) { - $descriptionItems[] = "Style: {$item->variant->style->getLabel()}"; - } - - if (!is_null($item->variant->volume)) { - $descriptionItems[] = "Volume: {$item->variant->volume->getLabel()}"; - } - - if (!is_null($item->variant->age)) { - $descriptionItems[] = "Age: {$item->variant->age->getLabel()}"; - } - - if (!is_null($item->variant->pattern)) { - $descriptionItems[] = "Pattern: {$item->variant->pattern->getLabel()}"; - } - - if (!is_null($item->variant->weight)) { - $descriptionItems[] = "Weight: {$item->variant->weight->getLabel()}"; - } - - if (!is_null($item->variant->length)) { - $descriptionItems[] = "Length: {$item->variant->length->getLabel()}"; - } - - if (!is_null($item->variant->finish)) { - $descriptionItems[] = "Finish: {$item->variant->finish->getLabel()}"; - } - - if (!is_null($item->variant->gender)) { - $descriptionItems[] = "Gender: {$item->variant->gender->getLabel()}"; - } - - // Convert array to string, with items separated by " - " - $description = implode(" - ", $descriptionItems); - return [ + return [ 'price_data' => [ 'currency' => 'USD', 'unit_amount' => $item->product->price * 100, 'product_data' => [ 'name' => $item->product->name, - 'description' => $description, + 'description' => implode(' - ', $descriptionItems), 'metadata' => [ 'product_id' => $item->product->id, 'product_variant_id' => $item->product_variant_id, @@ -123,4 +70,41 @@ private function formatCartItems(Collection $items) ]; })->toArray(); } + + private function generateDescriptionItems(CartItems $item): array + { + $attributes = [ + 'size' => OutfitSizes::class, + 'color' => Colors::class, + 'corecount' => CoreCount::class, + 'material' => Material::class, + 'enginevolume' => EngineVolume::class, + 'dstorage' => DStorage::class, + 'graphiccardtype' => GraphicCardType::class, + 'memorysize' => MemorySize::class, + 'processortype' => ProcessorType::class, + 'style' => Style::class, + 'volume' => Volume::class, + 'age' => Age::class, + 'pattern' => Patterns::class, + 'weight' => Weight::class, + 'length' => Length::class, + 'finish' => Finish::class, + 'gender' => Gender::class, + ]; + + $descriptionItems = []; + + foreach ($attributes as $attribute => $enumClass) { + $value = $item->variant->$attribute; + + if ($value instanceof $enumClass) { + $descriptionItems[] = ucfirst($attribute).": {$value->getLabel()}"; + } elseif (!is_null($value)) { + $descriptionItems[] = ucfirst($attribute).": $value"; + } + } + + return $descriptionItems; + } } diff --git a/app/Livewire/Cart.php b/app/Livewire/Cart.php index ad01496e..c23b764c 100644 --- a/app/Livewire/Cart.php +++ b/app/Livewire/Cart.php @@ -3,6 +3,7 @@ namespace App\Livewire; use App\Actions\Shop\CreateStripeCheckoutSession; +use App\Enums\Weight; use App\Factories\CartFactory; use Livewire\Attributes\Computed; use Livewire\Attributes\On; @@ -11,6 +12,7 @@ class Cart extends Component { + #[Computed] public function cart() { @@ -62,6 +64,42 @@ public function delete($itemId): void $this->cart->items()->where('id', $itemId)->delete(); $this->dispatch('productDeletedFromCart'); } + + public function renderAttributes($variant): array + { + $attributes = [ + 'size' => \App\Enums\OutfitSizes::class, + 'color' => \App\Enums\Colors::class, + 'corecount' => \App\Enums\CoreCount::class, + 'dstorage' => \App\Enums\DStorage::class, + 'enginevolume' => \App\Enums\EngineVolume::class, + 'graphiccardtype' => \App\Enums\GraphicCardType::class, + 'material' => \App\Enums\Material::class, + 'memorysize' => \App\Enums\MemorySize::class, + 'processortype' => \App\Enums\ProcessorType::class, + 'style' => \App\Enums\Style::class, + 'volume' => \App\Enums\Volume::class, + 'pattern' => \App\Enums\Patterns::class, + 'weight' => \App\Enums\Weight::class, + 'length' => \App\Enums\Length::class, + 'finish' => \App\Enums\Finish::class, + 'gender' => \App\Enums\Gender::class, + ]; + + $output = []; + + foreach ($attributes as $key => $enumClass) { + $value = $variant->$key ?? null; + if ($value instanceof $enumClass) { + $output[$key] = ucfirst($key) . ': ' . $value->getLabel(); + } elseif (!is_null($value)) { + $output[$key] = ucfirst($key) . ': ' . $value; + } + } + + return $output; + } + public function render() { return view('livewire.cart'); diff --git a/app/Livewire/ProductInfo.php b/app/Livewire/ProductInfo.php index 0a7a935c..d60d3e21 100644 --- a/app/Livewire/ProductInfo.php +++ b/app/Livewire/ProductInfo.php @@ -31,7 +31,6 @@ public function price(): Attribute } ); } - /** * Adds a product variant to the cart after validating the request and dispatches notifications. * diff --git a/config/filament-shield.php b/config/filament-shield.php index 77199182..451c6c52 100644 --- a/config/filament-shield.php +++ b/config/filament-shield.php @@ -6,8 +6,8 @@ 'slug' => 'shield/roles', 'navigation_sort' => -1, 'navigation_badge' => true, - 'navigation_group' => false, - 'is_globally_searchable' => true, + 'navigation_group' => true, + 'is_globally_searchable' => false, 'show_model_path' => true, 'is_scoped_to_tenant' => true, 'cluster' => null, diff --git a/database/factories/ProductVariantFactory.php b/database/factories/ProductVariantFactory.php index 3fd2d238..9e65dde7 100644 --- a/database/factories/ProductVariantFactory.php +++ b/database/factories/ProductVariantFactory.php @@ -2,6 +2,8 @@ namespace Database\Factories; +use App\Enums\Colors; +use App\Enums\OutfitSizes; use App\Models\ProductVariant; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Carbon; @@ -16,8 +18,8 @@ public function definition(): array 'created_at' => Carbon::now(), 'updated_at' => Carbon::now(), 'product_id' => $this->faker->randomNumber(), - 'color' => $this->faker->randomElement(['Blue', 'Green', 'Yellow', 'Red']), - 'size' => $this->faker->randomElement(['S', 'M', 'L']), + 'color' => $this->faker->randomElement(Colors::class), + 'size' => $this->faker->randomElement(OutfitSizes::class), ]; } } diff --git a/resources/views/components/variants/content.blade.php b/resources/views/components/variants/content.blade.php new file mode 100644 index 00000000..cb1c1e52 --- /dev/null +++ b/resources/views/components/variants/content.blade.php @@ -0,0 +1,176 @@ +
- {{'Size: '.$item->variant->size}} + @foreach ($this->renderAttributes($item->variant) as $label) +
+ {{ $label }}
- @endif - @if($item->variant->color) -- {{'Color: '.$item->variant->color->getLabel()}} -
- @endif - @if($item->variant->corecount) -- {{'Core count: '.$item->variant->corecount->getLabel()}} -
- @endif - @if($item->variant->dstorage) -- {{'Digital storage: '.$item->variant->dstorage->getLabel()}} -
- @endif - @if($item->variant->enginevolume) -- {{'Engine volume: '.$item->variant->enginevolume->getLabel()}} -
- @endif - @if($item->variant->graphiccardtype) -- {{'Graphic card type: '.$item->variant->graphiccardtype->getLabel()}} -
- @endif - @if($item->variant->material) -- {{'Material: '.$item->variant->material->getLabel()}} -
- @endif - @if($item->variant->memorysize) -- {{'Memory size: '.$item->variant->memorysize->getLabel()}} -
- @endif - @if($item->variant->processortype) -- {{'Processor type: '.$item->variant->processortype->getLabel()}} -
- @endif - @if($item->variant->atyle) -- {{'style: '.$item->variant->atyle->getLabel()}} -
- @endif - @if($item->variant->volume) -- {{'Volume: '.$item->variant->volume->getLabel()}} -
- @endif + @endforeach