Create GET route for shipping zones, handling case where shipping calculations are disabled.

This commit is contained in:
Jeff Stieler 2016-06-14 10:33:25 -06:00 committed by Justin Shreve
parent 267e5cba9a
commit 0de6f42b51
2 changed files with 33 additions and 0 deletions

View File

@ -36,6 +36,38 @@ class WC_REST_Shipping_Zones_Controller extends WC_REST_Controller {
*/
protected $rest_base = 'shipping/zones';
/**
* Register the routes for Shipping Zones.
*/
public function register_routes() {
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
}
/**
* Check whether a given request has permission to read Shipping Zones.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! wc_shipping_enabled() ) {
return new WP_Error( 'rest_no_route', __( 'Shipping is disabled.' ), array( 'status' => 404 ) );
}
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Get the Shipping Zones schema, conforming to JSON Schema
*

View File

@ -197,6 +197,7 @@ class WC_API extends WC_Legacy_API {
'WC_REST_Reports_Controller',
'WC_REST_Settings_Controller',
'WC_REST_Settings_Options_Controller',
'WC_REST_Shipping_Zones_Controller',
'WC_REST_Tax_Classes_Controller',
'WC_REST_Taxes_Controller',
'WC_REST_Webhook_Deliveries_Controller',