| 1 |
<?php |
| 2 |
namespace Depicter\DataSources; |
| 3 |
|
| 4 |
use Averta\Core\Utility\Data; |
| 5 |
use Averta\WordPress\Utility\JSON; |
| 6 |
use Averta\Core\Utility\Str; |
| 7 |
class Products extends Posts implements DataSourceInterface { |
| 8 |
|
| 9 |
/** |
| 10 |
* DataSource name |
| 11 |
* |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
protected $type = 'wooProducts'; |
| 15 |
|
| 16 |
/** |
| 17 |
* DataSource properties |
| 18 |
* |
| 19 |
* @var array |
| 20 |
*/ |
| 21 |
protected $properties = [ |
| 22 |
'type' => 'wooProducts', |
| 23 |
'postType' => 'product' |
| 24 |
]; |
| 25 |
|
| 26 |
/** |
| 27 |
* Default input params for retrieving dataSource records |
| 28 |
* |
| 29 |
* @var array |
| 30 |
*/ |
| 31 |
protected $defaultInputParams = [ |
| 32 |
'postType' => 'product', |
| 33 |
'perpage' => 5, |
| 34 |
'excerptLength' => 100, |
| 35 |
'offset' => 0, |
| 36 |
'linkSlides' => true, |
| 37 |
'orderBy' => 'date', |
| 38 |
'order' => 'DESC', |
| 39 |
'imageSource' => 'featured', |
| 40 |
'excludedIds' => '', |
| 41 |
'includedIds' => '', |
| 42 |
'excludeNonThumbnail' => true, |
| 43 |
'taxonomies' => '', |
| 44 |
'from' => 'all', // product types - available options => all, best-selling, top-rated, featured, on-sale |
| 45 |
'inStockOnly' => true, |
| 46 |
'regularProducts' => true, |
| 47 |
'downloadableProducts' => true, |
| 48 |
'virtualProducts' => true, |
| 49 |
'filterByPrice' => false, |
| 50 |
'startPrice' => '', |
| 51 |
'endPrice' => '', |
| 52 |
'startSalePrice' => '', |
| 53 |
'endSalePrice' => '' |
| 54 |
]; |
| 55 |
|
| 56 |
/** |
| 57 |
* Asset groups of this DataSource |
| 58 |
* |
| 59 |
* @var array |
| 60 |
*/ |
| 61 |
protected $assetGroupNames = [ 'post', 'product', 'taxonomy', 'acf', 'metaboxio' ]; |
| 62 |
|
| 63 |
/** |
| 64 |
* Retrieves the list of records based on query params |
| 65 |
* |
| 66 |
* @param $args |
| 67 |
* |
| 68 |
* @return \WP_Query |
| 69 |
*/ |
| 70 |
protected function getRecords( $args ){ |
| 71 |
|
| 72 |
$queryArgs = [ |
| 73 |
'post_type' => $args['postType'], |
| 74 |
'posts_per_page' => $args['perpage'], |
| 75 |
'order' => $args['order'], |
| 76 |
'orderby' => $args['orderBy'], |
| 77 |
'offset' => $args['offset'], |
| 78 |
'post__in' => $args['includedIds'], |
| 79 |
'post__not_in' => $args['excludedIds'], |
| 80 |
'tax_query' => [], |
| 81 |
'meta_query' => [] |
| 82 |
]; |
| 83 |
|
| 84 |
if ( !empty( $args['taxonomies'] ) ) { |
| 85 |
$taxonomies = $args['taxonomies']; |
| 86 |
|
| 87 |
if( JSON::isJson( $args['taxonomies'] ) ){ |
| 88 |
$taxonomies = JSON::decode( $args['taxonomies'] ); |
| 89 |
} |
| 90 |
|
| 91 |
if( !empty( $taxonomies->product_cat ) ){ |
| 92 |
$queryArgs['tax_query'][] = [ |
| 93 |
'taxonomy' => 'product_cat', |
| 94 |
'field' => 'slug', |
| 95 |
'terms' => $taxonomies->product_cat |
| 96 |
]; |
| 97 |
} |
| 98 |
|
| 99 |
if( !empty( $taxonomies->product_tag ) ){ |
| 100 |
$queryArgs['tax_query'][] = [ |
| 101 |
'taxonomy' => 'product_tag', |
| 102 |
'field' => 'slug', |
| 103 |
'terms' => $taxonomies->product_tag |
| 104 |
]; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if ( !empty( $args['from'] ) ) { |
| 109 |
switch ( $args['from'] ) { |
| 110 |
case 'featured': |
| 111 |
$queryArgs['tax_query'][] = array( |
| 112 |
'taxonomy' => 'product_visibility', |
| 113 |
'field' => 'name', |
| 114 |
'terms' => 'featured', |
| 115 |
); |
| 116 |
break; |
| 117 |
|
| 118 |
case 'best-selling': |
| 119 |
$queryArgs['orderby'] = 'meta_value_num'; |
| 120 |
break; |
| 121 |
|
| 122 |
case 'on-sale': |
| 123 |
$queryArgs['meta_query'] = WC()->query->get_meta_query(); |
| 124 |
$queryArgs['post__in'] = wc_get_product_ids_on_sale(); |
| 125 |
break; |
| 126 |
|
| 127 |
case 'top-rated': |
| 128 |
$queryArgs['meta_query'] = WC()->query->get_meta_query(); |
| 129 |
add_filter( 'posts_clauses', array( 'WC_Shortcodes', 'order_by_rating_post_clauses' ) ); |
| 130 |
break; |
| 131 |
default: // i.e. all products |
| 132 |
$queryArgs['meta_query'] = WC()->query->get_meta_query(); |
| 133 |
break; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if( Data::isTrue( $args['excludeNonThumbnail'] ) ){ |
| 138 |
$queryArgs['meta_query'][] = [ |
| 139 |
'key' => '_thumbnail_id', |
| 140 |
'compare' => 'EXISTS' |
| 141 |
]; |
| 142 |
} |
| 143 |
|
| 144 |
if ( Data::isTrue( $args['inStockOnly'] ) ) { |
| 145 |
$queryArgs['meta_query'][] = [ |
| 146 |
'key' => '_stock_status', |
| 147 |
'value' => 'instock', |
| 148 |
'compare' => '=', |
| 149 |
]; |
| 150 |
} |
| 151 |
|
| 152 |
if ( ! Data::isTrue( $args['regularProducts'] ) ) { |
| 153 |
if ( ! Data::isTrue( $args['downloadableProducts'] ) && ! Data::isTrue( $args['virtualProducts'] ) ) { |
| 154 |
return new \WP_Query(); |
| 155 |
} |
| 156 |
|
| 157 |
if ( Data::isTrue( $args['downloadableProducts'] ) ) { |
| 158 |
|
| 159 |
if ( Data::isTrue( $args['virtualProducts'] ) ) { |
| 160 |
$queryArgs['meta_query'][] = [ |
| 161 |
'relation' => 'OR', |
| 162 |
[ |
| 163 |
'key' => '_downloadable', |
| 164 |
'value' => 'yes', |
| 165 |
'compare' => '==', |
| 166 |
], |
| 167 |
[ |
| 168 |
'key' => '_virtual', |
| 169 |
'value' => 'yes', |
| 170 |
'compare' => '==', |
| 171 |
] |
| 172 |
|
| 173 |
]; |
| 174 |
} else { |
| 175 |
$queryArgs['meta_query'][] = [ |
| 176 |
'key' => '_downloadable', |
| 177 |
'value' => 'yes', |
| 178 |
'compare' => '==', |
| 179 |
]; |
| 180 |
} |
| 181 |
|
| 182 |
} else if ( Data::isTrue( $args['virtualProducts'] ) ) { |
| 183 |
$queryArgs['meta_query'][] = [ |
| 184 |
'key' => '_virtual', |
| 185 |
'value' => 'yes', |
| 186 |
'compare' => '==', |
| 187 |
]; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
} else { |
| 193 |
if ( ! Data::isTrue( $args['downloadableProducts'] ) ) { |
| 194 |
$queryArgs['meta_query'][] = [ |
| 195 |
'key' => '_downloadable', |
| 196 |
'value' => 'yes', |
| 197 |
'compare' => '!=', |
| 198 |
]; |
| 199 |
} |
| 200 |
|
| 201 |
if ( ! Data::isTrue( $args['virtualProducts'] ) ) { |
| 202 |
$queryArgs['meta_query'][] = [ |
| 203 |
'key' => '_virtual', |
| 204 |
'value' => 'yes', |
| 205 |
'compare' => '!=', |
| 206 |
]; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if ( Data::isTrue( $args['filterByPrice'] ) ) { |
| 211 |
|
| 212 |
if ( !empty( $args['startPrice'] ) ) { |
| 213 |
$queryArgs['meta_query'][] = [ |
| 214 |
'key' => '_price', |
| 215 |
'value' => $args['startPrice'], |
| 216 |
'compare' => '>=', |
| 217 |
'type' => 'NUMERIC', |
| 218 |
]; |
| 219 |
} |
| 220 |
|
| 221 |
if ( !empty( $args['endPrice'] ) ) { |
| 222 |
$queryArgs['meta_query'][] = [ |
| 223 |
'key' => '_price', |
| 224 |
'value' => $args['endPrice'], |
| 225 |
'compare' => '<=', |
| 226 |
'type' => 'NUMERIC', |
| 227 |
]; |
| 228 |
} |
| 229 |
|
| 230 |
if ( !empty( $args['startSalePrice'] ) ) { |
| 231 |
$queryArgs['meta_query'][] = [ |
| 232 |
'key' => '_sale_price', |
| 233 |
'value' => $args['startSalePrice'], |
| 234 |
'compare' => '>=', |
| 235 |
'type' => 'NUMERIC', |
| 236 |
]; |
| 237 |
} |
| 238 |
|
| 239 |
if ( !empty( $args['endSalePrice'] ) ) { |
| 240 |
$queryArgs['meta_query'][] = [ |
| 241 |
'key' => '_sale_price', |
| 242 |
'value' => $args['endSalePrice'], |
| 243 |
'compare' => '<=', |
| 244 |
'type' => 'NUMERIC', |
| 245 |
]; |
| 246 |
} |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
return new \WP_Query( $queryArgs ); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Renders preview for query params |
| 255 |
* |
| 256 |
* @param array $args |
| 257 |
* |
| 258 |
* @return array |
| 259 |
*/ |
| 260 |
public function previewRecords( array $args = [] ) { |
| 261 |
$args = $this->prepare( $args ); |
| 262 |
$availableRecords = $this->getRecords( $args ); |
| 263 |
|
| 264 |
$records = []; |
| 265 |
|
| 266 |
if ( $availableRecords && $availableRecords->have_posts() ) { |
| 267 |
|
| 268 |
foreach( $availableRecords->posts as $post ) { |
| 269 |
$product = wc_get_product( $post->ID ); |
| 270 |
if ( ! $product ) { |
| 271 |
continue; |
| 272 |
} |
| 273 |
|
| 274 |
$featuredImage = []; |
| 275 |
|
| 276 |
if( $featuredImageId = $product->get_image_id() ){ |
| 277 |
$imageInfo = wp_get_attachment_image_src( $featuredImageId, 'full' ); |
| 278 |
$featuredImage = [ |
| 279 |
'id' => $featuredImageId, |
| 280 |
'url' => $imageInfo[0] ?? '', |
| 281 |
'width' => $imageInfo[1] ?? '', |
| 282 |
'height' => $imageInfo[2] ?? '', |
| 283 |
]; |
| 284 |
} |
| 285 |
|
| 286 |
$secondaryImage = []; |
| 287 |
$attachment_ids = $product->get_gallery_image_ids(); |
| 288 |
if( !empty( $attachment_ids[0] ) ){ |
| 289 |
$imageInfo = wp_get_attachment_image_src( $attachment_ids[0], 'full' ); |
| 290 |
$secondaryImage = [ |
| 291 |
'id' => $attachment_ids[0], |
| 292 |
'url' => $imageInfo[0] ?? '', |
| 293 |
'width' => $imageInfo[1] ?? '', |
| 294 |
'height' => $imageInfo[2] ?? '', |
| 295 |
]; |
| 296 |
} |
| 297 |
|
| 298 |
$excerpt = !empty( $args['excerptLength'] ) ? Str::trimByChars( get_the_excerpt( $post->ID ), $args['excerptLength'] ) : get_the_excerpt( $post->ID ); |
| 299 |
$postInfo = [ |
| 300 |
'id' => $post->ID, |
| 301 |
'title' => $product->get_title(), |
| 302 |
'url' => get_permalink( $post->ID ), |
| 303 |
'featuredImage' => $featuredImage, |
| 304 |
'secondaryImage' => $secondaryImage, |
| 305 |
'date' => get_the_date('', $post->ID ), |
| 306 |
'excerpt' => $excerpt, |
| 307 |
'content' => $product->get_description(), |
| 308 |
'taxonomies'=> [], |
| 309 |
'shortDescription' => $product->get_short_description(), |
| 310 |
'price' => wc_price( $product->get_regular_price() ) . $product->get_price_suffix(), |
| 311 |
'salePrice' => $product->is_on_sale() ? wc_price( $product->get_sale_price() ) . $product->get_price_suffix() : '', |
| 312 |
'onSale'=> $product->is_on_sale(), |
| 313 |
'ratingAverage' => $product->get_average_rating(), |
| 314 |
'ratingCount' => $product->get_rating_count(), |
| 315 |
'reviewCount' => $product->get_review_count(), |
| 316 |
'sku' => $product->get_sku(), |
| 317 |
'stockStatus' => $this->getStockStatus( $product ), |
| 318 |
'stockStatusClass' => $product->is_in_stock() ? 'in-stock' : 'out-of-stock', |
| 319 |
'isInStock' => $product->is_in_stock(), |
| 320 |
'stockQuantity'=> $product->get_stock_quantity() |
| 321 |
|
| 322 |
]; |
| 323 |
|
| 324 |
$taxonomies = get_object_taxonomies( $args['postType'], 'objects' ); |
| 325 |
|
| 326 |
if ( !empty( $taxonomies ) ) { |
| 327 |
foreach( $taxonomies as $taxonomySlug => $taxonomy ) { |
| 328 |
$taxonomyInfo = [ |
| 329 |
"id" => $taxonomySlug, |
| 330 |
"label" => $taxonomy->label, |
| 331 |
"terms" => [] |
| 332 |
]; |
| 333 |
|
| 334 |
if ( $terms = wp_get_post_terms( $post->ID, $taxonomySlug ) ) { |
| 335 |
foreach( $terms as $term ) { |
| 336 |
$taxonomyInfo[ "terms" ][] = [ |
| 337 |
'id' => $term->term_id, |
| 338 |
'value' => $term->slug, |
| 339 |
'label' => $term->name, |
| 340 |
'link' => get_term_link( $term->term_id ) |
| 341 |
]; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
$postInfo['taxonomies'][] = $taxonomyInfo; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
$records[] = $postInfo; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
return $records; |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Retrieves product stock status |
| 358 |
* |
| 359 |
* @param $product |
| 360 |
* |
| 361 |
* @return mixed|void |
| 362 |
*/ |
| 363 |
protected function getStockStatus( $product ){ |
| 364 |
if ( $product->is_on_backorder() ) { |
| 365 |
$stock_html = __( 'On backorder', 'depicter' ); |
| 366 |
} elseif ( $product->is_in_stock() ) { |
| 367 |
$stock_html = __( 'In stock', 'depicter' ); |
| 368 |
} else { |
| 369 |
$stock_html = __( 'Out of stock', 'depicter' ); |
| 370 |
} |
| 371 |
return apply_filters( 'woocommerce_admin_stock_html', $stock_html, $product ); |
| 372 |
} |
| 373 |
} |
| 374 |
|