Base Structure And Methods Refactore For AJAX Call

This commit is contained in:
Devansh 2021-10-25 19:07:29 +05:30
parent 8f0548b70c
commit f6db7fe7ac
3 changed files with 152 additions and 129 deletions

View File

@ -2,41 +2,73 @@
namespace Webkul\Product\Repositories;
use Illuminate\Container\Container as App;
use Webkul\Attribute\Repositories\AttributeRepository;
use Webkul\Core\Eloquent\Repository;
class ProductFlatRepository extends Repository
{
public function model()
{
return 'Webkul\Product\Contracts\ProductFlat';
}
/**
* Maximum Price of Category Product
* Create a new repository instance.
*
* @param \Webkul\Category\Contracts\Category $category
* @return float
* @param \Webkul\Attribute\Repositories\AttributeRepository $attributeRepository
* @param \Illuminate\Container\Container $app
* @return void
*/
public function getCategoryProductMaximumPrice($category = null)
{
static $loadedCategoryMaxPrice = [];
public function __construct(
AttributeRepository $attributeRepository,
App $app
) {
$this->attributeRepository = $attributeRepository;
if (! $category) {
return $this->model->max('max_price');
}
if (array_key_exists($category->id, $loadedCategoryMaxPrice)) {
return $loadedCategoryMaxPrice[$category->id];
}
return $loadedCategoryMaxPrice[$category->id] = $this->model
->leftJoin('product_categories', 'product_flat.product_id', 'product_categories.product_id')
->where('product_categories.category_id', $category->id)
->max('max_price');
parent::__construct($app);
}
/**
* get Category Product Attribute
* Specify model.
*
* @return string
*/
public function model(): string
{
return \Webkul\Product\Contracts\ProductFlat::class;
}
/**
* Get category product model.
*
* @param int $categoryId
* @return \Illuminate\Support\Querybuilder
*/
public function categoryProductQuerybuilder($categoryId)
{
return $this->model
->leftJoin('product_categories', 'product_flat.product_id', 'product_categories.product_id')
->where('product_categories.category_id', $categoryId)
->where('product_flat.channel', core()->getCurrentChannelCode())
->where('product_flat.locale', app()->getLocale());
}
/**
* Update `product_flat` custom column.
*
* @param \Webkul\Attribute\Models\Attribute $attribute
* @param \Webkul\Product\Listeners\ProductFlat $listener
* @return object
*/
public function updateAttributeColumn(
\Webkul\Attribute\Models\Attribute $attribute,
\Webkul\Product\Listeners\ProductFlat $listener
) {
return $this->model
->leftJoin('product_attribute_values as v', function ($join) use ($attribute) {
$join->on('product_flat.id', '=', 'v.product_id')
->on('v.attribute_id', '=', \DB::raw($attribute->id));
})->update(['product_flat.' . $attribute->code => \DB::raw($listener->attributeTypeFields[$attribute->type] . '_value')]);
}
/**
* Get category product attribute.
*
* @param int $categoryId
* @return array
@ -49,20 +81,20 @@ class ProductFlatRepository extends Repository
$productIds = $qb->pluck('product_flat.product_id')->toArray();
$childProductIds = $this->model->distinct()
->whereIn('parent_id', $productFlatIds)
->pluck('product_id')->toArray();
->whereIn('parent_id', $productFlatIds)
->pluck('product_id')->toArray();
$productIds = array_merge($productIds, $childProductIds);
$attributeValues = $this->model
->distinct()
->leftJoin('product_attribute_values as pa', 'product_flat.product_id', 'pa.product_id')
->leftJoin('attributes as at', 'pa.attribute_id', 'at.id')
->leftJoin('product_super_attributes as ps', 'product_flat.product_id', 'ps.product_id')
->select('pa.integer_value', 'pa.text_value', 'pa.attribute_id', 'ps.attribute_id as attributeId')
->where('is_filterable', 1)
->WhereIn('pa.product_id', $productIds)
->get();
->distinct()
->leftJoin('product_attribute_values as pa', 'product_flat.product_id', 'pa.product_id')
->leftJoin('attributes as at', 'pa.attribute_id', 'at.id')
->leftJoin('product_super_attributes as ps', 'product_flat.product_id', 'ps.product_id')
->select('pa.integer_value', 'pa.text_value', 'pa.attribute_id', 'ps.attribute_id as attributeId')
->where('is_filterable', 1)
->WhereIn('pa.product_id', $productIds)
->get();
$attributeInfo['attributeOptions'] = $attributeInfo['attributes'] = [];
@ -70,18 +102,18 @@ class ProductFlatRepository extends Repository
$attributeKeys = array_keys($attribute->toArray());
foreach ($attributeKeys as $key) {
if (! is_null($attribute[$key])) {
if ($key == 'integer_value' && ! in_array($attribute[$key], $attributeInfo['attributeOptions'])) {
if (!is_null($attribute[$key])) {
if ($key == 'integer_value' && !in_array($attribute[$key], $attributeInfo['attributeOptions'])) {
array_push($attributeInfo['attributeOptions'], $attribute[$key]);
} else if ($key == 'text_value' && ! in_array($attribute[$key], $attributeInfo['attributeOptions'])) {
} else if ($key == 'text_value' && !in_array($attribute[$key], $attributeInfo['attributeOptions'])) {
$multiSelectArrributes = explode(",", $attribute[$key]);
foreach ($multiSelectArrributes as $multi) {
if (! in_array($multi, $attributeInfo['attributeOptions'])) {
if (!in_array($multi, $attributeInfo['attributeOptions'])) {
array_push($attributeInfo['attributeOptions'], $multi);
}
}
} else if (($key == 'attribute_id' || $key == 'attributeId') && ! in_array($attribute[$key], $attributeInfo['attributes'])) {
} else if (($key == 'attribute_id' || $key == 'attributeId') && !in_array($attribute[$key], $attributeInfo['attributes'])) {
array_push($attributeInfo['attributes'], $attribute[$key]);
}
}
@ -92,24 +124,9 @@ class ProductFlatRepository extends Repository
}
/**
* get Category Product Model
* Filter attributes according to products.
*
* @param int $categoryId
* @return \Illuminate\Support\Querybuilder
*/
public function categoryProductQuerybuilder($categoryId) {
return $this->model
->leftJoin('product_categories', 'product_flat.product_id', 'product_categories.product_id')
->where('product_categories.category_id', $categoryId)
->where('product_flat.channel', core()->getCurrentChannelCode())
->where('product_flat.locale', app()->getLocale());
}
/**
* filter attributes according to products
*
* @param array $category
* @param \Webkul\Category\Contracts\Category $category
* @return \Illuminate\Support\Collection
*/
public function getProductsRelatedFilterableAttributes($category)
@ -129,32 +146,78 @@ class ProductFlatRepository extends Repository
$allFilterableAttributes = array_filter(array_unique(array_intersect($categoryFilterableAttributes, $productCategoryArrributes['attributes'])));
$attributes = app('Webkul\Attribute\Repositories\AttributeRepository')->getModel()::with(['options' => function($query) use ($productCategoryArrributes) {
$attributes = $this->attributeRepository->getModel()::with([
'options' => function ($query) use ($productCategoryArrributes) {
return $query->whereIn('id', $productCategoryArrributes['attributeOptions'])
->orderBy('sort_order');
->orderBy('sort_order');
}
])->whereIn('id', $allFilterableAttributes)->get();
return $loadedCategoryAttributes[$category->id] = $attributes;
} else {
return $loadedCategoryAttributes[$category->id] = $category->filterableAttributes;
}
}
/**
* update product_flat custom column
* Maximum price of category product.
*
* @param \Webkul\Attribute\Models\Attribute $attribute
* @param \Webkul\Product\Listeners\ProductFlat $listener
* @param \Webkul\Category\Contracts\Category $category
* @return float
*/
public function updateAttributeColumn(
\Webkul\Attribute\Models\Attribute $attribute ,
\Webkul\Product\Listeners\ProductFlat $listener ) {
return $this->model
->leftJoin('product_attribute_values as v', function($join) use ($attribute) {
$join->on('product_flat.id', '=', 'v.product_id')
->on('v.attribute_id', '=', \DB::raw($attribute->id));
})->update(['product_flat.'.$attribute->code => \DB::raw($listener->attributeTypeFields[$attribute->type] .'_value')]);
public function getCategoryProductMaximumPrice($category = null)
{
static $loadedCategoryMaxPrice = [];
if (!$category) {
return $this->model->max('max_price');
}
if (array_key_exists($category->id, $loadedCategoryMaxPrice)) {
return $loadedCategoryMaxPrice[$category->id];
}
return $loadedCategoryMaxPrice[$category->id] = $this->model
->leftJoin('product_categories', 'product_flat.product_id', 'product_categories.product_id')
->where('product_categories.category_id', $category->id)
->max('max_price');
}
}
/**
* Get filter attributes.
*
* @param \Webkul\Category\Contracts\Category $category
* @return array
*/
public function getFilterAttributes($category)
{
$filterAttributes = [];
if (isset($category)) {
$filterAttributes = $this->getProductsRelatedFilterableAttributes($category);
}
if (!count($filterAttributes) > 0) {
$filterAttributes = $this->attributeRepository->getFilterAttributes();
}
return $filterAttributes;
}
/**
* Handle category product max price.
*
* @param \Webkul\Category\Contracts\Category $category
* @return float
*/
public function handleCategoryProductMaximumPrice($category)
{
$maxPrice = 0;
if (isset($category)) {
$maxPrice = core()->convertPrice($this->getCategoryProductMaximumPrice($category));
}
return $maxPrice;
}
}

View File

@ -1,49 +1,36 @@
@inject ('attributeRepository', 'Webkul\Attribute\Repositories\AttributeRepository')
@inject ('productFlatRepository', 'Webkul\Product\Repositories\ProductFlatRepository')
@inject ('productRepository', 'Webkul\Product\Repositories\ProductRepository')
<?php
$filterAttributes = $attributes = [];
$maxPrice = 0;
$filterAttributes = $productFlatRepository->getFilterAttributes($category);
if (isset($category)) {
$filterAttributes = $productFlatRepository->getProductsRelatedFilterableAttributes($category);
$maxPrice = core()->convertPrice($productFlatRepository->getCategoryProductMaximumPrice($category));
}
if (! count($filterAttributes) > 0) {
$filterAttributes = $attributeRepository->getFilterAttributes();
}
$maxPrice = $productFlatRepository->handleCategoryProductMaximumPrice($category);
?>
<div class="layered-filter-wrapper">
{!! view_render_event('bagisto.shop.products.list.layered-nagigation.before') !!}
<layered-navigation></layered-navigation>
<layered-navigation></layered-navigation>
{!! view_render_event('bagisto.shop.products.list.layered-nagigation.after') !!}
</div>
@push('scripts')
<script type="text/x-template" id="layered-navigation-template">
<div>
<div class="filter-title">
{{ __('shop::app.products.layered-nav-title') }}
</div>
<div class="filter-content">
<div class="filter-attributes">
<filter-attribute-item v-for='(attribute, index) in attributes' :attribute="attribute" :key="index" :index="index" @onFilterAdded="addFilters(attribute.code, $event)" :appliedFilterValues="appliedFilters[attribute.code]">
<filter-attribute-item
v-for='(attribute, index) in attributes'
:attribute="attribute"
:key="index"
:index="index"
@onFilterAdded="addFilters(attribute.code, $event)"
:appliedFilterValues="appliedFilters[attribute.code]">
</filter-attribute-item>
</div>
</div>
</div>
@ -51,7 +38,6 @@
<script type="text/x-template" id="filter-attribute-item-template">
<div class="filter-attributes-item" :class="[active ? 'active' : '']">
<div class="filter-attributes-title" @click="active = !active">
@{{ attribute.name ? attribute.name : attribute.admin_name }}
@ -65,16 +51,13 @@
</div>
<div class="filter-attributes-content">
<ol class="items" v-if="attribute.type != 'price'">
<li class="item" v-for='(option, index) in attribute.options'>
<span class="checkbox">
<input type="checkbox" :id="option.id" v-bind:value="option.id" v-model="appliedFilters" @change="addFilter($event)"/>
<label class="checkbox-view" :for="option.id"></label>
@{{ option.label ? option.label : option.admin_name }}
</span>
</li>
</ol>
@ -89,15 +72,12 @@
@change="priceRangeUpdated($event)"
></vue-slider>
</div>
</div>
</div>
</script>
<script>
Vue.component('layered-navigation', {
template: '#layered-navigation-template',
data: function() {
@ -109,12 +89,12 @@
},
created: function () {
var urlParams = new URLSearchParams(window.location.search);
let urlParams = new URLSearchParams(window.location.search);
var this_this = this;
let self = this;
urlParams.forEach(function (value, index) {
this_this.appliedFilters[index] = value.split(',');
self.appliedFilters[index] = value.split(',');
});
},
@ -130,7 +110,7 @@
},
applyFilter: function () {
var params = [];
let params = [];
for(key in this.appliedFilters) {
if (key != 'page') {
@ -144,7 +124,6 @@
});
Vue.component('filter-attribute-item', {
template: '#filter-attribute-item-template',
props: ['index', 'attribute', 'appliedFilterValues'],
@ -160,10 +139,7 @@
active: false,
sliderConfig: {
value: [
0,
0
],
value: [0, 0],
max: maxPrice,
processStyle: {
"backgroundColor": "#FF6472"
@ -177,8 +153,7 @@
},
created: function () {
if (!this.index)
this.active = true;
if (!this.index) this.active = true;
if (this.appliedFilterValues && this.appliedFilterValues.length) {
this.appliedFilters = this.appliedFilterValues;
@ -212,8 +187,6 @@
this.$emit('onFilterAdded', this.appliedFilters)
}
}
});
</script>
@endpush

View File

@ -1,30 +1,17 @@
@inject ('productRepository', 'Webkul\Product\Repositories\ProductRepository')
@inject ('attributeRepository', 'Webkul\Attribute\Repositories\AttributeRepository')
@inject ('productFlatRepository', 'Webkul\Product\Repositories\ProductFlatRepository')
<?php
$filterAttributes = $attributes = [];
$maxPrice = 0;
$filterAttributes = $productFlatRepository->getFilterAttributes($category);
if (isset($category)) {
$filterAttributes = $productFlatRepository->getProductsRelatedFilterableAttributes($category);
$maxPrice = core()->convertPrice($productFlatRepository->getCategoryProductMaximumPrice($category));
}
if (! count($filterAttributes) > 0) {
$filterAttributes = $attributeRepository->getFilterAttributes();
}
$maxPrice = $productFlatRepository->handleCategoryProductMaximumPrice($category);
?>
<div class="layered-filter-wrapper left">
{!! view_render_event('bagisto.shop.products.list.layered-nagigation.before') !!}
<layered-navigation></layered-navigation>
{!! view_render_event('bagisto.shop.products.list.layered-nagigation.after') !!}
</div>
@push('scripts')