Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. add_action( 'rest_api_init' , 'wt_rest_api');
  2. function wt_rest_api(){
  3. register_rest_route('wtrest','products',array(
  4. 'methods' => WP_REST_SERVER::READABLE,
  5. 'callback' => 'wtProductResults'
  6. ));
  7. }
  8.  
  9. function wtProductResults($data){
  10. $products = new WP_Query([
  11. 'post_type' => 'product',
  12. 'tax_query' => array(
  13. array(
  14. 'taxonomy' => 'product_cat',
  15. 'field' => 'term_id', //can be set to ID
  16. 'terms' => $data['cat'] //if field is ID you can reference by cat/term number
  17. )
  18. )
  19. ]);
  20.  
  21. $productsResults = [];
  22. global $woocommerce;
  23. global $product;
  24. $currency = get_woocommerce_currency_symbol();
  25.  
  26. while($products->have_posts()){
  27. $products->the_post();
  28. $product_cat = get_term( $data['cat'], 'product_cat', 'category', "OBJECT" );
  29. $regularPrice = get_post_meta( get_the_ID(), '_regular_price', true);
  30. $sale = get_post_meta( get_the_ID(), '_sale_price', true);
  31. $price = get_post_meta( get_the_ID(), '_price', true );
  32. array_push($productsResults , [
  33. 'title' => get_the_title(),
  34. 'productId' => get_the_id(),
  35. 'permalink' => get_the_permalink(),
  36. 'thumbnail' => get_the_post_thumbnail(),
  37. 'excerpt' => get_the_excerpt(),
  38. 'regularPrice' => $regularPrice,
  39. 'price' => $price,
  40. 'salePrice' => $sale,
  41. 'category' => $product_cat->name,
  42. 'isVariable' => is_type('variable'),
  43. 'variationPrice' => get_variation_prices()//**Here is My problem**
  44. ]);
  45. }
  46. wp_reset_postdata();
  47. return $productsResults;
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement