woocommerce
/
src
/
Internal
/
RestApi
/
Routes
/
V4
/
ShippingZoneMethod
/
ShippingMethodSchema.php
Controller.php
5 months ago
ShippingMethodSchema.php
7 months ago
ShippingZoneMethodService.php
7 months ago
ShippingMethodSchema.php
139 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Shipping Method Schema. |
| 4 | * |
| 5 | * @package WooCommerce\RestApi |
| 6 | */ |
| 7 | |
| 8 | declare( strict_types=1 ); |
| 9 | |
| 10 | namespace Automattic\WooCommerce\Internal\RestApi\Routes\V4\ShippingZoneMethod; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | use Automattic\WooCommerce\Internal\RestApi\Routes\V4\AbstractSchema; |
| 15 | use WP_REST_Request; |
| 16 | |
| 17 | /** |
| 18 | * Shipping Method Schema class. |
| 19 | */ |
| 20 | class ShippingMethodSchema extends AbstractSchema { |
| 21 | |
| 22 | /** |
| 23 | * The schema identifier. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | const IDENTIFIER = 'shipping_method'; |
| 28 | |
| 29 | /** |
| 30 | * Return all properties for the item schema |
| 31 | * |
| 32 | * @return array |
| 33 | */ |
| 34 | public function get_item_schema_properties(): array { |
| 35 | return array( |
| 36 | 'instance_id' => array( |
| 37 | 'description' => __( 'Shipping method instance ID.', 'woocommerce' ), |
| 38 | 'type' => 'integer', |
| 39 | 'context' => array( 'view', 'edit' ), |
| 40 | 'readonly' => true, |
| 41 | ), |
| 42 | 'zone_id' => array( |
| 43 | 'description' => __( 'Shipping zone ID.', 'woocommerce' ), |
| 44 | 'type' => 'integer', |
| 45 | 'context' => array( 'view', 'edit' ), |
| 46 | 'required' => true, |
| 47 | ), |
| 48 | 'enabled' => array( |
| 49 | 'description' => __( 'Whether the shipping method is enabled.', 'woocommerce' ), |
| 50 | 'type' => 'boolean', |
| 51 | 'context' => array( 'view', 'edit' ), |
| 52 | 'required' => true, |
| 53 | ), |
| 54 | 'order' => array( |
| 55 | 'description' => __( 'Shipping method sort order.', 'woocommerce' ), |
| 56 | 'type' => 'integer', |
| 57 | 'context' => array( 'view', 'edit' ), |
| 58 | 'sanitize_callback' => 'absint', |
| 59 | ), |
| 60 | 'method_id' => array( |
| 61 | 'description' => __( 'Shipping method ID.', 'woocommerce' ), |
| 62 | 'type' => 'string', |
| 63 | 'context' => array( 'view', 'edit' ), |
| 64 | 'required' => true, |
| 65 | ), |
| 66 | 'settings' => array( |
| 67 | 'description' => __( 'Shipping method settings including title and configuration.', 'woocommerce' ), |
| 68 | 'type' => 'object', |
| 69 | 'context' => array( 'view', 'edit' ), |
| 70 | 'required' => true, |
| 71 | 'properties' => array( |
| 72 | 'title' => array( |
| 73 | 'description' => __( 'Shipping method title.', 'woocommerce' ), |
| 74 | 'type' => 'string', |
| 75 | 'context' => array( 'view', 'edit' ), |
| 76 | 'required' => true, |
| 77 | ), |
| 78 | ), |
| 79 | 'additionalProperties' => true, |
| 80 | ), |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get the item response for a shipping method. |
| 86 | * |
| 87 | * @param object $method Shipping method instance. |
| 88 | * @param WP_REST_Request $request Request object. |
| 89 | * @param array $include_fields Fields to include in the response. |
| 90 | * @return array The item response. |
| 91 | */ |
| 92 | public function get_item_response( $method, WP_REST_Request $request, array $include_fields = array() ): array { |
| 93 | if ( isset( $request['zone_id'] ) ) { |
| 94 | $zone_id = (int) $request['zone_id']; |
| 95 | } else { |
| 96 | $data_store = \WC_Data_Store::load( 'shipping-zone' ); |
| 97 | $zone_id = $data_store->get_zone_id_by_instance_id( $method->instance_id ); |
| 98 | } |
| 99 | |
| 100 | return array( |
| 101 | 'instance_id' => (int) $method->instance_id, |
| 102 | 'zone_id' => (int) $zone_id, |
| 103 | 'enabled' => wc_string_to_bool( $method->enabled ), |
| 104 | 'order' => (int) $method->method_order, |
| 105 | 'method_id' => $method->id, |
| 106 | 'settings' => $this->get_method_settings( $method ), |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get shipping method settings with title included. |
| 112 | * |
| 113 | * @param object $method Shipping method instance. |
| 114 | * @return array Method settings including title. |
| 115 | */ |
| 116 | protected function get_method_settings( $method ): array { |
| 117 | $settings = array(); |
| 118 | |
| 119 | // Get the method title (moved from root to settings per Ismael's feedback). |
| 120 | $settings['title'] = $method->get_title(); |
| 121 | |
| 122 | // Get common method settings. |
| 123 | $common_fields = array( 'cost', 'min_amount', 'requires', 'class_cost', 'no_class_cost', 'tax_status' ); |
| 124 | |
| 125 | foreach ( $common_fields as $field ) { |
| 126 | if ( isset( $method->$field ) ) { |
| 127 | $settings[ $field ] = $method->$field; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Return all available settings for maximum flexibility. |
| 132 | if ( isset( $method->instance_settings ) && is_array( $method->instance_settings ) ) { |
| 133 | $settings = array_merge( $settings, $method->instance_settings ); |
| 134 | } |
| 135 | |
| 136 | return $settings; |
| 137 | } |
| 138 | } |
| 139 |