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