• I want to add product options to this api but can’t get it?

    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 to see the link]

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