【问题标题】:Extending the woocommerce rest api扩展 woocommerce rest api
【发布时间】:2019-01-27 10:40:16
【问题描述】:

我想扩展 woocommerce rest api 以包含其“预订”扩展插件的数据。目前这个扩展没有由 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,因此我需要使用其余的 api,因为我想使用 wordpress/woocommerce 作为无头 cms。

到目前为止,我已经在这个问题Creating WooCommerce Custom API 上展示了壁橱示例

【问题讨论】:

    标签: php wordpress woocommerce woocommerce-rest-api


    【解决方案1】:

    或者,您可以尝试以下代码来扩展 WooCommerce REST API 的产品响应而无需额外执行,因为您上面的钩子“woocommerce_rest_prepare_product”适用于 v1,但目前它是 v3 所以最新的钩子在一个下面(下面的钩子来自于 v3 扩展的 rest api 的 v2 控制器)。

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

    我测试了上面的代码,它工作得很好。希望对寻找类似解决方案的人有所帮助。

    【讨论】:

    • 我可以知道您如何以及在哪里找到挂钩名称 'woocommerce_rest_prepare_product_object' 吗?我无法找到可用的 WC Rest API 挂钩列表来扩展其功能。
    【解决方案2】:

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

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

    【讨论】:

    • 感谢您回复我!你看get_user_by()这个函数是wordpress还是你创建的?
    • 还有你的register_hooks函数在哪里?
    • get_user_by() wp 函数。
    • public function register_hooks() { // REST API. add_action( 'rest_api_init', array( $this, 'rest_api_register_routes' ) ); add_filter( 'woocommerce_rest_prepare_product', array( $this, 'rest_api_prepare_brands_to_product' ), 10, 2 ); // WC 2.6.x add_filter( "woocommerce_rest_prepare_shop_order_object", array( $this, "get_product_order_image"), 10, 3 ); }
    • 嘿@shameem Ali P.K 我想知道你是否可以深入了解我的新问题stackoverflow.com/questions/54673763/…。在非常感谢之前,您提供了如此大的帮助!
    【解决方案3】:

    只是针对那些提出这个问题的人的更新。现在,有一个用于预订扩展的rest api:https://docs.woocommerce.com/document/bookings-rest-api-reference/

    【讨论】:

    • 链接很棒,但如果您能总结出一种可能的方法来纠正问题中概述的问题,那将会更有帮助。
    猜你喜欢
    • 2017-04-09
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 2018-08-28
    相关资源
    最近更新 更多