我對 Wordpress/WooCommerce 還很陌生,我正在努力注冊一個自定義的 rest api 端點。當我嘗試端點時,以下內容給了我一個 404:
class WC_FastSpring_Fulfillment_Integration extends WC_Integration {
public function __construct() {
$this->id = 'av-wc-fastspring';
$this->method_title = '';
$this->method_description = '';
// Method with all the options fields
$this->init_form_fields();
// Load the settings.
$this->init_settings();
// This action hook saves the settings
add_action( 'woocommerce_update_options_integration_' . $this->id, array( $this, 'process_admin_options' ) );
$namespace = 'test';
$endpoint = '/fs-fulfill-order';
add_action( 'rest_api_init', function () {
$logger = wc_get_logger();
$logger->debug("Registered API Endpoint");
register_rest_route( $namespace, $endpoint, array(
'methods' => 'GET',
'callback' => array($this, 'fulfill_fastspring_order'),
'permission_callback' => function() {
return true;
}
) );
} );
}
public function fulfill_fastspring_order() {
global $woocommerce;
$logger = wc_get_logger();
$logger->debug("Received request to fullfill FastSpring order via Rest API.");
return new WP_REST_Response('Damn !');
}
...
}
如果我在課外注冊,它確實有效,但不在課堂內!有任何想法嗎 ?不過,我確實看到了我在 WooCommerce 日志中添加的除錯日志,因此似乎確實觸發了 add_action。
謝謝 !
uj5u.com熱心網友回復:
我已更正您的代碼并將代碼拆分為函式,現在它可以正常作業了
/**
* Request URL: http://127.0.0.1/wordpress/wp-json/test/fs-fulfill-order
*/
class WC_FastSpring_Fulfillment_Integration extends WC_Integration{
function __construct(){
add_action( 'rest_api_init', array($this, 'rest_api_rest_init_fun'));
}
public function rest_api_rest_init_fun(){
$namespace = 'test';
$endpoint = '/fs-fulfill-order';
register_rest_route( $namespace, $endpoint, array(
'methods' => 'GET',
'callback' => array($this, 'fulfill_fastspring_order'),
'permission_callback' => function() {
return true;
}
));
}
public function fulfill_fastspring_order() {
global $woocommerce;
$logger = wc_get_logger();
$logger->debug("Received request to fullfill FastSpring order via Rest API.");
return new WP_REST_Response('Damn !');
}
}
//call class
new WC_FastSpring_Fulfillment_Integration;
截圖:https : //prnt.sc/217fait
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/373465.html
標籤:php WordPress的 休息 求购
上一篇:KafkaConnect訪問架構注冊表的基本身份驗證
下一篇:如何將Base64發送到API
