How do can I extend woocommerce api endpoints?

試著忘記壹切 提交于 2020-01-23 13:10:50

问题


I'm trying to extend the customer endpoint in woocommerce api to include some customfields I've created in my functions.php. But I can't understand how to do it.

I have copied the class-wc-rest-customers-controller.php from the woocommerce plugin (woocommerce/includes/api/) to my woocommerce-folder in my theme as you should do with woocommerce files when you want to edit them.

This is how my class-wc-rest-customers-controller.php looks like: https://www.pastebucket.com/561405

My plan now was to edit this copy of the file to include my custom_fields I've added in functions.php. But I can't solve this part.

This is the code from my functions.php that I added my custom fields with: https://www.pastebucket.com/561406

It feels like it is in the function at line 475 in class-wc-rest-customers-controller.php, but I'm not sure. So I'm wondering where and how should I add my custom fields to this class-wc-rest-customers-controller.php or I'm all wrong about this?


回答1:


The file overrides apply AFAIK for templates only. In this case you are trying to override a class, which is different.

As you wrote, it's not a good idea to make your changes directly to the file inside WooCommerce directory. In fact, I wouldn't recommend changing the native endpoint at all, except through actions & filters.

One good, re-usable and future-proof way to change the behavior of a WC REST API endpoint would be to create your own endpoint which simply extends the Woocommerce controller class, and overrides the methods which need to be customized. Preferably, try not to override the entire method, but include the call to the parent method in your custom method.

Example solution: (haven't tested this particular one, but made a very similar one recently)

wp-content/plugins/
    woocommerce/
    your-plugin/
         includes/
             class-your-plugin.php
         your-plugin.php

your-plugin.php

<?php
defined( 'ABSPATH' ) || exit;

add_action('rest_api_init', function(){
    require_once __DIR__ . '/includes/class-your-plugin.php';
    $controller = new Your_Custom_Class();
    $controller->register_routes();
} );

includes/class-your-plugin.php

<?php

defined( 'ABSPATH' ) || exit;

/**
 * Class Your_Custom_Class
 */
class Your_Custom_Class extends WC_REST_Customers_Controller {

/**
 * Endpoint namespace.
 * Use the wc- prefix to make use of WooCommerce authentication for third-parties.
 * @see /wp-content/plugins/woocommerce/includes/api/class-wc-rest-authentication.php:63
 *
 * @var string
 */
protected $namespace = 'wc-your-route/v1';

public function register_routes() {
    register_rest_route(
        $this->namespace,
        '/' . $this->rest_base . '/your-custom-route',
        array(
            array(
                'methods'             => WP_REST_Server::EDITABLE,
                'callback'            => array( $this, 'your_customized_function' ),
                'permission_callback' => array( $this, 'get_items_permissions_check' ),
                'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
            ),
            'schema' => array( $this, 'get_item_schema' ),
        )
    );
}

/**
 * Your custom function
 */
protected function your_customized_function( $arg1, $arg2) {
    /*******************************
    // IMPLEMENT CUSTOM CODE HERE
    *******************************/

    parent::the_name_of_the_original_function( $arg1, $arg2 );

    /*******************************
    // IMPLEMENT CUSTOM CODE HERE
    *******************************/
}

This way you can freely extend the API for your own needs, take advantage of all current and future features of the WC API, and preserve the native API... well.. native.

While this should be a clean and "correct" solution, I would not recommend going for this path without a solid understanding of PHP classes and inheritance, as well as a good IDE to work with.




回答2:


So it seems I managed to solve it. I couldn't overwrite the class-wc-rest-customers-controller.php by copying it to my-theme-folder/woccomerce/includes/api/

So instead I just overwrote it and kept a backup of the original. But I now also need to keep an backup of of the class-wc-rest-customers-controller.php file I overwrote with.

This isn't the correct way of doing it, but this was the only way I could sole my problem.

UPDATE: Seems like I cannot update these values through the api. So this was not a solution at all.



来源:https://stackoverflow.com/questions/42372792/how-do-can-i-extend-woocommerce-api-endpoints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!