PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.26
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.26
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / app / Modules / Data / ProductQuery.php

ProductQuery.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.26, at app/Modules/Data/ProductQuery.php

230 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Modules\Data;
4
5 use FluentCart\App\Models\Product;
6 use FluentCart\App\Models\WpModels\Term;
7 use FluentCart\App\Services\TemplateService;
8 use FluentCart\Framework\Support\Arr;
9
10 class ProductQuery
11 {
12 private array $queryArgs = [];
13
14 public function __construct($args = [])
15 {
16 //example tax query
17 // $taxQuery = [
18 // 'product-categories' => [1,2],
19 //
20 // ];
21
22 $perPage = get_option('posts_per_page');
23
24 $defaults = [
25 'product_status' => 'publish',
26 'per_page' => $perPage,
27 'sort_by' => 'id', // id|date|title|price
28 'sort_type' => 'desc', // asc|desc
29 'paginate' => 'simple', //simple|cursor|false
30 'page' => 1,
31 'cursor' => null,
32 'product_type' => '', // physical|digital|subscription|onetime|simple|variations
33 'stock_status' => '', // in_stock|out_of_stock // check only if global stock management is enabled
34 'min_price' => 0,
35 'max_price' => 0,
36 'on_sale' => false,
37 'is_main_query' => false,
38 'search' => '',
39 'include_ids' => [], // array of product ids to include
40 'exclude_ids' => [], // array of product ids to exclude
41 'with' => ['detail', 'variants'], // relationships to load,
42 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
43 'tax_query' => [],
44 ];
45
46
47 $this->queryArgs = wp_parse_args($args, $defaults);
48
49 if (Arr::get($this->queryArgs, 'is_main_query')) {
50 $pageType = TemplateService::getCurrentFcPageType();
51 if ($pageType == 'product_taxonomy') {
52 $queried_object = get_queried_object();
53 if (!empty($queried_object->taxonomy) && is_tax(get_object_taxonomies('fluent-products'))) {
54 $currentTermId = $queried_object->term_id;
55 // get the childs
56 $childTerms = get_terms([
57 'taxonomy' => $queried_object->taxonomy,
58 'parent' => $currentTermId,
59 'hide_empty' => false,
60 ]);
61 $childTermIds = array_column($childTerms, 'term_id');
62
63 $termIds = array_merge([$currentTermId], $childTermIds);
64 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
65 $this->queryArgs['tax_query'] = [
66 $queried_object->taxonomy => $termIds,
67 ];
68 }
69 }
70
71 $paged = get_query_var('paged') ? get_query_var('paged') : null;
72 if ($paged) {
73 $this->queryArgs['page'] = absint($paged) ? absint($paged) : 1;
74 $this->queryArgs['paginate'] = 'simple';
75 }
76 }
77
78 }
79
80 public function getDefaultFilters()
81 {
82 return array_filter([
83 'per_page' => $this->queryArgs['per_page'],
84 'sort_by' => $this->queryArgs['sort_by'],
85 'sort_type' => $this->queryArgs['sort_type'],
86 'product_type' => $this->queryArgs['product_type'],
87 'stock_status' => $this->queryArgs['stock_status'],
88 'min_price' => $this->queryArgs['min_price'],
89 'max_price' => $this->queryArgs['max_price'],
90 'on_sale' => $this->queryArgs['on_sale'],
91 'search' => $this->queryArgs['search'],
92 'include_ids' => $this->queryArgs['include_ids'],
93 'exclude_ids' => $this->queryArgs['exclude_ids'],
94 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
95 'tax_query' => $this->queryArgs['tax_query']
96 ]);
97 }
98
99 public function setDefultFilters($filters = [])
100 {
101 foreach ($filters as $key => $value) {
102 if (array_key_exists($key, $this->queryArgs)) {
103 $this->queryArgs[$key] = $value;
104 }
105 }
106
107 return $this;
108 }
109
110 public function getParsedArgs()
111 {
112 return $this->queryArgs;
113 }
114
115 public function getQuery()
116 {
117 $query = Product::query()
118 ->where('post_status', $this->queryArgs['product_status'])
119 ->when($this->queryArgs['search'], function ($query, $search) {
120 $query->where('post_title', 'like', '%' . $search . '%');
121 })
122 ->when($this->queryArgs['include_ids'], function ($query, $includeIds) {
123 $query->whereIn('ID', $includeIds);
124 })
125 ->when($this->queryArgs['exclude_ids'], function ($query, $excludeIds) {
126 $query->whereNotIn('ID', $excludeIds);
127 })
128 //TODO: check if stock management module is on
129 ->when($this->queryArgs['stock_status'], function ($query, $stockStatus) {
130 $query->whereHas('detail', function ($query) use ($stockStatus) {
131 $query->where('stock_availability', $stockStatus);
132 });
133 })
134 ->when($this->queryArgs['on_sale'], function ($query) {
135 $query->whereHas('variants', function ($query) {
136 $query->where('item_price', '<', 'compare_price');
137 });
138 })
139 ->with($this->queryArgs['with']);
140
141 $query = $query->applyCustomSortBy(
142 $this->queryArgs['sort_by'],
143 $this->queryArgs['sort_type']
144 )
145 ->byVariantTypes(
146 $this->queryArgs['product_type'],
147 )->filterByTaxonomy(
148 $this->queryArgs['tax_query']
149 );
150
151 return $query;
152
153
154 }
155
156 public function get()
157 {
158 $paginate = $this->queryArgs['paginate'];
159 $query = $this->getQuery();
160
161 if (!$paginate) {
162 return $query->take(
163 $this->queryArgs['per_page']
164 )->get();
165 }
166
167 if ($paginate === 'cursor') {
168 return $query->cursorPaginate(
169 $this->queryArgs['per_page'],
170 ['*'],
171 'cursor',
172 $this->queryArgs['cursor']
173 );
174 }
175
176 return $query->paginate(
177 $this->queryArgs['per_page'],
178 ['*'],
179 'page',
180 $this->queryArgs['page']
181 );
182 }
183
184 public function getConnectedTerms($types = [], $formatted = true)
185 {
186 $productQuery = $this->getQuery();
187
188 $terms = Term::query()->whereHas('taxonomy', function ($query) use ($types, $productQuery) {
189 if (!empty($types)) {
190 $query->whereIn('taxonomy', (array)$types);
191 }
192 $query->whereHas('termRelationships', function ($query) use ($productQuery) {
193 $query->whereIn('object_id', $productQuery->select('ID'));
194 });
195 })
196 ->with(['taxonomy'])
197 ->get();
198
199 if ($formatted) {
200 $formattedTerms = [];
201
202 foreach ($terms as $term) {
203 $taxonomy = $term->taxonomy;
204 if ($taxonomy) {
205 $taxonomyName = $taxonomy->taxonomy;
206 if (!isset($formattedTerms[$taxonomyName])) {
207 $formattedTerms[$taxonomyName] = [
208 'taxonomy' => $taxonomyName,
209 'terms' => []
210 ];
211 }
212
213 $formattedTerms[$taxonomyName]['terms'][] = [
214 'term_id' => $term->term_id,
215 'name' => $term->name,
216 'slug' => $term->slug,
217 'parent' => $taxonomy->parent,
218 'description' => $term->description,
219 ];
220 }
221 }
222
223 return $formattedTerms;
224 }
225
226
227 return $terms;
228 }
229 }
230