|
|
@@ -40,6 +40,12 @@ add_action( 'rest_api_init', function () {
|
|
|
'callback' => 'eksrelay_shipping_costs',
|
|
|
'permission_callback' => '__return_true',
|
|
|
] );
|
|
|
+
|
|
|
+ register_rest_route( 'eksrelay/v1', '/shipping-eligibility', [
|
|
|
+ 'methods' => 'POST',
|
|
|
+ 'callback' => 'eksrelay_shipping_eligibility',
|
|
|
+ 'permission_callback' => '__return_true',
|
|
|
+ ] );
|
|
|
} );
|
|
|
|
|
|
// ═══════════════════════════════════════════════════════════════════
|
|
|
@@ -718,6 +724,71 @@ function eksrelay_wc_query_like( array $query, array $query_vars ): array {
|
|
|
// woocommerce_{method_id}_{instance_id}_settings → cost_EUR, cost_CZK, etc.
|
|
|
// ═══════════════════════════════════════════════════════════════════
|
|
|
|
|
|
+function eksrelay_shipping_eligibility( WP_REST_Request $request ) {
|
|
|
+ $body = $request->get_json_params() ?: [];
|
|
|
+ $country = strtoupper( sanitize_text_field( $body['country'] ?? $request->get_param( 'country' ) ?? '' ) );
|
|
|
+ $postcode = strtoupper( preg_replace( '/\s+/', '', sanitize_text_field( $body['postcode'] ?? $body['postalCode'] ?? $body['postal_code'] ?? $request->get_param( 'postcode' ) ?? '' ) ) );
|
|
|
+
|
|
|
+ if ( $country === '' || $postcode === '' ) {
|
|
|
+ return new WP_Error( 'eksrelay_missing_params', 'country and postcode are required', [ 'status' => 400 ] );
|
|
|
+ }
|
|
|
+
|
|
|
+ $blocked = false;
|
|
|
+ $matched = null;
|
|
|
+ $source = 'aiac_disable_dpd_using_postcode';
|
|
|
+
|
|
|
+ if ( function_exists( 'aiac_get_blocked_postcodes' ) && function_exists( 'aiac_is_postcode_blocked_for_dpd' ) ) {
|
|
|
+ $rules = aiac_get_blocked_postcodes();
|
|
|
+ $patterns = is_array( $rules ) && isset( $rules[ $country ] ) && is_array( $rules[ $country ] ) ? $rules[ $country ] : [];
|
|
|
+ foreach ( $patterns as $pattern ) {
|
|
|
+ if ( aiac_is_postcode_blocked_for_dpd( $postcode, [ $pattern ] ) ) {
|
|
|
+ $blocked = true;
|
|
|
+ $matched = (string) $pattern;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $source = 'fallback_es_ranges';
|
|
|
+ $fallback = [
|
|
|
+ 'ES' => [ '35000-35999', '38001-38999', '51001-51015', '52001-52006' ],
|
|
|
+ ];
|
|
|
+ foreach ( $fallback[ $country ] ?? [] as $pattern ) {
|
|
|
+ if ( eksrelay_postcode_matches_pattern( $postcode, $pattern ) ) {
|
|
|
+ $blocked = true;
|
|
|
+ $matched = $pattern;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'ok' => true,
|
|
|
+ 'eligible' => ! $blocked,
|
|
|
+ 'country' => $country,
|
|
|
+ 'postcode' => $postcode,
|
|
|
+ 'reason_code' => $blocked ? 'blocked_postcode' : 'eligible',
|
|
|
+ 'reason' => $blocked ? 'Nie wysyłamy do tego regionu/kodu pocztowego.' : 'Brak blokady dla tego kodu pocztowego w regułach sklepu.',
|
|
|
+ 'matched_rule' => $matched,
|
|
|
+ 'source' => $source,
|
|
|
+ ];
|
|
|
+}
|
|
|
+
|
|
|
+function eksrelay_postcode_matches_pattern( $postcode, $pattern ) {
|
|
|
+ $postcode_digits = preg_replace( '/\D/', '', (string) $postcode );
|
|
|
+ $pattern = strtoupper( preg_replace( '/\s+/', '', (string) $pattern ) );
|
|
|
+ if ( strpos( $pattern, '-' ) !== false ) {
|
|
|
+ list( $from, $to ) = array_map( 'trim', explode( '-', $pattern, 2 ) );
|
|
|
+ if ( ctype_digit( $from ) && ctype_digit( $to ) && $postcode_digits !== '' ) {
|
|
|
+ $pc_int = (int) $postcode_digits;
|
|
|
+ return $pc_int >= (int) $from && $pc_int <= (int) $to;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ( substr( $pattern, -1 ) === '*' ) {
|
|
|
+ return strpos( strtoupper( (string) $postcode ), rtrim( $pattern, '*' ) ) === 0;
|
|
|
+ }
|
|
|
+ return strtoupper( (string) $postcode ) === $pattern || $postcode_digits === preg_replace( '/\D/', '', $pattern );
|
|
|
+}
|
|
|
+
|
|
|
function eksrelay_shipping_costs( WP_REST_Request $request ) {
|
|
|
$zone_id = intval( $request->get_param( 'zone_id' ) );
|
|
|
$currency = strtoupper( sanitize_text_field( $request->get_param( 'currency' ) ?: 'PLN' ) );
|