FleetCart/Themes/Storefront/Http/ViewComposers/ProductIndexPageComposer.php

68 lines
1.6 KiB
PHP
Raw Normal View History

2023-06-11 12:14:03 +00:00
<?php
namespace Themes\Storefront\Http\ViewComposers;
2023-12-03 14:07:47 +00:00
use Illuminate\View\View;
2023-06-11 12:14:03 +00:00
use Modules\Support\Money;
use Modules\Product\Entities\Product;
use Modules\Category\Entities\Category;
2023-12-03 14:07:47 +00:00
use Modules\Product\Entities\ProductVariant;
2023-06-11 12:14:03 +00:00
class ProductIndexPageComposer
{
/**
* Bind data to the view.
*
2023-12-03 14:07:47 +00:00
* @param View $view
*
2023-06-11 12:14:03 +00:00
* @return void
*/
public function compose($view)
{
$view->with([
'categories' => $this->categories(),
2023-12-03 14:07:47 +00:00
'minPrice' => $this->minPrice(),
2023-06-11 12:14:03 +00:00
'maxPrice' => $this->maxPrice(),
'latestProducts' => $this->latestProducts(),
]);
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function categories()
{
return Category::tree();
}
2023-12-03 14:07:47 +00:00
private function minPrice()
{
$minProductPrice = Product::min('selling_price');
$minVariantPrice = ProductVariant::min('selling_price');
$minPrice = min($minProductPrice, $minVariantPrice);
return Money::inDefaultCurrency($minPrice)
->convertToCurrentCurrency()
->ceil()
->amount();
}
2023-06-11 12:14:03 +00:00
private function maxPrice()
{
2023-12-03 14:07:47 +00:00
$maxProductPrice = Product::max('selling_price');
$maxVariantPrice = ProductVariant::max('selling_price');
$maxPrice = max($maxProductPrice, $maxVariantPrice);
return Money::inDefaultCurrency($maxPrice)
2023-06-11 12:14:03 +00:00
->convertToCurrentCurrency()
->ceil()
->amount();
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function latestProducts()
{
return Product::forCard()->take(5)->latest()->get()->map->clean();
}
}