php - Extending the woocommerce rest api
问题描述
I would like to extend the woocommerce rest api to include data of its 'booking' extension plugin. Currently this extension does not have default endpoints provided by the rest api.
So far I have created a plugin, and I've added the following code;
add_filter( 'woocommerce_rest_prepare_product', 'custom_data');
function custom_data($response, $object) {
if( empty( $response->data ) )
return $response;
$response->data['meta_data'] = get_post_meta( $object[ID], 'availability', true);
return $response;
}
When I call the end point /products
only the default data outlined by woocommerce is still called my little add on is no where to be found.
I don't even know where to find the above filter as I just saw this posted on a webpage and I tried to get it to do what I wanted, don't know if this is the correct direction to go down either. Webpage: https://francescocarlucci.com/woocommerce/woocommerce-api-custom-data-default-endpoints/#more-96
The above was me trying to extend the api but I also decided to try making a custom endpoint to see if I can get my desired outcome but so far I've just made a endpoint which calls but I have no idea what to write to retrieve the data I want.
custom end point code:
function register_custom_route() {
register_rest_route( 'ce/v1', '/bookable',
array(
'methods' => 'GET',
'callback' => 'get_bookable'
)
);
}
function get_bookable( ) {
return array( 'custom' => 'woocommerce here' );
//What code do I write here :(
}
Is there anyway I can achieve what I want under one of the above methods? I'm quite new to dev and I'm familiar with javascript not PHP hence my need to want to use the rest api as I would like to use wordpress/woocommerce as a headless cms.
So far the closets example I've come to has been shown on this question Creating WooCommerce Custom API
解决方案
Alternatively you can try the below code to extend product response of WooCommerce REST API without doing additionally, as your above hook "woocommerce_rest_prepare_product" is for v1 but currently it was v3 so the hook for the latest is below one(the below hook is from v2 controller of rest api which is extended by v3).
add_filter('woocommerce_rest_prepare_product_object', 'so54387226_custom_data', 10, 3);
function so54387226_custom_data($response, $object, $request) {
if (empty($response->data))
return $response;
$id = $object->get_id(); //it will fetch product id
$response->data['booking_meta_data'] = get_post_meta($id, 'availability', true);
return $response;
}
I tested the above code it works perfectly fine. Hope it may helps to someone who search for similar solution.
推荐阅读
- reactjs - 如何仅显示平面列表中一项的加载?
- java - 打印 java 中的 trie 树中的所有单词,这些单词存在于先前插入的树中
- email - html 电子邮件通知无法正常工作
- c# - 如何从迁移脚本中删除默认约束
- powershell - 如何过滤 EventLog 以每天获取一个日志 - PowerShell
- android - 使用 Android Camera2 为 Surfaces 配置不同的 FPS
- python - 在 apply 中访问 groupby 值
- vba - 当我使用 DoCmd.OpenForm 时运行时关闭
- node.js - 使用 node.js 脚本重命名文件名的一部分
- javascript - 从 SQL 中以 JSON 格式检索多个记录集