扩展woocommerce rest api [英] Extending the woocommerce rest api

查看:33
本文介绍了扩展woocommerce rest api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想扩展 woocommerce rest api 以包含其预订"扩展插件的数据.目前这个扩展没有由 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;
    }

当我调用终点 /products 时,只有 woocommerce 概述的默认数据仍然被称为我的小插件无处可寻.

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.

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

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

以上是我试图扩展 api 但我也决定尝试创建一个自定义端点以查看是否可以获得我想要的结果但到目前为止我刚刚创建了一个调用的端点但我不知道该怎么做写入以检索我想要的数据.

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.

自定义端点代码:

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,因此我需要使用其余的 api,因为我想使用 wordpress/woocommerce 作为无头 cms.

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.

到目前为止,我已经在这个问题上展示了壁橱示例 创建 WooCommerce 自定义 API

So far the closets example I've come to has been shown on this question Creating WooCommerce Custom API

推荐答案

这只是我代码的一部分.一些未定义的变量.而这只是概念.希望您可以根据您的要求进行修改.

this is only part of my code. some variable not defined. and this just concept. hopefully, you can modify as per your requirement.

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

} 

这篇关于扩展woocommerce rest api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆