扩展woocommerce Rest API

卡迪

我想扩展woocommerce rest api,使其包含“ booking”扩展插件的数据。当前,该扩展名没有由rest api提供的默认终结点。

到目前为止,我已经创建了一个插件,并且添加了以下代码;

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;
    }

当我呼叫终点时/products,仍然只将woocommerce概述的默认数据称为我的小附加组件,无处可寻。

我什至不知道在哪里可以找到上面的过滤器,因为我刚刚在网页上看到了这个过滤器,我试图让它做我想做的事情,也不知道这是否是正确的选择。网页:https//francescocarlucci.com/woocommerce/woocommerce-api-custom-data-default-endpoints/#more-96

上面是我试图扩展api的方法,但是我也决定尝试创建一个自定义的端点,以查看是否可以达到所需的结果,但是到目前为止,我只是做了一个可以调用的端点,但是我不知道该写些什么来检索我想要的数据。

自定义终点代码:

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 :(

        }

无论如何,我可以通过上述方法之一实现我想要的吗?我对开发人员很陌生,我不熟悉JavaScript,而不是PHP,因此我想使用rest api,因为我想将wordpress / woocommerce用作无头cms。

到目前为止,我已经来到这个问题的壁橱示例。创建WooCommerce自定义API

莎米·阿里(Shameem Ali)

这只是我的代码的一部分。一些未定义的变量。和这个公正的概念。希望您可以根据需要进行修改。

    public function __construct() {     
    $this->template_url = apply_filters( 'woocommerce_template_url', 'woocommerce/' 
);
    $this->api_namespace = 'wc/v';
    $this->base = 'home';
    $this->api_version = '2';       
    add_action( 'woocommerce_loaded', array( $this, 'register_hooks' ) );
}

    $namespace = $this->api_namespace . $this->api_version;     
         register_rest_route(
            $namespace, '/wclogin/',
            array(
                'methods'  => 'GET',
                'callback' => array( $this, 'wc_login'),
        )
    );



   function wc_login($request){


    $user = get_user_by('email', $request["email"]);

    //bad email
    if(!$user){
        $error = new WP_Error();
        $error->add('invalid', __('<strong>ERROR</strong>: Either the email or password you entered is invalid.'));
        return $error;
    }
    else{ //check password
        if(!wp_check_password($request["password"], $user->user_pass, $user->ID)){ //bad password
            $error = new WP_Error();
            $error->add('invalid', __('<strong>ERROR</strong>: Either the email or password you entered is invalid.'));
            return $error;
        }else{
            return $user; //passed
        }
    }

} 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章