first upload all files
This commit is contained in:
40
Modules/Product/Http/Controllers/Admin/ProductController.php
Normal file
40
Modules/Product/Http/Controllers/Admin/ProductController.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Product\Http\Controllers\Admin;
|
||||
|
||||
use Modules\Product\Entities\Product;
|
||||
use Modules\Admin\Traits\HasCrudActions;
|
||||
use Modules\Product\Http\Requests\SaveProductRequest;
|
||||
|
||||
class ProductController
|
||||
{
|
||||
use HasCrudActions;
|
||||
|
||||
/**
|
||||
* Model for the resource.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $model = Product::class;
|
||||
|
||||
/**
|
||||
* Label of the resource.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $label = 'product::products.product';
|
||||
|
||||
/**
|
||||
* View path of the resource.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $viewPath = 'product::admin.products';
|
||||
|
||||
/**
|
||||
* Form requests for the resource.
|
||||
*
|
||||
* @var array|string
|
||||
*/
|
||||
protected $validation = SaveProductRequest::class;
|
||||
}
|
||||
68
Modules/Product/Http/Controllers/ProductController.php
Normal file
68
Modules/Product/Http/Controllers/ProductController.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Product\Http\Controllers;
|
||||
|
||||
use Illuminate\Routing\Controller;
|
||||
use Modules\Review\Entities\Review;
|
||||
use Modules\Product\Entities\Product;
|
||||
use Modules\Product\Events\ProductViewed;
|
||||
use Modules\Product\Filters\ProductFilter;
|
||||
use Modules\Product\Http\Middleware\SetProductSortOption;
|
||||
|
||||
class ProductController extends Controller
|
||||
{
|
||||
use ProductSearch;
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware(SetProductSortOption::class)->only('index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @param \Modules\Product\Entities\Product $model
|
||||
* @param \Modules\Product\Filters\ProductFilter $productFilter
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index(Product $model, ProductFilter $productFilter)
|
||||
{
|
||||
if (request()->expectsJson()) {
|
||||
return $this->searchProducts($model, $productFilter);
|
||||
}
|
||||
|
||||
return view('public.products.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*
|
||||
* @param string $slug
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($slug)
|
||||
{
|
||||
$product = Product::findBySlug($slug);
|
||||
$relatedProducts = $product->relatedProducts()->forCard()->get();
|
||||
$upSellProducts = $product->upSellProducts()->forCard()->get();
|
||||
$review = $this->getReviewData($product);
|
||||
|
||||
event(new ProductViewed($product));
|
||||
|
||||
return view('public.products.show', compact('product', 'relatedProducts', 'upSellProducts', 'review'));
|
||||
}
|
||||
|
||||
private function getReviewData(Product $product)
|
||||
{
|
||||
if (! setting('reviews_enabled')) {
|
||||
return;
|
||||
}
|
||||
|
||||
return Review::countAndAvgRating($product);
|
||||
}
|
||||
}
|
||||
52
Modules/Product/Http/Controllers/ProductPriceController.php
Normal file
52
Modules/Product/Http/Controllers/ProductPriceController.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Product\Http\Controllers;
|
||||
|
||||
use Modules\Cart\CartItem;
|
||||
use Darryldecode\Cart\ItemCollection;
|
||||
use Modules\Product\Entities\Product;
|
||||
use Modules\Product\Services\ChosenProductOptions;
|
||||
|
||||
class ProductPriceController
|
||||
{
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$product = Product::queryWithoutEagerRelations()
|
||||
->select('id')
|
||||
->withPrice()
|
||||
->findOrFail($id);
|
||||
|
||||
$variantPrice = $this->cartItem($product, request('options', []))
|
||||
->total()
|
||||
->convertToCurrentCurrency()
|
||||
->format();
|
||||
|
||||
return product_price_formatted($product, function ($price) use ($product, $variantPrice) {
|
||||
if (! $product->hasSpecialPrice()) {
|
||||
return $variantPrice;
|
||||
}
|
||||
|
||||
return "{$variantPrice} <span class='previous-price'>{$price}</span>";
|
||||
});
|
||||
}
|
||||
|
||||
private function cartItem(Product $product, array $options)
|
||||
{
|
||||
$chosenOptions = new ChosenProductOptions($product, $options);
|
||||
|
||||
return new CartItem(new ItemCollection([
|
||||
'id' => $product->id,
|
||||
'quantity' => 1,
|
||||
'attributes' => [
|
||||
'product' => $product,
|
||||
'options' => $chosenOptions->getEntities(),
|
||||
],
|
||||
]));
|
||||
}
|
||||
}
|
||||
74
Modules/Product/Http/Controllers/ProductSearch.php
Normal file
74
Modules/Product/Http/Controllers/ProductSearch.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Product\Http\Controllers;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\Product\Entities\Product;
|
||||
use Modules\Category\Entities\Category;
|
||||
use Modules\Attribute\Entities\Attribute;
|
||||
use Modules\Product\Filters\ProductFilter;
|
||||
use Modules\Product\Events\ShowingProductList;
|
||||
|
||||
trait ProductSearch
|
||||
{
|
||||
/**
|
||||
* Search products for the request.
|
||||
*
|
||||
* @param \Modules\Product\Entities\Product $model
|
||||
* @param \Modules\Product\Filters\ProductFilter $productFilter
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function searchProducts(Product $model, ProductFilter $productFilter)
|
||||
{
|
||||
$productIds = [];
|
||||
|
||||
if (request()->filled('query')) {
|
||||
$model = $model->search(request('query'));
|
||||
$productIds = $model->keys();
|
||||
}
|
||||
|
||||
$query = $model->filter($productFilter);
|
||||
|
||||
if (request()->filled('category')) {
|
||||
$productIds = (clone $query)->select('products.id')->resetOrders()->pluck('id');
|
||||
}
|
||||
|
||||
$products = $query->paginate(request('perPage', 30));
|
||||
|
||||
event(new ShowingProductList($products));
|
||||
|
||||
return response()->json([
|
||||
'products' => $products,
|
||||
'attributes' => $this->getAttributes($productIds),
|
||||
]);
|
||||
}
|
||||
|
||||
private function getAttributes($productIds)
|
||||
{
|
||||
if (! request()->filled('category') || $this->filteringViaRootCategory()) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
return Attribute::with('values')
|
||||
->where('is_filterable', true)
|
||||
->whereHas('categories', function ($query) use ($productIds) {
|
||||
$query->whereIn('id', $this->getProductsCategoryIds($productIds));
|
||||
})
|
||||
->get();
|
||||
}
|
||||
|
||||
private function filteringViaRootCategory()
|
||||
{
|
||||
return Category::where('slug', request('category'))
|
||||
->firstOrNew([])
|
||||
->isRoot();
|
||||
}
|
||||
|
||||
private function getProductsCategoryIds($productIds)
|
||||
{
|
||||
return DB::table('product_categories')
|
||||
->whereIn('product_id', $productIds)
|
||||
->distinct()
|
||||
->pluck('category_id');
|
||||
}
|
||||
}
|
||||
83
Modules/Product/Http/Controllers/SuggestionController.php
Normal file
83
Modules/Product/Http/Controllers/SuggestionController.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Product\Http\Controllers;
|
||||
|
||||
use Modules\Product\Entities\Product;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Modules\Product\Http\Response\SuggestionsResponse;
|
||||
|
||||
class SuggestionController
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index(Product $model)
|
||||
{
|
||||
$products = $this->getProducts($model);
|
||||
|
||||
return new SuggestionsResponse(
|
||||
request('query'),
|
||||
$products,
|
||||
$products->pluck('categories')->flatten(),
|
||||
$this->getTotalResults($model)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get total results count.
|
||||
*
|
||||
* @param \Modules\Product\Entities\Product $model
|
||||
* @return int
|
||||
*/
|
||||
private function getTotalResults(Product $model)
|
||||
{
|
||||
return $model->search(request('query'))
|
||||
->query()
|
||||
->when(request()->filled('category'), $this->categoryQuery())
|
||||
->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get products suggestions.
|
||||
*
|
||||
* @param \Modules\Product\Entities\Product $model
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
private function getProducts(Product $model)
|
||||
{
|
||||
return $model->search(request('query'))
|
||||
->query()
|
||||
->limit(10)
|
||||
->withName()
|
||||
->withBaseImage()
|
||||
->withPrice()
|
||||
->addSelect([
|
||||
'products.id',
|
||||
'products.slug',
|
||||
'products.in_stock',
|
||||
'products.manage_stock',
|
||||
'products.qty',
|
||||
])
|
||||
->with(['files', 'categories' => function ($query) {
|
||||
$query->limit(5);
|
||||
}])
|
||||
->when(request()->filled('category'), $this->categoryQuery())
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns categories condition closure.
|
||||
*
|
||||
* @return \Closure
|
||||
*/
|
||||
private function categoryQuery()
|
||||
{
|
||||
return function (Builder $query) {
|
||||
$query->whereHas('categories', function ($categoryQuery) {
|
||||
$categoryQuery->where('slug', request('category'));
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user