Add shipping zone locations update endpoint and handler.

This commit is contained in:
Jeff Stieler 2016-06-15 14:18:35 -06:00 committed by Justin Shreve
parent c041e69e86
commit 626819f9b8
1 changed files with 37 additions and 2 deletions

View File

@ -32,15 +32,21 @@ class WC_REST_Shipping_Zone_Locations_Controller extends WC_REST_Shipping_Zones_
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'permissions_check' ),
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_items' ),
'permission_callback' => array( $this, 'permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
}
/**
* Get all Shipping Zones.
* Get all Shipping Zone Locations.
*
* @param WP_REST_Request $request
* @return WP_REST_Response
* @return WP_REST_Response|WP_Error
*/
public function get_items( $request ) {
$zone = $this->get_zone( $request['id'] );
@ -61,6 +67,35 @@ class WC_REST_Shipping_Zone_Locations_Controller extends WC_REST_Shipping_Zones_
return rest_ensure_response( $data );
}
/**
* Update all Shipping Zone Locations.
*
* @param WP_REST_Request $request
* @return WP_REST_Response|WP_Error
*/
public function update_items( $request ) {
$zone = $this->get_zone( $request['id'] );
if ( is_wp_error( $zone ) ) {
return $zone;
}
$raw_locations = $request->get_json_params();
$locations = array();
foreach ( $raw_locations as $raw_location ) {
if ( empty( $raw_location['code'] ) || empty( $raw_location['type'] ) ) {
continue;
}
$locations[] = $raw_location;
}
$zone->set_locations( $locations );
$zone->save();
return $this->get_items( $request );
}
/**
* Prepare the Shipping Zone Location for the REST response.
*