| @@ -3,23 +3,33 @@ | ||
| 3 | 3 | |
| 4 | 4 | use Averta\Core\Utility\Data; |
| 5 | 5 | use Averta\WordPress\Utility\JSON; |
| 6 | 6 | use Averta\Core\Utility\Str; |
| 7 | -class Products extends Posts { | |
| 7 | +class Products extends Posts implements DataSourceInterface { | |
| 8 | 8 | |
| 9 | - /** | |
| 10 | - * Source name | |
| 9 | + /** | |
| 10 | + * DataSource name | |
| 11 | 11 | * |
| 12 | 12 | * @var string |
| 13 | 13 | */ |
| 14 | 14 | protected $type = 'wooProducts'; |
| 15 | 15 | |
| 16 | - /** | |
| 17 | - * Input params to filter source result | |
| 16 | + /** | |
| 17 | + * DataSource properties | |
| 18 | 18 | * |
| 19 | 19 | * @var array |
| 20 | 20 | */ |
| 21 | - protected $params = [ | |
| 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 = [ | |
| 22 | 32 | 'postType' => 'product', |
| 23 | 33 | 'perpage' => 5, |
| 24 | 34 | 'excerptLength' => 100, |
| 25 | 35 | 'offset' => 0, |
| @@ -43,27 +53,13 @@ | ||
| 43 | 53 | 'endSalePrice' => '' |
| 44 | 54 | ]; |
| 45 | 55 | |
| 46 | 56 | /** |
| 47 | - * List of valid dynamic tags for this source | |
| 57 | + * Asset groups of this DataSource | |
| 48 | 58 | * |
| 49 | - * @var string[] | |
| 59 | + * @var array | |
| 50 | 60 | */ |
| 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 | - ]; | |
| 61 | + protected $assetGroupNames = [ 'post', 'product', 'taxonomy', 'acf', 'metaboxio' ]; | |
| 66 | 62 | |
| 67 | 63 | /** |
| 68 | 64 | * Retrieves the list of records based on query params |
| 69 | 65 | * |
| @@ -152,24 +148,66 @@ | ||
| 152 | 148 | 'compare' => '=', |
| 153 | 149 | ]; |
| 154 | 150 | } |
| 155 | 151 | |
| 156 | - if ( ! Data::isTrue( $args['downloadableProducts'] ) ) { | |
| 157 | - $queryArgs['meta_query'][] = [ | |
| 158 | - 'key' => '_downloadable', | |
| 159 | - 'value' => 'yes', | |
| 160 | - 'compare' => '!=', | |
| 161 | - ]; | |
| 162 | - } | |
| 152 | + if ( ! Data::isTrue( $args['regularProducts'] ) ) { | |
| 153 | + if ( ! Data::isTrue( $args['downloadableProducts'] ) && ! Data::isTrue( $args['virtualProducts'] ) ) { | |
| 154 | + return new \WP_Query(); | |
| 155 | + } | |
| 163 | 156 | |
| 164 | - if ( ! Data::isTrue( $args['virtualProducts'] ) ) { | |
| 165 | - $queryArgs['meta_query'][] = [ | |
| 166 | - 'key' => '_virtual', | |
| 167 | - 'value' => 'yes', | |
| 168 | - 'compare' => '!=', | |
| 169 | - ]; | |
| 170 | - } | |
| 157 | + if ( Data::isTrue( $args['downloadableProducts'] ) ) { | |
| 171 | 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 | + | |
| 172 | 210 | if ( Data::isTrue( $args['filterByPrice'] ) ) { |
| 173 | 211 | |
| 174 | 212 | if ( !empty( $args['startPrice'] ) ) { |
| 175 | 213 | $queryArgs['meta_query'][] = [ |
| @@ -211,9 +249,9 @@ | ||
| 211 | 249 | |
| 212 | 250 | return new \WP_Query( $queryArgs ); |
| 213 | 251 | } |
| 214 | 252 | |
| 215 | - /** | |
| 253 | + /** | |
| 216 | 254 | * Renders preview for query params |
| 217 | 255 | * |
| 218 | 256 | * @param array $args |
| 219 | 257 | * |
| @@ -238,9 +276,9 @@ | ||
| 238 | 276 | if( $featuredImageId = $product->get_image_id() ){ |
| 239 | 277 | $imageInfo = wp_get_attachment_image_src( $featuredImageId, 'full' ); |
| 240 | 278 | $featuredImage = [ |
| 241 | 279 | 'id' => $featuredImageId, |
| 242 | - 'url' => $imageInfo[0] ?? '', | |
| 280 | + 'src' => $imageInfo[0] ?? '', | |
| 243 | 281 | 'width' => $imageInfo[1] ?? '', |
| 244 | 282 | 'height' => $imageInfo[2] ?? '', |
| 245 | 283 | ]; |
| 246 | 284 | } |
| @@ -250,9 +288,9 @@ | ||
| 250 | 288 | if( !empty( $attachment_ids[0] ) ){ |
| 251 | 289 | $imageInfo = wp_get_attachment_image_src( $attachment_ids[0], 'full' ); |
| 252 | 290 | $secondaryImage = [ |
| 253 | 291 | 'id' => $attachment_ids[0], |
| 254 | - 'url' => $imageInfo[0] ?? '', | |
| 292 | + 'src' => $imageInfo[0] ?? '', | |
| 255 | 293 | 'width' => $imageInfo[1] ?? '', |
| 256 | 294 | 'height' => $imageInfo[2] ?? '', |
| 257 | 295 | ]; |
| 258 | 296 | } |
| @@ -265,21 +303,27 @@ | ||
| 265 | 303 | 'featuredImage' => $featuredImage, |
| 266 | 304 | 'secondaryImage' => $secondaryImage, |
| 267 | 305 | 'date' => get_the_date('', $post->ID ), |
| 268 | 306 | 'excerpt' => $excerpt, |
| 307 | + 'author' => [ | |
| 308 | + 'name' => get_the_author_meta( 'display_name', $post->post_author ), | |
| 309 | + 'page' => get_author_posts_url( $post->post_author ), | |
| 310 | + ], | |
| 269 | 311 | 'content' => $product->get_description(), |
| 270 | - 'taxonomies'=> [], | |
| 312 | + 'taxonomy'=> [], | |
| 271 | 313 | 'shortDescription' => $product->get_short_description(), |
| 272 | - 'price' => wc_price( $product->get_regular_price() ) . $product->get_price_suffix(), | |
| 314 | + 'price' => wc_price( $product->get_price() ) . $product->get_price_suffix(), | |
| 315 | + 'regularPrice' => wc_price( $product->get_regular_price() ) . $product->get_price_suffix(), | |
| 273 | 316 | '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(), | |
| 317 | + 'onSale' => $product->is_on_sale(), | |
| 318 | + 'rating' => $product->get_average_rating(), | |
| 276 | 319 | 'ratingCount' => $product->get_rating_count(), |
| 277 | 320 | 'reviewCount' => $product->get_review_count(), |
| 278 | 321 | 'sku' => $product->get_sku(), |
| 279 | - 'stockStatus' => $this->getStockStatus( $product ), | |
| 280 | - 'stockQuantity' => $product->get_stock_quantity() | |
| 281 | - | |
| 322 | + 'stockStatus' => $this->getStockStatus( $product ), | |
| 323 | + 'stockStatusClass' => $product->is_in_stock() ? 'in-stock' : 'out-of-stock', | |
| 324 | + 'isInStock' => $product->is_in_stock(), | |
| 325 | + 'stockQuantity'=> $product->get_stock_quantity() | |
| 282 | 326 | ]; |
| 283 | 327 | |
| 284 | 328 | $taxonomies = get_object_taxonomies( $args['postType'], 'objects' ); |
| 285 | 329 | |
| @@ -301,9 +345,9 @@ | ||
| 301 | 345 | ]; |
| 302 | 346 | } |
| 303 | 347 | } |
| 304 | 348 | |
| 305 | - $postInfo['taxonomies'][] = $taxonomyInfo; | |
| 349 | + $postInfo['taxonomy'][] = $taxonomyInfo; | |
| 306 | 350 | } |
| 307 | 351 | } |
| 308 | 352 | |
| 309 | 353 | $records[] = $postInfo; |
| @@ -311,58 +355,8 @@ | ||
| 311 | 355 | } |
| 312 | 356 | |
| 313 | 357 | return $records; |
| 314 | 358 | } |
| 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 | 359 | |
| 366 | 360 | /** |
| 367 | 361 | * Retrieves product stock status |
| 368 | 362 | * |