feat: add carousel for images
This commit is contained in:
@@ -11,6 +11,7 @@ use Openguru\OpenCartFramework\Http\JsonResponse;
|
||||
use Openguru\OpenCartFramework\Http\Request;
|
||||
use Openguru\OpenCartFramework\QueryBuilder\Builder;
|
||||
use Openguru\OpenCartFramework\QueryBuilder\JoinClause;
|
||||
use Openguru\OpenCartFramework\Support\Arr;
|
||||
|
||||
class ProductsHandler
|
||||
{
|
||||
@@ -31,7 +32,10 @@ class ProductsHandler
|
||||
{
|
||||
$languageId = 1;
|
||||
$page = $request->get('page', 1);
|
||||
$perPage = $request->get('perPage', 20);
|
||||
$perPage = $request->get('perPage', 10);
|
||||
|
||||
$imageWidth = 200;
|
||||
$imageHeight = 200;
|
||||
|
||||
$products = $this->queryBuilder->newQuery()
|
||||
->select([
|
||||
@@ -53,17 +57,42 @@ class ProductsHandler
|
||||
->forPage($page, $perPage)
|
||||
->get();
|
||||
|
||||
$productIds = Arr::pluck($products, 'product_id');
|
||||
$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')
|
||||
->whereIn('product_id', $productIds)
|
||||
->get();
|
||||
|
||||
/** @var Closure $resize */
|
||||
$resize = Application::getInstance()->get('image_resize');
|
||||
|
||||
$productsImagesMap = [];
|
||||
foreach ($productsImages as $item) {
|
||||
$productsImagesMap[$item['product_id']][] = [
|
||||
'url' => $resize($item['image'], $imageWidth, $imageHeight),
|
||||
'alt' => 'Product Image',
|
||||
];
|
||||
}
|
||||
|
||||
return new JsonResponse([
|
||||
'data' => array_map(function ($product) use ($resize) {
|
||||
'data' => array_map(function ($product) use ($resize, $productsImagesMap, $imageWidth, $imageHeight) {
|
||||
$allImages = [];
|
||||
if ($product['product_image']) {
|
||||
$image = $resize($product['product_image'], 500, 500);
|
||||
$image = $resize($product['product_image'], $imageWidth, $imageHeight);
|
||||
} else {
|
||||
$image = $resize('placeholder.png', 500, 500);
|
||||
$image = $resize('placeholder.png', $imageWidth, $imageHeight);
|
||||
}
|
||||
|
||||
$allImages[] = [
|
||||
'url' => $image,
|
||||
'alt' => $product['product_name'],
|
||||
];
|
||||
|
||||
$price = $this->currency->format(
|
||||
$this->tax->calculate(
|
||||
$product['price'],
|
||||
@@ -73,12 +102,16 @@ class ProductsHandler
|
||||
$this->settings->get('oc_currency'),
|
||||
);
|
||||
|
||||
if (! empty($productsImagesMap[$product['product_id']])) {
|
||||
$allImages = array_merge($allImages, $productsImagesMap[$product['product_id']]);
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => (int)$product['product_id'],
|
||||
'product_quantity' => (int)$product['product_quantity'],
|
||||
'name' => $product['product_name'],
|
||||
'price' => $price,
|
||||
'image' => $image,
|
||||
'images' => $allImages,
|
||||
];
|
||||
}, $products),
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user