Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'rest_api_init' , 'wt_rest_api');
- function wt_rest_api(){
- register_rest_route('wtrest','products',array(
- 'methods' => WP_REST_SERVER::READABLE,
- 'callback' => 'wtProductResults'
- ));
- }
- function wtProductResults($data){
- $products = new WP_Query([
- 'post_type' => 'product',
- 'tax_query' => array(
- array(
- 'taxonomy' => 'product_cat',
- 'field' => 'term_id', //can be set to ID
- 'terms' => $data['cat'] //if field is ID you can reference by cat/term number
- )
- )
- ]);
- $productsResults = [];
- global $woocommerce;
- global $product;
- $currency = get_woocommerce_currency_symbol();
- while($products->have_posts()){
- $products->the_post();
- $product_cat = get_term( $data['cat'], 'product_cat', 'category', "OBJECT" );
- $regularPrice = get_post_meta( get_the_ID(), '_regular_price', true);
- $sale = get_post_meta( get_the_ID(), '_sale_price', true);
- $price = get_post_meta( get_the_ID(), '_price', true );
- array_push($productsResults , [
- 'title' => get_the_title(),
- 'productId' => get_the_id(),
- 'permalink' => get_the_permalink(),
- 'thumbnail' => get_the_post_thumbnail(),
- 'excerpt' => get_the_excerpt(),
- 'regularPrice' => $regularPrice,
- 'price' => $price,
- 'salePrice' => $sale,
- 'category' => $product_cat->name,
- 'isVariable' => is_type('variable'),
- 'variationPrice' => get_variation_prices()//**Here is My problem**
- ]);
- }
- wp_reset_postdata();
- return $productsResults;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement