Some checks are pending
Telegram Mini App Shop Builder / Compute version metadata (push) Waiting to run
Telegram Mini App Shop Builder / Run Frontend tests (push) Waiting to run
Telegram Mini App Shop Builder / Run Backend tests (push) Waiting to run
Telegram Mini App Shop Builder / Run PHP_CodeSniffer (push) Waiting to run
Telegram Mini App Shop Builder / Build module. (push) Blocked by required conditions
Telegram Mini App Shop Builder / release (push) Blocked by required conditions
55 lines
1.3 KiB
PHP
Executable File
55 lines
1.3 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Handlers;
|
|
|
|
use App\Services\CartService;
|
|
use Cart\Cart;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Acme\ECommerceFramework\Http\Request;
|
|
|
|
class CartHandler
|
|
{
|
|
private Cart $cart;
|
|
private CartService $cartService;
|
|
|
|
public function __construct(Cart $cart, CartService $cartService)
|
|
{
|
|
$this->cart = $cart;
|
|
$this->cartService = $cartService;
|
|
}
|
|
|
|
public function index(): JsonResponse
|
|
{
|
|
$items = $this->cartService->getCart();
|
|
|
|
return new JsonResponse([
|
|
'data' => $items,
|
|
]);
|
|
}
|
|
|
|
public function checkout(Request $request): JsonResponse
|
|
{
|
|
$items = $request->json();
|
|
|
|
foreach ($items as $item) {
|
|
$options = [];
|
|
|
|
foreach ($item['options'] as $option) {
|
|
if (! empty($option['value']) && ! empty($option['value']['product_option_value_id'])) {
|
|
$options[$option['product_option_id']] = $option['value']['product_option_value_id'];
|
|
}
|
|
}
|
|
|
|
$this->cart->add(
|
|
$item['productId'],
|
|
$item['quantity'],
|
|
$options,
|
|
);
|
|
}
|
|
|
|
return new JsonResponse([
|
|
'data' => $items,
|
|
]);
|
|
}
|
|
}
|