Title: rest api option?
Last modified: March 28, 2024

---

# rest api option?

 *  [quangsangzs](https://wordpress.org/support/users/quangsangzs/)
 * (@quangsangzs)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/rest-api-option/)
 * I want to add product options to this api but can’t get it?
 *     ```wp-block-code
       function get_store_products_by_category($data) {
       $store_id = $data['store_id'];
       $category_id = isset($data['category_id']) ? $data['category_id'] : '';
       // Thực hiện truy vấn để lấy danh sách sản phẩm của cửa hàng dựa trên danh mục sản phẩm nếu có, nếu không lấy tất cả sản phẩm.
       $args = array(
           'post_type' => 'product',
           'posts_per_page' => -1,
           'orderby' => 'title', // Sắp xếp theo tên sản phẩm
           'order' => 'ASC', // Sắp xếp theo thứ tự tăng dần (A-Z)
           'author' => $store_id, // ID của cửa hàng
       );
   
       if (!empty($category_id)) {
           $args['tax_query'] = array(
               array(
                   'taxonomy' => 'product_cat',
                   'field' => 'term_id',
                   'terms' => $category_id,
               ),
           );
       }
   
       $query = new WP_Query($args);
       $products = $query->posts;
   
       // Chuẩn bị dữ liệu trả về
       $response = array();
       foreach ($products as $product) {
           $product_data = wc_get_product($product->ID); // Lấy đối tượng sản phẩm hoàn chỉnh của sản phẩm
   
           // Lấy thông tin cơ bản của sản phẩm
           $product_info = array(
               'id' => $product_data->get_id(),
               'name' => $product_data->get_name(),
               'permalink' => $product_data->get_permalink(),
               'price' => $product_data->get_price(),
               'categories' => wp_get_post_terms($product_data->get_id(), 'product_cat', array('fields' => 'names')),
               'image' => $product_data->get_image(),
           );
   
   
   
           // Thêm thông tin sản phẩm vào mảng response
           $response[] = $product_info;
       }
   
       // Trả về kết quả dưới dạng JSON
       return rest_ensure_response($response);
       ```
   
 * }
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Frest-api-option%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

The topic ‘rest api option?’ is closed to new replies.

 * ![](https://ps.w.org/wpc-product-options/assets/icon-128x128.png?rev=2767166)
 * [WPC Product Options for WooCommerce](https://wordpress.org/plugins/wpc-product-options/)
 * [Support Threads](https://wordpress.org/support/plugin/wpc-product-options/)
 * [Active Topics](https://wordpress.org/support/plugin/wpc-product-options/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wpc-product-options/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wpc-product-options/reviews/)

 * 0 replies
 * 1 participant
 * Last reply from: [quangsangzs](https://wordpress.org/support/users/quangsangzs/)
 * Last activity: [2 years, 2 months ago](https://wordpress.org/support/topic/rest-api-option/)
 * Status: not resolved