| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Checkout_Blocks/Blocks_Integration file. |
| 4 |
* |
| 5 |
* @package PostNLWooCommerce\Checkout_Blocks |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace PostNLWooCommerce\Checkout_Blocks; |
| 9 |
|
| 10 |
use function PostNLWooCommerce\postnl; |
| 11 |
use Automattic\WooCommerce\StoreApi\StoreApi; |
| 12 |
use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema; |
| 13 |
use Automattic\WooCommerce\StoreApi\Schemas\V1\CheckoutSchema; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Class for extending the Store API endpoint for custom PostNL data in checkout. |
| 21 |
* |
| 22 |
* @package PostNLWooCommerce\Checkout_Blocks |
| 23 |
*/ |
| 24 |
class Extend_Store_Endpoint { |
| 25 |
/** |
| 26 |
* Stores Rest Extending instance. |
| 27 |
* |
| 28 |
* @var ExtendSchema |
| 29 |
*/ |
| 30 |
private static $extend; |
| 31 |
|
| 32 |
/** |
| 33 |
* Plugin Identifier, unique to each plugin. |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
const IDENTIFIER = 'postnl'; |
| 38 |
|
| 39 |
/** |
| 40 |
* Bootstraps the class and hooks required data. |
| 41 |
*/ |
| 42 |
public static function init() { |
| 43 |
add_action( |
| 44 |
'init', |
| 45 |
function () { |
| 46 |
self::$extend = StoreApi::container()->get( ExtendSchema::class ); |
| 47 |
self::extend_store(); |
| 48 |
} |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Registers the actual data into the Checkout endpoint. |
| 54 |
*/ |
| 55 |
public static function extend_store() { |
| 56 |
if ( is_callable( array( self::$extend, 'register_endpoint_data' ) ) ) { |
| 57 |
self::$extend->register_endpoint_data( |
| 58 |
array( |
| 59 |
'endpoint' => CheckoutSchema::IDENTIFIER, |
| 60 |
'namespace' => self::IDENTIFIER, |
| 61 |
'schema_callback' => array( __CLASS__, 'extend_checkout_schema' ), |
| 62 |
'schema_type' => ARRAY_A, |
| 63 |
) |
| 64 |
); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Defines the schema for PostNL delivery data in Checkout. |
| 70 |
* |
| 71 |
* @return array Schema structure. |
| 72 |
*/ |
| 73 |
public static function extend_checkout_schema(): array { |
| 74 |
|
| 75 |
$schema = array(); |
| 76 |
|
| 77 |
$settings = postnl()->get_shipping_settings(); |
| 78 |
|
| 79 |
if ( $settings && $settings->is_reorder_nl_address_enabled() ) { |
| 80 |
$schema['houseNumber'] = array( |
| 81 |
'description' => 'Shipping house number PostNL', |
| 82 |
'context' => array( 'view', 'edit' ), |
| 83 |
'readonly' => false, |
| 84 |
'default' => null, |
| 85 |
'nullable' => true, |
| 86 |
'type' => array( 'string', 'integer', 'null' ), |
| 87 |
); |
| 88 |
} |
| 89 |
$schema += array( |
| 90 |
'deliveryDay' => array( |
| 91 |
'description' => 'Selected delivery day for PostNL', |
| 92 |
'context' => array( |
| 93 |
'view', |
| 94 |
'edit', |
| 95 |
), |
| 96 |
'readonly' => false, |
| 97 |
'default' => '', |
| 98 |
'type' => 'string', |
| 99 |
), |
| 100 |
'deliveryDayDate' => array( |
| 101 |
'description' => 'Selected delivery day date for PostNL', |
| 102 |
'context' => array( |
| 103 |
'view', |
| 104 |
'edit', |
| 105 |
), |
| 106 |
'readonly' => false, |
| 107 |
'default' => '', |
| 108 |
'type' => 'string', |
| 109 |
), |
| 110 |
'deliveryDayFrom' => array( |
| 111 |
'description' => 'Delivery start time for PostNL delivery day', |
| 112 |
'context' => array( |
| 113 |
'view', |
| 114 |
'edit', |
| 115 |
), |
| 116 |
'readonly' => false, |
| 117 |
'default' => '', |
| 118 |
'type' => 'string', |
| 119 |
), |
| 120 |
'deliveryDayTo' => array( |
| 121 |
'description' => 'Delivery end time for PostNL delivery day', |
| 122 |
'context' => array( |
| 123 |
'view', |
| 124 |
'edit', |
| 125 |
), |
| 126 |
'readonly' => false, |
| 127 |
'default' => '', |
| 128 |
'type' => 'string', |
| 129 |
), |
| 130 |
'deliveryDayPrice' => array( |
| 131 |
'description' => 'Price for the selected PostNL delivery time', |
| 132 |
'context' => array( |
| 133 |
'view', |
| 134 |
'edit', |
| 135 |
), |
| 136 |
'readonly' => false, |
| 137 |
'type' => 'string', |
| 138 |
), |
| 139 |
'deliveryDayType' => array( |
| 140 |
'description' => 'Type of delivery (Morning, Evening, etc.) for PostNL delivery day', |
| 141 |
'context' => array( |
| 142 |
'view', |
| 143 |
'edit', |
| 144 |
), |
| 145 |
'readonly' => false, |
| 146 |
'default' => '', |
| 147 |
'type' => 'string', |
| 148 |
), |
| 149 |
'dropoffPoints' => array( |
| 150 |
'description' => 'Selected drop-off point identifier', |
| 151 |
'context' => array( |
| 152 |
'view', |
| 153 |
'edit', |
| 154 |
), |
| 155 |
'readonly' => false, |
| 156 |
'default' => '', |
| 157 |
'type' => 'string', |
| 158 |
), |
| 159 |
'dropoffPointsAddressCompany' => array( |
| 160 |
'description' => 'Company name of the drop-off point', |
| 161 |
'context' => array( |
| 162 |
'view', |
| 163 |
'edit', |
| 164 |
), |
| 165 |
'readonly' => false, |
| 166 |
'default' => '', |
| 167 |
'type' => 'string', |
| 168 |
), |
| 169 |
'dropoffPointsAddress1' => array( |
| 170 |
'description' => 'Address line 1 of the drop-off point', |
| 171 |
'context' => array( |
| 172 |
'view', |
| 173 |
'edit', |
| 174 |
), |
| 175 |
'readonly' => false, |
| 176 |
'default' => '', |
| 177 |
'type' => 'string', |
| 178 |
), |
| 179 |
'dropoffPointsAddress2' => array( |
| 180 |
'description' => 'Address line 2 of the drop-off point', |
| 181 |
'context' => array( |
| 182 |
'view', |
| 183 |
'edit', |
| 184 |
), |
| 185 |
'readonly' => false, |
| 186 |
'default' => '', |
| 187 |
'type' => array( 'number', 'string' ), |
| 188 |
), |
| 189 |
'dropoffPointsCity' => array( |
| 190 |
'description' => 'City of the drop-off point', |
| 191 |
'context' => array( |
| 192 |
'view', |
| 193 |
'edit', |
| 194 |
), |
| 195 |
'readonly' => false, |
| 196 |
'default' => '', |
| 197 |
'type' => 'string', |
| 198 |
), |
| 199 |
'dropoffPointsPostcode' => array( |
| 200 |
'description' => 'Postcode of the drop-off point', |
| 201 |
'context' => array( |
| 202 |
'view', |
| 203 |
'edit', |
| 204 |
), |
| 205 |
'readonly' => false, |
| 206 |
'default' => '', |
| 207 |
'type' => 'string', |
| 208 |
), |
| 209 |
'dropoffPointsCountry' => array( |
| 210 |
'description' => 'Country of the drop-off point', |
| 211 |
'context' => array( |
| 212 |
'view', |
| 213 |
'edit', |
| 214 |
), |
| 215 |
'readonly' => false, |
| 216 |
'default' => '', |
| 217 |
'type' => 'string', |
| 218 |
), |
| 219 |
'dropoffPointsPartnerID' => array( |
| 220 |
'description' => 'Partner ID of the drop-off point', |
| 221 |
'context' => array( |
| 222 |
'view', |
| 223 |
'edit', |
| 224 |
), |
| 225 |
'readonly' => false, |
| 226 |
'default' => '', |
| 227 |
'type' => 'string', |
| 228 |
), |
| 229 |
'dropoffPointsDate' => array( |
| 230 |
'description' => 'Date of the drop-off point selection', |
| 231 |
'context' => array( |
| 232 |
'view', |
| 233 |
'edit', |
| 234 |
), |
| 235 |
'readonly' => false, |
| 236 |
'default' => '', |
| 237 |
'type' => 'string', |
| 238 |
), |
| 239 |
'dropoffPointsTime' => array( |
| 240 |
'description' => 'Time of the drop-off point selection', |
| 241 |
'context' => array( |
| 242 |
'view', |
| 243 |
'edit', |
| 244 |
), |
| 245 |
'readonly' => false, |
| 246 |
'default' => '', |
| 247 |
'type' => 'string', |
| 248 |
), |
| 249 |
'dropoffPointsType' => array( |
| 250 |
'description' => 'Type of the drop-off point selection', |
| 251 |
'context' => array( |
| 252 |
'view', |
| 253 |
'edit', |
| 254 |
), |
| 255 |
'readonly' => false, |
| 256 |
'default' => '', |
| 257 |
'type' => 'string', |
| 258 |
), |
| 259 |
'dropoffPointsDistance' => array( |
| 260 |
'description' => 'Distance to the drop-off point', |
| 261 |
'context' => array( |
| 262 |
'view', |
| 263 |
'edit', |
| 264 |
), |
| 265 |
'readonly' => false, |
| 266 |
'default' => 0.0, |
| 267 |
'nullable' => true, |
| 268 |
'type' => array( 'string', 'number', 'null' ), |
| 269 |
), |
| 270 |
); |
| 271 |
return $schema; |
| 272 |
} |
| 273 |
} |
| 274 |
|