Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kristi11 committed Dec 2, 2024
1 parent eb26c28 commit 4f9b1d8
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 184 deletions.
170 changes: 77 additions & 93 deletions app/Actions/Shop/CreateStripeCheckoutSession.php
Original file line number Diff line number Diff line change
@@ -1,118 +1,65 @@
<?php

namespace App\Actions\Shop;

use App\Enums\Age;
use App\Enums\Colors;
use App\Enums\CoreCount;
use App\Enums\DStorage;
use App\Enums\EngineVolume;
use App\Enums\Finish;
use App\Enums\Gender;
use App\Enums\GraphicCardType;
use App\Enums\Length;
use App\Enums\Material;
use App\Enums\MemorySize;
use App\Enums\OutfitSizes;
use App\Enums\Patterns;
use App\Enums\ProcessorType;
use App\Enums\Style;
use App\Enums\Volume;
use App\Enums\Weight;
use App\Models\Cart;
use App\Models\CartItems;
use Illuminate\Database\Eloquent\Collection;
use Laravel\Cashier\Checkout;

class CreateStripeCheckoutSession
{
public function createFromCart(Cart $cart)
public function createFromCart(Cart $cart): Checkout
{
return $cart->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,
Expand All @@ -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;
}
}
38 changes: 38 additions & 0 deletions app/Livewire/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,6 +12,7 @@
class Cart extends Component
{


#[Computed]
public function cart()
{
Expand Down Expand Up @@ -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');
Expand Down
1 change: 0 additions & 1 deletion app/Livewire/ProductInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public function price(): Attribute
}
);
}

/**
* Adds a product variant to the cart after validating the request and dispatches notifications.
*
Expand Down
4 changes: 2 additions & 2 deletions config/filament-shield.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions database/factories/ProductVariantFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
];
}
}
Loading

0 comments on commit 4f9b1d8

Please sign in to comment.