feat: add product view page
This commit is contained in:
@@ -117,4 +117,80 @@ class ProductsHandler
|
||||
}, $products),
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(Request $request): JsonResponse
|
||||
{
|
||||
$productId = (int) $request->get('id');
|
||||
$languageId = 1;
|
||||
$imageWidth = 500;
|
||||
$imageHeight = 500;
|
||||
|
||||
$product = $this->queryBuilder->newQuery()
|
||||
->select([
|
||||
'products.product_id' => 'product_id',
|
||||
'products.quantity' => 'product_quantity',
|
||||
'product_description.name' => 'product_name',
|
||||
'product_description.description' => 'product_description',
|
||||
'products.price' => 'price',
|
||||
'products.image' => 'product_image',
|
||||
'products.tax_class_id' => 'tax_class_id',
|
||||
])
|
||||
->from(db_table('product'), 'products')
|
||||
->join(
|
||||
db_table('product_description') . ' AS product_description',
|
||||
function (JoinClause $join) use ($languageId) {
|
||||
$join->on('products.product_id', '=', 'product_description.product_id')
|
||||
->where('product_description.language_id', '=', $languageId);
|
||||
}
|
||||
)
|
||||
->where('products.product_id', '=', $productId)
|
||||
->limit(1)
|
||||
->firstOrNull();
|
||||
|
||||
$productsImages = $this->queryBuilder->newQuery()
|
||||
->select([
|
||||
'products_images.product_id' => 'product_id',
|
||||
'products_images.image' => 'image',
|
||||
])
|
||||
->from(db_table('product_image'), 'products_images')
|
||||
->orderBy('products_images.sort_order', 'ASC')
|
||||
->where('products_images.product_id', '=', $productId)
|
||||
->get();
|
||||
|
||||
/** @var Closure $resize */
|
||||
$resize = Application::getInstance()->get('image_resize');
|
||||
|
||||
$images = [];
|
||||
$images[] = [
|
||||
'url' => $resize($product['product_image'], $imageWidth, $imageHeight),
|
||||
'alt' => $product['product_name'],
|
||||
];
|
||||
foreach ($productsImages as $item) {
|
||||
$images[] = [
|
||||
'url' => $resize($item['image'], $imageWidth, $imageHeight),
|
||||
'alt' => $product['product_name'],
|
||||
];
|
||||
}
|
||||
|
||||
$price = $this->currency->format(
|
||||
$this->tax->calculate(
|
||||
$product['price'],
|
||||
$product['tax_class_id'],
|
||||
$this->settings->get('oc_config_tax'),
|
||||
),
|
||||
$this->settings->get('oc_currency'),
|
||||
);
|
||||
|
||||
$data = [
|
||||
'id' => $product['product_id'],
|
||||
'name' => $product['product_name'],
|
||||
'description' => html_entity_decode($product['product_description']),
|
||||
'price' => $price,
|
||||
'images' => $images,
|
||||
];
|
||||
|
||||
return new JsonResponse([
|
||||
'data' => $data,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,5 +6,6 @@ use App\Handlers\ProductsHandler;
|
||||
|
||||
return [
|
||||
'products' => [ProductsHandler::class, 'handle'],
|
||||
'product_show' => [ProductsHandler::class, 'show'],
|
||||
'order_create' => [OrderCreateHandler::class, 'handle'],
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user