Create GET route for shipping zones, handling case where shipping calculations are disabled.
This commit is contained in:
parent
267e5cba9a
commit
0de6f42b51
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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',
|
||||
|
|
Loading…
Reference in New Issue