woocommerce offtopic – How to retrieve full product information?
I’ve being working on a plugin and at this point I need to retrieve all product information, including category name, attributes, image URL, etc…
When I use wc_get_product
I can only retrieve the ID of these attributes and not the names.
This is and example of the data retrieved by wc_get_product:
{
// ...
gallery_image_ids: [42,43,44],
category_ids: [10]
// ...
}
The content that I need is basically the same provided from the REST API:
"categories": [
{
"id": 9,
"name": "Clothing",
"slug": "clothing"
},
{
"id": 14,
"name": "T-shirts",
"slug": "t-shirts"
}
],
"images": [
{
"id": 792,
"date_created": "2017-03-23T14:01:13",
"date_created_gmt": "2017-03-23T20:01:13",
"date_modified": "2017-03-23T14:01:13",
"date_modified_gmt": "2017-03-23T20:01:13",
"src": "https://example.com/wp-content/uploads/2017/03/T_2_front-4.jpg",
"name": "",
"alt": ""
}
Looking into the plugin code I could manage to find the controller that provide the product response;WC_REST_Products_Controller
, I also saw the method get_item
that is responsible to respond to the REST API. (v3/product)
This is the method: https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-crud-controller.php#L136
I could simply instantiate this class and call ->get_item()
but it doesn’t feel right because I’m calling a controller and not a service class…
I was wondering, is there a simpler or recommended way to retrieve full product information in the same way as REST API retrieves, how can I do this?
Leave an answer