| 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 |
'allow_out_of_stock' => false, // when false, show only in-stock products |
| 35 |
'min_price' => 0, |
| 36 |
'max_price' => 0, |
| 37 |
'on_sale' => false, |
| 38 |
'is_main_query' => false, |
| 39 |
'search' => '', |
| 40 |
'include_ids' => [], // array of product ids to include |
| 41 |
'exclude_ids' => [], // array of product ids to exclude |
| 42 |
'with' => ['detail', 'variants'], // relationships to load, |
| 43 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 44 |
'tax_query' => [], |
| 45 |
]; |
| 46 |
|
| 47 |
|
| 48 |
$this->queryArgs = wp_parse_args($args, $defaults); |
| 49 |
|
| 50 |
if (Arr::get($this->queryArgs, 'is_main_query')) { |
| 51 |
$pageType = TemplateService::getCurrentFcPageType(); |
| 52 |
if ($pageType == 'product_taxonomy') { |
| 53 |
$queried_object = get_queried_object(); |
| 54 |
if (!empty($queried_object->taxonomy) && is_tax(get_object_taxonomies('fluent-products'))) { |
| 55 |
$currentTermId = $queried_object->term_id; |
| 56 |
// get the childs |
| 57 |
$childTerms = get_terms([ |
| 58 |
'taxonomy' => $queried_object->taxonomy, |
| 59 |
'parent' => $currentTermId, |
| 60 |
'hide_empty' => false, |
| 61 |
]); |
| 62 |
$childTermIds = array_column($childTerms, 'term_id'); |
| 63 |
|
| 64 |
$termIds = array_merge([$currentTermId], $childTermIds); |
| 65 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 66 |
$this->queryArgs['tax_query'] = [ |
| 67 |
$queried_object->taxonomy => $termIds, |
| 68 |
]; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
$paged = get_query_var('paged') ? get_query_var('paged') : null; |
| 73 |
if ($paged) { |
| 74 |
$this->queryArgs['page'] = absint($paged) ? absint($paged) : 1; |
| 75 |
$this->queryArgs['paginate'] = 'simple'; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
public function getDefaultFilters() |
| 82 |
{ |
| 83 |
return array_filter([ |
| 84 |
'per_page' => $this->queryArgs['per_page'], |
| 85 |
'sort_by' => $this->queryArgs['sort_by'], |
| 86 |
'sort_type' => $this->queryArgs['sort_type'], |
| 87 |
'product_type' => $this->queryArgs['product_type'], |
| 88 |
'stock_status' => $this->queryArgs['stock_status'], |
| 89 |
'min_price' => $this->queryArgs['min_price'], |
| 90 |
'max_price' => $this->queryArgs['max_price'], |
| 91 |
'on_sale' => $this->queryArgs['on_sale'], |
| 92 |
'search' => $this->queryArgs['search'], |
| 93 |
'include_ids' => $this->queryArgs['include_ids'], |
| 94 |
'exclude_ids' => $this->queryArgs['exclude_ids'], |
| 95 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 96 |
'tax_query' => $this->queryArgs['tax_query'] |
| 97 |
]); |
| 98 |
} |
| 99 |
|
| 100 |
public function setDefultFilters($filters = []) |
| 101 |
{ |
| 102 |
foreach ($filters as $key => $value) { |
| 103 |
if (array_key_exists($key, $this->queryArgs)) { |
| 104 |
$this->queryArgs[$key] = $value; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return $this; |
| 109 |
} |
| 110 |
|
| 111 |
public function getParsedArgs() |
| 112 |
{ |
| 113 |
return $this->queryArgs; |
| 114 |
} |
| 115 |
|
| 116 |
public function getQuery() |
| 117 |
{ |
| 118 |
$query = Product::query() |
| 119 |
->where('post_status', $this->queryArgs['product_status']) |
| 120 |
->when($this->queryArgs['search'], function ($query, $search) { |
| 121 |
$query->where('post_title', 'like', '%' . $search . '%'); |
| 122 |
}) |
| 123 |
->when($this->queryArgs['include_ids'], function ($query, $includeIds) { |
| 124 |
$query->whereIn('ID', $includeIds); |
| 125 |
}) |
| 126 |
->when($this->queryArgs['exclude_ids'], function ($query, $excludeIds) { |
| 127 |
$query->whereNotIn('ID', $excludeIds); |
| 128 |
}) |
| 129 |
->when(!$this->queryArgs['allow_out_of_stock'], function ($query) { |
| 130 |
$query->whereHas('detail', function ($query) { |
| 131 |
$query->where('stock_availability', 'in-stock'); |
| 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 |
|