This commit is contained in:
Sam Chen 2022-01-05 10:35:41 +08:00
parent de462ce77f
commit 834334efdb
11 changed files with 817 additions and 216 deletions

View File

@ -43,6 +43,8 @@ class ProductsController extends Controller
public function edit(Product $product)
{
$product->loadMissing('descriptions');
$data = [
'product' => $product,
];

View File

@ -4,13 +4,22 @@ namespace App\Http\Controllers\Shop;
use App\Http\Controllers\Controller;
use App\Models\ProductSku;
use App\Services\CartService;
use Illuminate\Http\Request;
class CartsController extends Controller
{
public function store(Request $request, ProductSku $sku)
public function store(Request $request)
{
$skuId = $request->sku_id;
$quantity = $request->quantity ?? 1;
dd($sku);
$sku = ProductSku::query()
->whereRelation('product', 'active', '=', true)
->findOrFail($skuId);
$cart = (new CartService)->add($sku, $quantity);
return $cart;
}
}

18
app/Models/Cart.php Normal file
View File

@ -0,0 +1,18 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Cart extends Model
{
use HasFactory;
protected $fillable = ['customer_id', 'selected', 'product_id', 'product_sku_id', 'quantity'];
public function sku()
{
return $this->belongsTo(ProductSku::class);
}
}

View File

@ -14,4 +14,9 @@ class ProductSku extends Model
protected $casts = [
'variants' => 'array',
];
public function product()
{
return $this->belongsTo(Product::class);
}
}

View File

@ -0,0 +1,32 @@
<?php
/**
* CartService.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Sam Chen <sam.chen@opencart.cn>
* @created 2022-01-05 10:12:57
* @modified 2022-01-05 10:12:57
*/
namespace App\Services;
use App\Models\Cart;
use App\Models\ProductSku;
class CartService
{
public function add(ProductSku $sku, int $quantity)
{
$cart = Cart::create([
'customer_id' => 0,
'product_id' => $sku->product_id,
'product_sku_id' => $sku->id,
'quantity' => $quantity,
'selected' => true,
]);
return $cart;
}
}

View File

@ -45,7 +45,7 @@ class ProductService
$skus = [];
foreach ($data['skus'] as $index => $sku) {
$sku->position = $index;
$sku['position'] = $index;
$skus[] = $sku;
}
$product->skus()->createMany($skus);

906
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -51,6 +51,16 @@ class CreateTables extends Migration
$table->boolean('is_default');
$table->timestamps();
});
Schema::create('carts', function (Blueprint $table) {
$table->id();
$table->integer('customer_id');
$table->boolean('selected');
$table->integer('product_id');
$table->integer('product_sku_id');
$table->unsignedInteger('quantity');
$table->timestamps();
});
}
/**
@ -63,5 +73,6 @@ class CreateTables extends Migration
Schema::dropIfExists('products');
Schema::dropIfExists('product_descriptions');
Schema::dropIfExists('product_skus');
Schema::dropIfExists('carts');
}
}

View File

@ -10,9 +10,18 @@
@csrf
@method(($product ?? null) ? 'PUT' : 'POST')
@php
if ($product ?? null) {
$descriptions = $product->descriptions->keyBy('locale');
} else {
$descriptions = [];
}
@endphp
<div>
@foreach (locales() as $locale)
<input type="text" name="descriptions[{{ $locale['code'] }}][name]" placeholder="Name {{ $locale['name'] }}" value="{{ old('name', $product->descriptions->name ?? '') }}">
<input type="text" name="descriptions[{{ $locale['code'] }}][name]" placeholder="Name {{ $locale['name'] }}" value="{{ old('descriptions.'.$locale['code'].'.name', $descriptions[$locale['code']]->name ?? '') }}">
@endforeach
</div>
@ -232,4 +241,4 @@
return results;
}
</script>
@endpush
@endpush

View File

@ -7,7 +7,7 @@ Route::prefix('/')
->group(function () {
Route::get('/', [App\Http\Controllers\Shop\HomeController::class, 'index'])->name('home.index');
Route::get('carts/{sku}', [App\Http\Controllers\Shop\CartsController::class, 'store'])->name('carts.store');
Route::get('carts', [App\Http\Controllers\Shop\CartsController::class, 'store'])->name('carts.store');
Route::get('products/{product}', [App\Http\Controllers\Shop\ProductsController::class, 'show'])->name('products.show');
});

View File

@ -10,12 +10,27 @@
<p>{{ $product->description->name }}</p>
<h2>SKUs</h2>
@foreach ($product->skus as $sku)
<p>
<span>model: {{ $sku->model }}</span>
<span>price: {{ $sku->price }}</span>
<a href="{{ route('shop.carts.store', $sku) }}">Add to cart</a>
</p>
@endforeach
<table>
<thead>
<tr>
<th>#</th>
<th>model</th>
<th>sku</th>
<th>price</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach ($product->skus as $sku)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $sku->model }}</td>
<td>{{ $sku->sku }}</td>
<td>{{ $sku->price }}</td>
<td><a href="{{ route('shop.carts.store', ['sku_id' => $sku->id]) }}">Add to cart</a></td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>