PageRenderTime 103ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/packages/woocommerce-blocks/src/StoreApi/Schemas/CartShippingRateSchema.php

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