AI
1 year ago
Agentic
7 months ago
AbstractAddressSchema.php
1 year ago
AbstractSchema.php
1 month ago
BatchSchema.php
2 years ago
BillingAddressSchema.php
2 years ago
CartCouponSchema.php
1 year ago
CartExtensionsSchema.php
1 year ago
CartFeeSchema.php
2 years ago
CartItemSchema.php
1 month ago
CartSchema.php
2 months ago
CartShippingRateSchema.php
1 month ago
CheckoutOrderSchema.php
2 years ago
CheckoutSchema.php
1 month ago
ErrorSchema.php
2 years ago
ImageAttachmentSchema.php
3 months ago
ItemSchema.php
11 months ago
OrderCouponSchema.php
2 years ago
OrderFeeSchema.php
2 years ago
OrderItemSchema.php
1 month ago
OrderSchema.php
2 months ago
PatternsSchema.php
1 year ago
ProductAttributeSchema.php
2 years ago
ProductAttributeTermSchema.php
1 month ago
ProductBrandSchema.php
1 year ago
ProductCategorySchema.php
2 years ago
ProductCollectionDataSchema.php
11 months ago
ProductReviewSchema.php
2 years ago
ProductSchema.php
1 month ago
ShippingAddressSchema.php
2 years ago
ShopperListItemSchema.php
1 month ago
ShopperListSchema.php
1 month ago
TermSchema.php
2 years ago
CartShippingRateSchema.php
355 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Schemas\V1; |
| 3 | |
| 4 | use WC_Shipping_Rate as ShippingRate; |
| 5 | |
| 6 | /** |
| 7 | * CartShippingRateSchema class. |
| 8 | */ |
| 9 | class CartShippingRateSchema extends AbstractSchema { |
| 10 | /** |
| 11 | * The schema item name. |
| 12 | * |
| 13 | * @var string |
| 14 | */ |
| 15 | protected $title = 'cart-shipping-rate'; |
| 16 | |
| 17 | /** |
| 18 | * The schema item identifier. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | const IDENTIFIER = 'cart-shipping-rate'; |
| 23 | |
| 24 | /** |
| 25 | * Cart schema properties. |
| 26 | * |
| 27 | * @return array |
| 28 | */ |
| 29 | public function get_properties() { |
| 30 | return [ |
| 31 | 'package_id' => [ |
| 32 | 'description' => __( 'The ID of the package the shipping rates belong to.', 'woocommerce' ), |
| 33 | 'type' => [ 'integer', 'string' ], |
| 34 | 'context' => [ 'view', 'edit' ], |
| 35 | 'readonly' => true, |
| 36 | ], |
| 37 | 'name' => [ |
| 38 | 'description' => __( 'Name of the package.', 'woocommerce' ), |
| 39 | 'type' => 'string', |
| 40 | 'context' => [ 'view', 'edit' ], |
| 41 | 'readonly' => true, |
| 42 | ], |
| 43 | 'destination' => [ |
| 44 | 'description' => __( 'Shipping destination address.', 'woocommerce' ), |
| 45 | 'type' => 'object', |
| 46 | 'context' => [ 'view', 'edit' ], |
| 47 | 'readonly' => true, |
| 48 | 'properties' => [ |
| 49 | 'address_1' => [ |
| 50 | 'description' => __( 'First line of the address being shipped to.', 'woocommerce' ), |
| 51 | 'type' => 'string', |
| 52 | 'context' => [ 'view', 'edit' ], |
| 53 | 'readonly' => true, |
| 54 | ], |
| 55 | 'address_2' => [ |
| 56 | 'description' => __( 'Second line of the address being shipped to.', 'woocommerce' ), |
| 57 | 'type' => 'string', |
| 58 | 'context' => [ 'view', 'edit' ], |
| 59 | 'readonly' => true, |
| 60 | ], |
| 61 | 'city' => [ |
| 62 | 'description' => __( 'City of the address being shipped to.', 'woocommerce' ), |
| 63 | 'type' => 'string', |
| 64 | 'context' => [ 'view', 'edit' ], |
| 65 | 'readonly' => true, |
| 66 | ], |
| 67 | 'state' => [ |
| 68 | 'description' => __( 'ISO code, or name, for the state, province, or district of the address being shipped to.', 'woocommerce' ), |
| 69 | 'type' => 'string', |
| 70 | 'context' => [ 'view', 'edit' ], |
| 71 | 'readonly' => true, |
| 72 | ], |
| 73 | 'postcode' => [ |
| 74 | 'description' => __( 'Zip or Postcode of the address being shipped to.', 'woocommerce' ), |
| 75 | 'type' => 'string', |
| 76 | 'context' => [ 'view', 'edit' ], |
| 77 | 'readonly' => true, |
| 78 | ], |
| 79 | 'country' => [ |
| 80 | 'description' => __( 'ISO code for the country of the address being shipped to.', 'woocommerce' ), |
| 81 | 'type' => 'string', |
| 82 | 'context' => [ 'view', 'edit' ], |
| 83 | 'readonly' => true, |
| 84 | ], |
| 85 | ], |
| 86 | ], |
| 87 | 'items' => [ |
| 88 | 'description' => __( 'List of cart items the returned shipping rates apply to.', 'woocommerce' ), |
| 89 | 'type' => 'array', |
| 90 | 'context' => [ 'view', 'edit' ], |
| 91 | 'readonly' => true, |
| 92 | 'items' => [ |
| 93 | 'type' => 'object', |
| 94 | 'properties' => [ |
| 95 | 'key' => [ |
| 96 | 'description' => __( 'Unique identifier for the item within the cart.', 'woocommerce' ), |
| 97 | 'type' => 'string', |
| 98 | 'context' => [ 'view', 'edit' ], |
| 99 | 'readonly' => true, |
| 100 | ], |
| 101 | 'name' => [ |
| 102 | 'description' => __( 'Name of the item.', 'woocommerce' ), |
| 103 | 'type' => 'string', |
| 104 | 'context' => [ 'view', 'edit' ], |
| 105 | 'readonly' => true, |
| 106 | ], |
| 107 | 'quantity' => [ |
| 108 | 'description' => __( 'Quantity of the item in the current package.', 'woocommerce' ), |
| 109 | 'type' => 'number', |
| 110 | 'context' => [ 'view', 'edit' ], |
| 111 | 'readonly' => true, |
| 112 | ], |
| 113 | ], |
| 114 | ], |
| 115 | ], |
| 116 | 'shipping_rates' => [ |
| 117 | 'description' => __( 'List of shipping rates.', 'woocommerce' ), |
| 118 | 'type' => 'array', |
| 119 | 'context' => [ 'view', 'edit' ], |
| 120 | 'readonly' => true, |
| 121 | 'items' => [ |
| 122 | 'type' => 'object', |
| 123 | 'properties' => $this->get_rate_properties(), |
| 124 | ], |
| 125 | ], |
| 126 | ]; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Schema for a single rate. |
| 131 | * |
| 132 | * @return array |
| 133 | */ |
| 134 | protected function get_rate_properties() { |
| 135 | return array_merge( |
| 136 | [ |
| 137 | 'rate_id' => [ |
| 138 | 'description' => __( 'ID of the shipping rate.', 'woocommerce' ), |
| 139 | 'type' => 'string', |
| 140 | 'context' => [ 'view', 'edit' ], |
| 141 | 'readonly' => true, |
| 142 | ], |
| 143 | 'name' => [ |
| 144 | 'description' => __( 'Name of the shipping rate, e.g. Express shipping.', 'woocommerce' ), |
| 145 | 'type' => 'string', |
| 146 | 'context' => [ 'view', 'edit' ], |
| 147 | 'readonly' => true, |
| 148 | ], |
| 149 | 'description' => [ |
| 150 | 'description' => __( 'Description of the shipping rate, e.g. Dispatched via USPS.', 'woocommerce' ), |
| 151 | 'type' => 'string', |
| 152 | 'context' => [ 'view', 'edit' ], |
| 153 | 'readonly' => true, |
| 154 | ], |
| 155 | 'delivery_time' => [ |
| 156 | 'description' => __( 'Delivery time estimate text, e.g. 3-5 business days.', 'woocommerce' ), |
| 157 | 'type' => 'string', |
| 158 | 'context' => [ 'view', 'edit' ], |
| 159 | 'readonly' => true, |
| 160 | ], |
| 161 | 'price' => [ |
| 162 | 'description' => __( 'Price of this shipping rate using the smallest unit of the currency.', 'woocommerce' ), |
| 163 | 'type' => 'string', |
| 164 | 'context' => [ 'view', 'edit' ], |
| 165 | 'readonly' => true, |
| 166 | ], |
| 167 | 'taxes' => [ |
| 168 | 'description' => __( 'Taxes applied to this shipping rate using the smallest unit of the currency.', 'woocommerce' ), |
| 169 | 'type' => 'string', |
| 170 | 'context' => [ 'view', 'edit' ], |
| 171 | 'readonly' => true, |
| 172 | ], |
| 173 | 'method_id' => [ |
| 174 | 'description' => __( 'ID of the shipping method that provided the rate.', 'woocommerce' ), |
| 175 | 'type' => 'string', |
| 176 | 'context' => [ 'view', 'edit' ], |
| 177 | 'readonly' => true, |
| 178 | ], |
| 179 | 'instance_id' => [ |
| 180 | 'description' => __( 'Instance ID of the shipping method that provided the rate.', 'woocommerce' ), |
| 181 | 'type' => 'integer', |
| 182 | 'context' => [ 'view', 'edit' ], |
| 183 | 'readonly' => true, |
| 184 | ], |
| 185 | 'meta_data' => [ |
| 186 | 'description' => __( 'Meta data attached to the shipping rate.', 'woocommerce' ), |
| 187 | 'type' => 'array', |
| 188 | 'context' => [ 'view', 'edit' ], |
| 189 | 'items' => [ |
| 190 | 'type' => 'object', |
| 191 | 'properties' => [ |
| 192 | 'key' => [ |
| 193 | 'description' => __( 'Meta key.', 'woocommerce' ), |
| 194 | 'type' => 'string', |
| 195 | 'context' => [ 'view', 'edit' ], |
| 196 | 'readonly' => true, |
| 197 | ], |
| 198 | 'value' => [ |
| 199 | 'description' => __( 'Meta value.', 'woocommerce' ), |
| 200 | 'type' => 'string', |
| 201 | 'context' => [ 'view', 'edit' ], |
| 202 | 'readonly' => true, |
| 203 | ], |
| 204 | ], |
| 205 | ], |
| 206 | ], |
| 207 | 'selected' => [ |
| 208 | 'description' => __( 'True if this is the rate currently selected by the customer for the cart.', 'woocommerce' ), |
| 209 | 'type' => 'boolean', |
| 210 | 'context' => [ 'view', 'edit' ], |
| 211 | 'readonly' => true, |
| 212 | ], |
| 213 | ], |
| 214 | $this->get_store_currency_properties() |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Convert a shipping rate from WooCommerce into a valid response. |
| 220 | * |
| 221 | * @param array $package Shipping package complete with rates from WooCommerce. |
| 222 | * @return array |
| 223 | */ |
| 224 | public function get_item_response( $package ) { |
| 225 | return [ |
| 226 | 'package_id' => $package['package_id'], |
| 227 | 'name' => $package['package_name'], |
| 228 | 'destination' => $this->prepare_package_destination_response( $package ), |
| 229 | 'items' => $this->prepare_package_items_response( $package ), |
| 230 | 'shipping_rates' => $this->prepare_package_shipping_rates_response( $package ), |
| 231 | ]; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Gets and formats the destination address of a package. |
| 236 | * |
| 237 | * @param array $package Shipping package complete with rates from WooCommerce. |
| 238 | * @return object |
| 239 | */ |
| 240 | protected function prepare_package_destination_response( $package ) { |
| 241 | // If address_1 fails check address for back compatibility. |
| 242 | $address = isset( $package['destination']['address_1'] ) ? $package['destination']['address_1'] : $package['destination']['address']; |
| 243 | return (object) $this->prepare_html_response( |
| 244 | [ |
| 245 | 'address_1' => $address, |
| 246 | 'address_2' => $package['destination']['address_2'], |
| 247 | 'city' => $package['destination']['city'], |
| 248 | 'state' => $package['destination']['state'], |
| 249 | 'postcode' => $package['destination']['postcode'], |
| 250 | 'country' => $package['destination']['country'], |
| 251 | ] |
| 252 | ); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Gets items from a package and creates an array of strings containing product names and quantities. |
| 257 | * |
| 258 | * @param array $package Shipping package complete with rates from WooCommerce. |
| 259 | * @return array |
| 260 | */ |
| 261 | protected function prepare_package_items_response( $package ) { |
| 262 | $items = array(); |
| 263 | foreach ( $package['contents'] as $values ) { |
| 264 | $items[] = [ |
| 265 | 'key' => $values['key'], |
| 266 | 'name' => $values['data']->get_name(), |
| 267 | 'quantity' => $values['quantity'], |
| 268 | ]; |
| 269 | } |
| 270 | return $items; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Prepare an array of rates from a package for the response. |
| 275 | * |
| 276 | * @param array $package Shipping package complete with rates from WooCommerce. |
| 277 | * @return array |
| 278 | */ |
| 279 | protected function prepare_package_shipping_rates_response( $package ) { |
| 280 | $rates = $package['rates']; |
| 281 | $selected_rates = wc()->session->get( 'chosen_shipping_methods', array() ); |
| 282 | $selected_rate = isset( $selected_rates[ $package['package_id'] ] ) ? $selected_rates[ $package['package_id'] ] : ''; |
| 283 | |
| 284 | if ( empty( $selected_rate ) && ! empty( $package['rates'] ) ) { |
| 285 | $selected_rate = wc_get_chosen_shipping_method_for_package( $package['package_id'], $package ); |
| 286 | } |
| 287 | |
| 288 | $response = []; |
| 289 | |
| 290 | foreach ( $package['rates'] as $rate ) { |
| 291 | $response[] = $this->get_rate_response( $rate, $selected_rate ); |
| 292 | } |
| 293 | |
| 294 | return $response; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Response for a single rate. |
| 299 | * |
| 300 | * @param WC_Shipping_Rate $rate Rate object. |
| 301 | * @param string $selected_rate Selected rate. |
| 302 | * @return array |
| 303 | */ |
| 304 | protected function get_rate_response( $rate, $selected_rate = '' ) { |
| 305 | return $this->prepare_currency_response( |
| 306 | [ |
| 307 | 'rate_id' => $this->get_rate_prop( $rate, 'id' ), |
| 308 | 'name' => $this->prepare_html_response( $this->get_rate_prop( $rate, 'label' ) ), |
| 309 | 'description' => $this->prepare_html_response( $this->get_rate_prop( $rate, 'description' ) ), |
| 310 | 'delivery_time' => $this->prepare_html_response( $this->get_rate_prop( $rate, 'delivery_time' ) ), |
| 311 | 'price' => $this->prepare_money_response( $this->get_rate_prop( $rate, 'cost' ), wc_get_price_decimals() ), |
| 312 | 'taxes' => $this->prepare_money_response( array_sum( (array) $this->get_rate_prop( $rate, 'taxes' ) ), wc_get_price_decimals() ), |
| 313 | 'instance_id' => $this->get_rate_prop( $rate, 'instance_id' ), |
| 314 | 'method_id' => $this->get_rate_prop( $rate, 'method_id' ), |
| 315 | 'meta_data' => $this->get_rate_meta_data( $rate ), |
| 316 | 'selected' => $selected_rate === $this->get_rate_prop( $rate, 'id' ), |
| 317 | ] |
| 318 | ); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Gets a prop of the rate object, if callable. |
| 323 | * |
| 324 | * @param WC_Shipping_Rate $rate Rate object. |
| 325 | * @param string $prop Prop name. |
| 326 | * @return string |
| 327 | */ |
| 328 | protected function get_rate_prop( $rate, $prop ) { |
| 329 | $getter = 'get_' . $prop; |
| 330 | return \is_callable( array( $rate, $getter ) ) ? $rate->$getter() : ''; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Converts rate meta data into a suitable response object. |
| 335 | * |
| 336 | * @param WC_Shipping_Rate $rate Rate object. |
| 337 | * @return array |
| 338 | */ |
| 339 | protected function get_rate_meta_data( $rate ) { |
| 340 | $meta_data = $rate->get_meta_data(); |
| 341 | |
| 342 | return array_reduce( |
| 343 | array_keys( $meta_data ), |
| 344 | function( $return, $key ) use ( $meta_data ) { |
| 345 | $return[] = [ |
| 346 | 'key' => $key, |
| 347 | 'value' => $meta_data[ $key ], |
| 348 | ]; |
| 349 | return $return; |
| 350 | }, |
| 351 | [] |
| 352 | ); |
| 353 | } |
| 354 | } |
| 355 |