Skip to content

WCAPIEndpoint trait

Barberklingen\BasePlugin\Traits\WCAPIEndpoint

Registers a WooCommerce REST API action via the woocommerce_api_{action_name} hook. Use this for lightweight WC API callbacks; for full WP REST API routes see REST API.

Usage

use Barberklingen\BasePlugin\Traits\WCAPIEndpoint;

class MyWCEndpoint {
    use WCAPIEndpoint;

    public function action_name(): string {
        return 'my_endpoint'; // registers woocommerce_api_my_endpoint
    }

    public function execute(): void {
        $orderId = $this->get_request()->input('order_id');
        $order   = wc_get_order($orderId);

        if (!$order) {
            $this->error('Order not found', 404);
        }

        $this->success(['order_id' => $order->get_id()]);
    }
}

MyWCEndpoint::get_instance();

Response methods

Method Description
success($data = []) HTTP 200 JSON response
error($message, $code = 500) Error JSON response
response($data, $code) Raw response with custom code
success_with_filter($data, ...$args) 200 response, data passed through a filter

Frontend model helper

$accountData = $this->create_account_data_object($subscription);

Passes the subscription through the base-plugin/frontend/subscription-data-model filter, allowing consuming plugins to customise the data shape returned to the frontend.

hooks() stub

The trait exposes an empty hooks(): void method that subclasses can override to register additional actions/filters beyond the main endpoint.