WC_Connect_API_Client::get_label_rates PHP Method

get_label_rates() public method

Gets shipping rates (for labels) from the Connect for WooCommerce Server
public get_label_rates ( array $request ) : object | WP_Error
$request array - array( 'packages' => array( array( 'id' => 'box_1', 'height' => 10, 'length' => 10, 'width' => 10, 'weight' => 10, ), array( 'id' => 'box_2', 'box_id' => 'medium_flat_box_top', 'weight' => 5, ), ... ), 'carrier' => 'usps', 'origin' => array( 'address' => '132 Hawthorne St', 'address_2' => '', 'city' => 'San Francisco', 'state' => 'CA', 'postcode' => '94107', 'country' => 'US', ), 'destination' => array( 'address' => '1550 Snow Creek Dr', 'address_2' => '', 'city' => 'Park City', 'state' => 'UT', 'postcode' => '84060', 'country' => 'US', ), )
return object | WP_Error
        public function get_label_rates($request)
        {
            return $this->request('POST', '/shipping/label/rates', $request);
        }

Usage Example

 /**
  *
  * @param WP_REST_Request $request - See WC_Connect_API_Client::get_label_rates()
  * @return array|WP_Error
  */
 public function update_items($request)
 {
     $request_body = $request->get_body();
     $payload = json_decode($request_body, true, WOOCOMMERCE_CONNECT_MAX_JSON_DECODE_DEPTH);
     // Hardcode USPS rates for now
     $payload['carrier'] = 'usps';
     $response = $this->api_client->get_label_rates($payload);
     if (is_wp_error($response)) {
         $error = new WP_Error($response->get_error_code(), $response->get_error_message(), array('message' => $response->get_error_message()));
         $this->logger->log($error, __CLASS__);
         return $error;
     }
     return array('success' => true, 'rates' => property_exists($response, 'rates') ? $response->rates : new stdClass());
 }