wip: shopping cart, product options
This commit is contained in:
@@ -5,39 +5,28 @@ namespace App\Handlers;
|
||||
use Cart\Cart;
|
||||
use Openguru\OpenCartFramework\Http\JsonResponse;
|
||||
use Openguru\OpenCartFramework\Http\Request;
|
||||
use Openguru\OpenCartFramework\QueryBuilder\Connections\ConnectionInterface;
|
||||
use Openguru\OpenCartFramework\ImageTool\ImageToolInterface;
|
||||
|
||||
class CheckoutHandler
|
||||
class CartHandler
|
||||
{
|
||||
private Cart $cart;
|
||||
private \DB $connection;
|
||||
private ImageToolInterface $imageTool;
|
||||
|
||||
public function __construct(Cart $cart, \DB $database)
|
||||
public function __construct(Cart $cart, \DB $database, ImageToolInterface $imageTool)
|
||||
{
|
||||
$this->cart = $cart;
|
||||
$this->database = $database;
|
||||
$this->imageTool = $imageTool;
|
||||
}
|
||||
|
||||
public function addToCart(Request $request): JsonResponse
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$item = $request->json();
|
||||
|
||||
$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' => $this->cart->getProducts(),
|
||||
'data' => [
|
||||
'products' => $this->getProducts(),
|
||||
'count' => $this->cart->countProducts(),
|
||||
'total' => $this->cart->getTotal(),
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -65,4 +54,15 @@ class CheckoutHandler
|
||||
'data' => $items,
|
||||
]);
|
||||
}
|
||||
|
||||
private function getProducts(): array
|
||||
{
|
||||
$products = $this->cart->getProducts();
|
||||
|
||||
foreach ($products as &$product) {
|
||||
$product['thumb'] = $this->imageTool->resize($product['image'], 100, 100, 'placeholder.png');
|
||||
}
|
||||
|
||||
return $products;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Handlers\CategoriesHandler;
|
||||
use App\Handlers\CheckoutHandler;
|
||||
use App\Handlers\CartHandler;
|
||||
use App\Handlers\HelloWorldHandler;
|
||||
use App\Handlers\OrderCreateHandler;
|
||||
use App\Handlers\ProductsHandler;
|
||||
@@ -13,6 +13,6 @@ return [
|
||||
|
||||
'categoriesList' => [CategoriesHandler::class, 'index'],
|
||||
|
||||
'checkout' => [CheckoutHandler::class, 'checkout'],
|
||||
'addToCart' => [CheckoutHandler::class, 'addToCart'],
|
||||
'checkout' => [CartHandler::class, 'checkout'],
|
||||
'cart' => [CartHandler::class, 'index'],
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user