commit before consolodating payment methods
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Product;
|
||||
use App\Services\ShippingService;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CartController extends Controller
|
||||
@@ -30,30 +31,36 @@ class CartController extends Controller
|
||||
$product = Product::find($productId);
|
||||
if ($product) {
|
||||
$quantity = is_array($cartItem) ? ($cartItem['quantity'] ?? 1) : $cartItem;
|
||||
$isSample = is_array($cartItem) ? ($cartItem['is_sample'] ?? false) : false;
|
||||
$stockCost = 0;
|
||||
$stock = null;
|
||||
|
||||
// Get print stock if available
|
||||
if ($printStockId) {
|
||||
$stock = $product->printStocks()->find($printStockId);
|
||||
if ($stock) {
|
||||
$stockCost = ($type === 'wallpaper') ? $stock->cost_per_meter : $stock->cost_per_m2;
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate price based on stock cost only (no base design cost)
|
||||
$basePrice = $stockCost;
|
||||
|
||||
if ($type === 'wallpaper') {
|
||||
$length = is_array($cartItem) ? ($cartItem['length'] ?? 0) : 0;
|
||||
$itemTotal = $basePrice * $length * $quantity;
|
||||
} elseif ($type === 'mural') {
|
||||
$width = is_array($cartItem) ? ($cartItem['width'] ?? 0) : 0;
|
||||
$height = is_array($cartItem) ? ($cartItem['height'] ?? 0) : 0;
|
||||
$m2 = $width * $height;
|
||||
$itemTotal = $basePrice * $m2 * $quantity;
|
||||
// For samples, use fixed sample cost
|
||||
if ($isSample) {
|
||||
$itemTotal = ShippingService::getSampleCost() * $quantity;
|
||||
} else {
|
||||
$itemTotal = $basePrice * $quantity;
|
||||
// Get print stock if available
|
||||
if ($printStockId) {
|
||||
$stock = $product->printStocks()->find($printStockId);
|
||||
if ($stock) {
|
||||
$stockCost = ($type === 'wallpaper') ? $stock->cost_per_meter : $stock->cost_per_m2;
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate price based on stock cost only (no base design cost)
|
||||
$basePrice = $stockCost;
|
||||
|
||||
if ($type === 'wallpaper') {
|
||||
$length = is_array($cartItem) ? ($cartItem['length'] ?? 0) : 0;
|
||||
$itemTotal = $basePrice * $length * $quantity;
|
||||
} elseif ($type === 'mural') {
|
||||
$width = is_array($cartItem) ? ($cartItem['width'] ?? 0) : 0;
|
||||
$height = is_array($cartItem) ? ($cartItem['height'] ?? 0) : 0;
|
||||
$m2 = $width * $height;
|
||||
$itemTotal = $basePrice * $m2 * $quantity;
|
||||
} else {
|
||||
$itemTotal = $basePrice * $quantity;
|
||||
}
|
||||
}
|
||||
|
||||
$total += $itemTotal;
|
||||
@@ -63,6 +70,7 @@ class CartController extends Controller
|
||||
'stock' => $stock,
|
||||
'quantity' => $quantity,
|
||||
'type' => $type,
|
||||
'is_sample' => $isSample,
|
||||
'length' => is_array($cartItem) ? ($cartItem['length'] ?? null) : null,
|
||||
'width' => is_array($cartItem) ? ($cartItem['width'] ?? null) : null,
|
||||
'height' => is_array($cartItem) ? ($cartItem['height'] ?? null) : null,
|
||||
@@ -72,9 +80,14 @@ class CartController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
$shippingFee = ShippingService::calculateShippingFee($total);
|
||||
$shippingLabel = ShippingService::getShippingLabel($total);
|
||||
|
||||
return view('cart', [
|
||||
'items' => $items,
|
||||
'total' => $total,
|
||||
'shippingFee' => $shippingFee,
|
||||
'shippingLabel' => $shippingLabel,
|
||||
'itemCount' => count($cart)
|
||||
]);
|
||||
}
|
||||
@@ -151,4 +164,43 @@ class CartController extends Controller
|
||||
session()->forget('cart');
|
||||
return redirect()->back()->with('success', 'Cart cleared!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Order a sample of a product
|
||||
*/
|
||||
public function orderSample(Request $request, Product $product)
|
||||
{
|
||||
$request->validate([
|
||||
'print_stock_id' => 'required|exists:print_stocks,id',
|
||||
]);
|
||||
|
||||
// Create a sample cart item
|
||||
$cart = session()->get('cart', []);
|
||||
$sampleSize = 0.3;
|
||||
|
||||
$cartItemKey = $product->id . '_sample_' . $request->input('print_stock_id') . '_' . uniqid();
|
||||
|
||||
$cartItem = [
|
||||
'product_id' => $product->id,
|
||||
'print_stock_id' => $request->input('print_stock_id'),
|
||||
'quantity' => 1,
|
||||
'type' => $product->type,
|
||||
'is_sample' => true,
|
||||
];
|
||||
|
||||
if ($product->type === 'wallpaper') {
|
||||
// For wallpaper samples: use 30cm length (0.3m) with default width
|
||||
$cartItem['width'] = 0.3;
|
||||
$cartItem['height'] = 0.3;
|
||||
} elseif ($product->type === 'mural') {
|
||||
// For mural samples: use 6cm × 5cm (0.06m × 0.05m = 0.003m²)
|
||||
$cartItem['width'] = 0.3;
|
||||
$cartItem['height'] = 0.3;
|
||||
}
|
||||
|
||||
$cart[$cartItemKey] = $cartItem;
|
||||
session()->put('cart', $cart);
|
||||
|
||||
return redirect()->route('cart')->with('success', 'Sample added to cart!');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user