eksrelay_api.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. <?php
  2. /**
  3. * Plugin Name: EKSRelay API
  4. * Description: Dedykowane REST API dla EKSRelay – get_car_data i get_product_compatibility.
  5. * Otwarte endpointy REST (bez autoryzacji) – odpowiedniki nopriv AJAX.
  6. * Wdróż ten plik na serwer WP jako: /wp-content/mu-plugins/eksrelay_api.php
  7. * Version: 1.0.0
  8. */
  9. if ( ! defined( 'ABSPATH' ) ) {
  10. exit;
  11. }
  12. // ═══════════════════════════════════════════════════════════════════
  13. // Rejestracja tras REST
  14. // ═══════════════════════════════════════════════════════════════════
  15. add_action( 'rest_api_init', function () {
  16. register_rest_route( 'eksrelay/v1', '/car-data', [
  17. 'methods' => 'POST',
  18. 'callback' => 'eksrelay_car_data',
  19. 'permission_callback' => '__return_true',
  20. ] );
  21. register_rest_route( 'eksrelay/v1', '/product-compatibility', [
  22. 'methods' => 'POST',
  23. 'callback' => 'eksrelay_product_compatibility',
  24. 'permission_callback' => '__return_true',
  25. ] );
  26. /**
  27. * GET /wp-json/eksrelay/v1/shipping-costs?zone_id=X&currency=EUR
  28. *
  29. * Returns shipping methods for a zone with costs in the requested currency.
  30. * WCML stores per-currency costs in wp_options under the key
  31. * woocommerce_{method_id}_{instance_id}_settings as cost_EUR, cost_CZK, etc.
  32. */
  33. register_rest_route( 'eksrelay/v1', '/shipping-costs', [
  34. 'methods' => 'GET',
  35. 'callback' => 'eksrelay_shipping_costs',
  36. 'permission_callback' => '__return_true',
  37. ] );
  38. } );
  39. // ═══════════════════════════════════════════════════════════════════
  40. // Handler: POST /wp-json/eksrelay/v1/car-data
  41. //
  42. // Body JSON: { car_year, car_brand?, car_model?, car_engine? }
  43. // ═══════════════════════════════════════════════════════════════════
  44. function eksrelay_car_data( WP_REST_Request $request ) {
  45. $body = $request->get_json_params() ?: [];
  46. if ( empty( $body['car_year'] ) ) {
  47. return [ 'error' => 'car_year jest wymagany' ];
  48. }
  49. $car_year = intval( sanitize_text_field( $body['car_year'] ) );
  50. $has_brand = ! empty( $body['car_brand'] );
  51. $has_model = ! empty( $body['car_model'] );
  52. $car_brand = false;
  53. $car_model = false;
  54. if ( ! $has_brand && ! $has_model ) {
  55. return [ 'error' => 'car_brand lub car_model jest wymagany' ];
  56. }
  57. // ── Tylko brand, bez modelu ──────────────────────────────────────
  58. if ( $has_brand && ! $has_model ) {
  59. $car_brand = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_brand'] ) ) );
  60. $termIds = get_terms( [ 'name__like' => $car_brand, 'parent' => 0, 'fields' => 'ids' ] );
  61. if ( count( $termIds ) === 1 ) {
  62. $car_models = eksrelay_models_for_brand_year( $termIds, $car_year );
  63. return [ 'error' => 'options', 'field' => 'car_model', 'options' => $car_models, 'filtered' => false ];
  64. }
  65. return [ 'error' => 'options', 'field' => 'car_brand', 'options' => eksrelay_all_brands(), 'filtered' => false ];
  66. }
  67. // ── Tylko model, bez brandu ─────────────────────────────────────
  68. if ( $has_model && ! $has_brand ) {
  69. $car_model = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_model'] ) ) );
  70. $termIds = get_terms( [ 'name__like' => $car_model, 'fields' => 'ids' ] );
  71. // exact match → auto-wybierz
  72. foreach ( $termIds as $tid ) {
  73. $term = get_term_by( 'id', $tid, 'car_model' );
  74. if ( $term && eksrelay_replace_ascii( $term->name ) === $car_model ) {
  75. $termIds = [ $term->term_id ];
  76. break;
  77. }
  78. }
  79. if ( count( $termIds ) === 1 ) {
  80. $term = get_term( $termIds[0] );
  81. if ( ! is_wp_error( $term ) && $term->parent !== 0 ) {
  82. $parent = get_term( $term->parent );
  83. if ( ! is_wp_error( $parent ) ) {
  84. $car_brand = $parent->name;
  85. }
  86. } else {
  87. return [ 'error' => 'nie znaleziono takiego modelu', 'car_model' => $car_model ];
  88. }
  89. } else {
  90. $models = [];
  91. foreach ( $termIds as $tid ) {
  92. $t = get_term_by( 'id', $tid, 'car_model' );
  93. if ( $t ) {
  94. $models[] = $t->name;
  95. }
  96. }
  97. if ( $models ) {
  98. return [ 'error' => 'options', 'field' => 'car_model', 'options' => $models, 'filtered' => true ];
  99. }
  100. return [ 'error' => 'options', 'field' => 'car_brand', 'options' => eksrelay_all_brands(), 'filtered' => false ];
  101. }
  102. }
  103. // ── Mamy oba ────────────────────────────────────────────────────
  104. if ( ! $car_brand ) {
  105. $car_brand = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_brand'] ) ) );
  106. }
  107. if ( ! $car_model ) {
  108. $car_model = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_model'] ) ) );
  109. }
  110. $car_engine = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_engine'] ?? '' ) ) );
  111. $termIds = get_terms( [ 'name__like' => $car_brand, 'fields' => 'ids' ] );
  112. $termIds2 = get_terms( [ 'name__like' => $car_model, 'fields' => 'ids' ] );
  113. if ( ! count( $termIds ) ) {
  114. return [ 'error' => 'options', 'field' => 'car_brand', 'options' => eksrelay_all_brands(), 'filtered' => false ];
  115. }
  116. if ( ! count( $termIds2 ) ) {
  117. if ( count( $termIds ) === 1 ) {
  118. $car_models = eksrelay_models_for_brand_year( $termIds, $car_year );
  119. return [ 'error' => 'options', 'field' => 'car_model', 'options' => $car_models, 'filtered' => false ];
  120. }
  121. return [ 'error' => 'options', 'field' => 'car_brand', 'options' => eksrelay_all_brands(), 'filtered' => false ];
  122. }
  123. $query = new WP_Query( [
  124. 'post_type' => 'car',
  125. 'posts_per_page' => -1,
  126. 'tax_query' => [
  127. 'relation' => 'AND',
  128. [ 'taxonomy' => 'car_model', 'field' => 'id', 'terms' => $termIds ],
  129. [ 'taxonomy' => 'car_model', 'field' => 'id', 'terms' => $termIds2 ],
  130. [ 'taxonomy' => 'car_production_year', 'field' => 'slug', 'terms' => (string) $car_year ],
  131. ],
  132. ] );
  133. $cars = $query->get_posts();
  134. $engine_info = eksrelay_get_engines_info( $cars, true, $car_year );
  135. if ( ! $cars ) {
  136. return [
  137. 'error' => 'nie znaleziono auta, ' . get_option(
  138. 'aiac_tool_get_car_data_no_car',
  139. 'Orientacyjnie do 1994 roku włącznie powinien pasować gaz R12, dla aut 1995-2016 gaz R134A a dla aut od 2016 roku gaz R1234YF'
  140. ),
  141. 'car_brand' => $car_brand,
  142. 'car_model' => $car_model,
  143. ];
  144. }
  145. $engine_names = array_column( $engine_info, 'name' );
  146. if ( count( $engine_info ) === 1 ) {
  147. $car_engine = $engine_info[0]['name'];
  148. }
  149. if ( count( $engine_info ) > 1 && ! $car_engine ) {
  150. return [ 'error' => 'options', 'field' => 'car_engine', 'options' => $engine_names, 'filtered' => false ];
  151. }
  152. foreach ( $engine_info as $eng ) {
  153. if ( strtolower( trim( $eng['name'] ) ) !== strtolower( trim( $car_engine ) ) ) {
  154. continue;
  155. }
  156. $car_id = intval( $eng['id'] );
  157. $maybe_diff = get_field( 'custom_ac', $car_id );
  158. $gas_types = array_unique( array_filter( [
  159. get_field( 'ac_gas_type', $car_id ),
  160. $maybe_diff ? get_field( 'ac_gas_type_2', $car_id ) : '',
  161. ] ) );
  162. $year_term = get_terms( [ 'taxonomy' => 'car_production_year', 'name__like' => $car_year ] );
  163. $brand_term = $termIds ? get_term( $termIds[0] ) : false;
  164. $model_term = $termIds2 ? get_term( $termIds2[0] ) : false;
  165. $data = [
  166. 'success' => true,
  167. 'selected_car' => [
  168. 'ek_ac_gas_type' => implode( ',', $gas_types ),
  169. 'ek_brand_id' => $termIds[0] ?? '',
  170. 'ek_brand' => ( $brand_term && ! is_wp_error( $brand_term ) ) ? $brand_term->name : $car_brand,
  171. 'ek_car_id' => $car_id,
  172. 'ek_engine_type' => $eng['name'],
  173. 'ek_info' => '',
  174. 'ek_model_id' => $termIds2[0] ?? '',
  175. 'ek_model' => ( $model_term && ! is_wp_error( $model_term ) ) ? $model_term->name : $car_model,
  176. 'ek_year_id' => ( ! is_wp_error( $year_term ) && $year_term ) ? $year_term[0]->term_id : '',
  177. 'ek_year' => $car_year,
  178. ],
  179. 'shop_url' => $eng['url'],
  180. 'adapters' => get_field( 'adapters', $car_id ) ?: false,
  181. 'ac_gas_type' => get_field( 'ac_gas_type', $car_id ),
  182. 'ac_gas_amount' => ( get_field( 'ac_gas_amount', $car_id ) ?: '-' ) . ' g',
  183. 'ac_ports_count' => intval( get_field( 'ac_ports_count', $car_id ) ),
  184. 'ac_oil' => get_field( 'ac_oil', $car_id ),
  185. 'ac_oil_amount' => ( get_field( 'ac_oil_amount', $car_id ) ?: '-' ) . ' cm3',
  186. 'ac_clutch' => get_field( 'ac_clutch', $car_id ),
  187. 'maybe_different_layout' => $maybe_diff,
  188. ];
  189. if ( $maybe_diff ) {
  190. $data['different_layout_info'] = get_field( 'custom_ac_info', $car_id ) ?: '';
  191. $data['different_layout_adapters'] = get_field( 'adapters_2', $car_id ) ?: false;
  192. $data['different_layout_ac_gas_type'] = get_field( 'ac_gas_type_2', $car_id );
  193. $data['different_layout_ac_gas_amount'] = ( get_field( 'ac_gas_amount_2', $car_id ) ?: '-' ) . ' g';
  194. $data['different_layout_ac_ports_count'] = get_field( 'ac_ports_count_2', $car_id );
  195. $data['different_layout_ac_oil'] = get_field( 'ac_oil_2', $car_id );
  196. $data['different_layout_ac_oil_amount'] = ( get_field( 'ac_oil_amount_2', $car_id ) ?: '-' ) . ' cm3';
  197. $data['different_layout_ac_clutch'] = get_field( 'ac_clutch_2', $car_id );
  198. }
  199. return $data;
  200. }
  201. // podany silnik nie pasuje do żadnego na liście → pokaż opcje
  202. return [ 'error' => 'options', 'field' => 'car_engine', 'options' => $engine_names, 'filtered' => false ];
  203. }
  204. // ═══════════════════════════════════════════════════════════════════
  205. // Handler: POST /wp-json/eksrelay/v1/product-compatibility
  206. //
  207. // Body JSON: { car_year, car_brand?, car_model?, car_engine?, product_name? }
  208. // ═══════════════════════════════════════════════════════════════════
  209. function eksrelay_product_compatibility( WP_REST_Request $request ) {
  210. $body = $request->get_json_params() ?: [];
  211. if ( empty( $body['car_year'] ) ) {
  212. return [ 'error' => 'car_year jest wymagany' ];
  213. }
  214. $lang = sanitize_text_field( $body['lang'] ?? 'pl' );
  215. $currency = strtoupper( sanitize_text_field( $body['currency'] ?? 'PLN' ) );
  216. $car_year = intval( sanitize_text_field( $body['car_year'] ) );
  217. $has_brand = ! empty( $body['car_brand'] );
  218. $has_model = ! empty( $body['car_model'] );
  219. $car_brand = false;
  220. $car_model = false;
  221. // ── Tylko brand ─────────────────────────────────────────────────
  222. if ( $has_brand && ! $has_model ) {
  223. $car_brand = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_brand'] ) ) );
  224. $termIds = get_terms( [ 'name__like' => $car_brand, 'parent' => 0, 'fields' => 'ids' ] );
  225. if ( count( $termIds ) === 1 ) {
  226. $car_models = eksrelay_models_for_brand_year( $termIds, $car_year );
  227. return [ 'error' => 'options', 'field' => 'car_model', 'options' => $car_models, 'filtered' => false ];
  228. }
  229. return [ 'error' => 'options', 'field' => 'car_brand', 'options' => eksrelay_all_brands(), 'filtered' => false ];
  230. }
  231. // ── Tylko model ─────────────────────────────────────────────────
  232. if ( $has_model && ! $has_brand ) {
  233. $car_model = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_model'] ) ) );
  234. $termIds = get_terms( [ 'name__like' => $car_model, 'fields' => 'ids' ] );
  235. if ( count( $termIds ) === 1 ) {
  236. $term = get_term( $termIds[0] );
  237. if ( ! is_wp_error( $term ) && $term->parent !== 0 ) {
  238. $parent = get_term( $term->parent );
  239. if ( ! is_wp_error( $parent ) ) {
  240. $car_brand = $parent->name;
  241. }
  242. } else {
  243. return [ 'error' => 'nie znaleziono takiego modelu', 'car_model' => $car_model ];
  244. }
  245. } else {
  246. $models = [];
  247. foreach ( $termIds as $tid ) {
  248. $t = get_term_by( 'id', $tid, 'car_model' );
  249. if ( $t ) {
  250. $models[] = $t->name;
  251. }
  252. }
  253. if ( $models ) {
  254. return [ 'error' => 'options', 'field' => 'car_model', 'options' => $models, 'filtered' => true ];
  255. }
  256. return [ 'error' => 'options', 'field' => 'car_brand', 'options' => eksrelay_all_brands(), 'filtered' => false ];
  257. }
  258. }
  259. if ( ! $has_brand && ! $has_model ) {
  260. return [ 'error' => 'car_brand lub car_model jest wymagany' ];
  261. }
  262. if ( ! $car_brand ) {
  263. $car_brand = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_brand'] ) ) );
  264. }
  265. if ( ! $car_model ) {
  266. $car_model = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_model'] ) ) );
  267. }
  268. $car_engine = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_engine'] ?? '' ) ) );
  269. $car_engine_index = isset( $body['car_engine_index'] ) ? intval( $body['car_engine_index'] ) : -1;
  270. $product_name = eksrelay_replace_ascii( trim( sanitize_text_field( $body['product_name'] ?? '' ) ) );
  271. $product_id = intval( $body['product_id'] ?? 0 );
  272. // ── Znajdź produkt ──────────────────────────────────────────────
  273. $product = false;
  274. if ( $product_id > 0 ) {
  275. $p = wc_get_product( $product_id );
  276. if ( $p && ! is_wp_error( $p ) ) {
  277. $product = $p;
  278. }
  279. } elseif ( $product_name ) {
  280. $products = eksrelay_search_products_by_name( $product_name );
  281. if ( count( $products ) > 1 ) {
  282. // exact match → auto-wybierz
  283. foreach ( $products as $p ) {
  284. if ( eksrelay_replace_ascii( strtolower( trim( $p->get_title() ) ) ) === strtolower( $product_name ) ) {
  285. $product = $p;
  286. break;
  287. }
  288. }
  289. if ( ! $product ) {
  290. return [
  291. 'error' => 'options',
  292. 'field' => 'product_name',
  293. 'options' => array_map( fn( $p ) => [ 'id' => $p->get_id(), 'title' => $p->get_title() ], $products ),
  294. 'filtered' => true,
  295. ];
  296. }
  297. } elseif ( count( $products ) === 1 ) {
  298. $product = $products[0];
  299. }
  300. }
  301. // ── Znajdź auto ─────────────────────────────────────────────────
  302. $termIds = get_terms( [ 'name__like' => $car_brand, 'fields' => 'ids' ] );
  303. $termIds2 = get_terms( [ 'name__like' => $car_model, 'fields' => 'ids' ] );
  304. // Uwaga: celowo bez filtra roku – engine_info filtruje rok wewnętrznie
  305. $query = new WP_Query( [
  306. 'post_type' => 'car',
  307. 'posts_per_page' => -1,
  308. 'tax_query' => [
  309. 'relation' => 'AND',
  310. [ 'taxonomy' => 'car_model', 'field' => 'id', 'terms' => $termIds ],
  311. [ 'taxonomy' => 'car_model', 'field' => 'id', 'terms' => $termIds2 ],
  312. ],
  313. ] );
  314. $cars = $query->get_posts();
  315. $engine_info = eksrelay_get_engines_info( $cars, false, $car_year );
  316. if ( ! $cars ) {
  317. return [ 'error' => 'nie znaleziono takiego samochodu', 'car_brand' => $car_brand, 'car_model' => $car_model ];
  318. }
  319. // If year filter returns nothing, fall back to all engines for this model (year=0 disables filter).
  320. // This handles cases where the user provides an approximate or incorrect year.
  321. if ( empty( $engine_info ) && $car_year ) {
  322. $engine_info = eksrelay_get_engines_info( $cars, false, 0 );
  323. }
  324. $engine_names = array_column( $engine_info, 'name' );
  325. if ( count( $engine_info ) === 1 ) {
  326. $car_engine = $engine_info[0]['name'];
  327. }
  328. if ( count( $engine_info ) > 1 && ! $car_engine && $car_engine_index < 0 ) {
  329. // Return options as {index, name} objects so the caller can select by numeric index,
  330. // avoiding string-matching issues (e.g. LLM stripping engine-type prefixes like "B ", "D ").
  331. $engine_options = array_map(
  332. fn( $i, $eng ) => [ 'index' => $i, 'name' => $eng['name'] ],
  333. array_keys( $engine_info ),
  334. array_values( $engine_info )
  335. );
  336. return [ 'error' => 'options', 'field' => 'car_engine', 'options' => $engine_options, 'filtered' => false ];
  337. }
  338. foreach ( $engine_info as $idx => $eng ) {
  339. // Prefer index-based selection (unambiguous) over name-based (fragile string match).
  340. if ( $car_engine_index >= 0 ) {
  341. if ( $idx !== $car_engine_index ) continue;
  342. } else {
  343. if ( strtolower( trim( $eng['name'] ) ) !== strtolower( trim( $car_engine ) ) ) continue;
  344. }
  345. $engine_gas = array_values( array_filter( [ $eng['ac_gas_type'] ?? '', $eng['ac_gas_type_2'] ?? '' ] ) );
  346. $engine_adapters = array_values( array_filter( [ $eng['adapters'] ?? '', $eng['adapters_2'] ?? '' ] ) );
  347. if ( ! $engine_gas ) $engine_gas = [ 'no-gas' ];
  348. if ( ! $engine_adapters ) $engine_adapters = [ 'no' ];
  349. if ( $product ) {
  350. // ── Sprawdzenie kompatybilności konkretnego produktu ─────────
  351. $attributes = eksrelay_product_attributes( $product );
  352. foreach ( $attributes as $k => $arr ) {
  353. $attributes[ $k ] = array_map( fn( $slug ) => str_replace( "-{$lang}", '', $slug ), $arr );
  354. }
  355. if ( ! isset( $attributes['pa_rg'] ) || empty( $attributes['pa_rg'] ) ) $attributes['pa_rg'] = [ 'no-gas' ];
  356. if ( ! isset( $attributes['pa_ra'] ) || empty( $attributes['pa_ra'] ) ) $attributes['pa_ra'] = [ 'no' ];
  357. $gas_fit = count( array_intersect( $engine_gas, $attributes['pa_rg'] ) ) > 0;
  358. $adapter_fit = count( array_intersect( $engine_adapters, $attributes['pa_ra'] ) ) > 0;
  359. return [
  360. 'success' => true,
  361. 'fit' => $gas_fit && $adapter_fit,
  362. 'gas_fit' => $gas_fit,
  363. 'adapter_fit' => $adapter_fit,
  364. 'selected_engine' => $eng,
  365. 'selected_product' => [
  366. 'id' => $product->get_id(),
  367. 'title' => $product->get_title(),
  368. 'type' => $product->get_type(),
  369. 'attributes' => $attributes,
  370. ],
  371. ];
  372. }
  373. // ── Brak konkretnego produktu – lista wszystkich kompatybilnych ─
  374. // Switch WPML language so product titles and WCML prices are returned
  375. // in the requested language/currency. Restored in finally block below.
  376. if ( $lang && $lang !== 'pl' ) {
  377. do_action( 'wpml_switch_language', $lang );
  378. }
  379. try {
  380. $all_products = wc_get_products( [
  381. 'status' => 'publish',
  382. 'visibility' => 'visible',
  383. 'limit' => -1,
  384. 'orderby' => 'name',
  385. ] );
  386. $compatible = [];
  387. foreach ( $all_products as $p ) {
  388. $attrs = eksrelay_product_attributes( $p );
  389. foreach ( $attrs as $k => $arr ) {
  390. $attrs[ $k ] = array_map( fn( $slug ) => str_replace( "-{$lang}", '', $slug ), $arr );
  391. }
  392. $p_gas = ( isset( $attrs['pa_rg'] ) && ! empty( $attrs['pa_rg'] ) ) ? $attrs['pa_rg'] : [ 'no-gas' ];
  393. $p_adapters = ( isset( $attrs['pa_ra'] ) && ! empty( $attrs['pa_ra'] ) ) ? $attrs['pa_ra'] : [ 'no' ];
  394. if (
  395. count( array_intersect( $engine_gas, $p_gas ) ) > 0 &&
  396. count( array_intersect( $engine_adapters, $p_adapters ) ) > 0
  397. ) {
  398. $compatible[] = [
  399. 'id' => $p->get_id(),
  400. 'title' => $p->get_title(),
  401. 'price' => $p->get_price(),
  402. 'permalink' => get_permalink( $p->get_id() ),
  403. ];
  404. }
  405. }
  406. } finally {
  407. if ( $lang && $lang !== 'pl' ) {
  408. do_action( 'wpml_switch_language', null );
  409. }
  410. }
  411. return [
  412. 'success' => true,
  413. 'compatible_products' => $compatible,
  414. 'selected_engine' => $eng,
  415. ];
  416. }
  417. // No engine matched — return options again so user can pick
  418. $engine_options = array_map(
  419. fn( $i, $eng ) => [ 'index' => $i, 'name' => $eng['name'] ],
  420. array_keys( $engine_info ),
  421. array_values( $engine_info )
  422. );
  423. return [ 'error' => 'options', 'field' => 'car_engine', 'options' => $engine_options, 'filtered' => false ];
  424. }
  425. // ═══════════════════════════════════════════════════════════════════
  426. // Pomocnicze funkcje wewnętrzne
  427. // ═══════════════════════════════════════════════════════════════════
  428. /**
  429. * Zwraca listę modeli dla danej marki (termIds) i roku produkcji.
  430. */
  431. function eksrelay_models_for_brand_year( array $termIds, int $car_year ): array {
  432. $query = new WP_Query( [
  433. 'post_type' => 'car',
  434. 'posts_per_page' => -1,
  435. 'tax_query' => [
  436. 'relation' => 'AND',
  437. [ 'taxonomy' => 'car_model', 'field' => 'id', 'terms' => $termIds ],
  438. [ 'taxonomy' => 'car_production_year', 'field' => 'slug', 'terms' => [ (string) $car_year ] ],
  439. ],
  440. ] );
  441. $car_models = [];
  442. foreach ( $query->get_posts() as $car ) {
  443. foreach ( wp_get_post_terms( $car->ID, 'car_model' ) as $term ) {
  444. if ( ! in_array( $term->name, $car_models, true ) && $term->parent !== 0 ) {
  445. $car_models[] = $term->name;
  446. }
  447. }
  448. }
  449. return $car_models;
  450. }
  451. /**
  452. * Zwraca listę wszystkich marek aut (terminy car_model z parent=0).
  453. */
  454. function eksrelay_all_brands(): array {
  455. return (array) get_terms( [
  456. 'taxonomy' => 'car_model',
  457. 'parent' => 0,
  458. 'fields' => 'names',
  459. 'hide_empty' => true,
  460. ] );
  461. }
  462. /**
  463. * Zwraca info o silnikach dla podanych postów 'car'.
  464. *
  465. * Parametr $prefiltered:
  466. * true – WP_Query już przefiltrował po roku przez taxonomy (get_car_data)
  467. * false – rok musi być sprawdzony wewnętrznie przez pola ACF year_from / year_to (get_product_compatibility)
  468. *
  469. * Nazwa silnika jest budowana z pól ACF: engine_type, engine_size (ccm), engine_power_kw, engine_power_km.
  470. * Format: "B 1.8 - 141 kW / 192 KM"
  471. *
  472. * Zakres roku pochodzi z pól ACF year_from / year_to (przechowywane jako term_id taksonomii car_production_year).
  473. *
  474. * Dodatkowe warianty silnika mogą być przechowywane w ACF repeaterze 'engines' na poście car.
  475. */
  476. function eksrelay_get_engines_info( array $cars, bool $prefiltered, int $car_year ): array {
  477. $engines = [];
  478. $seen = [];
  479. foreach ( $cars as $car ) {
  480. $car_id = $car->ID;
  481. // ── Filtrowanie zakresu roku przez pola ACF year_from / year_to ─
  482. if ( $car_year ) {
  483. $year_from_id = get_field( 'year_from', $car_id );
  484. $year_to_id = get_field( 'year_to', $car_id );
  485. $year_from = 0;
  486. $year_to = 9999;
  487. if ( $year_from_id ) {
  488. $t = get_term( (int) $year_from_id, 'car_production_year' );
  489. if ( $t && ! is_wp_error( $t ) ) {
  490. $year_from = (int) $t->slug;
  491. }
  492. }
  493. if ( $year_to_id ) {
  494. $t = get_term( (int) $year_to_id, 'car_production_year' );
  495. if ( $t && ! is_wp_error( $t ) ) {
  496. $year_to = (int) $t->slug;
  497. }
  498. }
  499. if ( $car_year < $year_from || $car_year > $year_to ) {
  500. continue;
  501. }
  502. }
  503. // ── Budowanie nazwy silnika z pól ACF ────────────────────────────
  504. $engine_type = (string) ( get_field( 'engine_type', $car_id ) ?: '' );
  505. $engine_size_cc = (float) ( get_field( 'engine_size', $car_id ) ?: 0 );
  506. $power_kw = (string) ( get_field( 'engine_power_kw', $car_id ) ?: '' );
  507. $power_km = (string) ( get_field( 'engine_power_km', $car_id ) ?: '' );
  508. $engine_size = number_format( $engine_size_cc / 1000, 1 );
  509. $name = "{$engine_type} {$engine_size} - {$power_kw} kW / {$power_km} KM";
  510. // ── Typ gazu i adaptery ──────────────────────────────────────────
  511. $ac_gas_type = (string) ( get_field( 'ac_gas_type', $car_id ) ?: '' );
  512. $ac_gas_type_2 = (string) ( get_field( 'ac_gas_type_2', $car_id ) ?: '' );
  513. $adapters_raw = get_field( 'adapters', $car_id );
  514. $adapters_raw2 = get_field( 'adapters_2', $car_id );
  515. $adapters = is_array( $adapters_raw ) ? ( implode( ',', $adapters_raw ) ?: 'no' ) : (string) ( $adapters_raw ?: 'no' );
  516. $adapters_2 = is_array( $adapters_raw2 ) ? implode( ',', $adapters_raw2 ) : (string) ( $adapters_raw2 ?: '' );
  517. // ── Korekty gazu na podstawie roku (sanity check) ────────────────
  518. if ( $car_year ) {
  519. if ( $car_year <= 1994 && $ac_gas_type === '' ) {
  520. $ac_gas_type = 'r12';
  521. } elseif ( $car_year >= 2017 && $ac_gas_type === '' ) {
  522. $ac_gas_type = 'r1234yf';
  523. } elseif ( $car_year >= 1995 && $car_year <= 2016 && $ac_gas_type === '' ) {
  524. $ac_gas_type = 'r134a';
  525. }
  526. }
  527. // ── Główny silnik ────────────────────────────────────────────────
  528. $key = mb_strtolower( $name );
  529. if ( ! isset( $seen[ $key ] ) ) {
  530. $seen[ $key ] = true;
  531. $engines[] = [
  532. 'name' => $name,
  533. 'id' => $car_id,
  534. 'url' => get_permalink( $car_id ),
  535. 'ac_gas_type' => $ac_gas_type,
  536. 'ac_gas_type_2' => $ac_gas_type_2,
  537. 'adapters' => $adapters,
  538. 'adapters_2' => $adapters_2,
  539. ];
  540. }
  541. // ── ACF repeater 'engines' – dodatkowe warianty silnika ──────────
  542. $extra_engines = get_field( 'engines', $car_id );
  543. if ( is_array( $extra_engines ) ) {
  544. foreach ( $extra_engines as $extra ) {
  545. $e_type = (string) ( $extra['engine_type'] ?? '' );
  546. $e_size_cc = (float) ( $extra['engine_size'] ?? 0 );
  547. $e_kw = (string) ( $extra['engine_power_kw'] ?? '' );
  548. $e_km = (string) ( $extra['engine_power_km'] ?? '' );
  549. $e_size = number_format( $e_size_cc / 1000, 1 );
  550. $e_name = "{$e_type} {$e_size} - {$e_kw} kW / {$e_km} KM";
  551. $e_gas = (string) ( $extra['ac_gas_type'] ?? $ac_gas_type );
  552. $e_gas_2 = (string) ( $extra['ac_gas_type_2'] ?? $ac_gas_type_2 );
  553. $e_adp_r = $extra['adapters'] ?? null;
  554. $e_adp_r2 = $extra['adapters_2'] ?? null;
  555. $e_adp = is_array( $e_adp_r ) ? implode( ',', $e_adp_r ) : (string) ( $e_adp_r ?: $adapters );
  556. $e_adp_2 = is_array( $e_adp_r2 ) ? implode( ',', $e_adp_r2 ) : (string) ( $e_adp_r2 ?: $adapters_2 );
  557. $e_key = mb_strtolower( $e_name );
  558. if ( ! isset( $seen[ $e_key ] ) ) {
  559. $seen[ $e_key ] = true;
  560. $engines[] = [
  561. 'name' => $e_name,
  562. 'id' => $car_id,
  563. 'url' => get_permalink( $car_id ),
  564. 'ac_gas_type' => $e_gas,
  565. 'ac_gas_type_2' => $e_gas_2,
  566. 'adapters' => $e_adp,
  567. 'adapters_2' => $e_adp_2,
  568. ];
  569. }
  570. }
  571. }
  572. }
  573. return $engines;
  574. }
  575. /**
  576. * Zwraca atrybuty produktu WC jako [ slug_atrybutu => [ slug_wartości, ... ] ].
  577. * Używane do sprawdzania pa_rg (typ gazu) i pa_ra (adapter).
  578. *
  579. * Implementacja wzorowana na aiac_get_product_attributes_as_str_arr() z aiac_chat_api.php:
  580. * – dla wariantów (variation): [ $attr_val ] (pojedyncza wartość ze zmiennej produktowej)
  581. * – dla pozostałych typów: $attr_obj->get_slugs()
  582. */
  583. function eksrelay_product_attributes( WC_Product $product ): array {
  584. $result = [];
  585. $attributes = $product->get_attributes();
  586. if ( $product->is_type( 'variation' ) ) {
  587. foreach ( $attributes as $attr_key => $attr_val ) {
  588. $result[ $attr_key ] = [ $attr_val ];
  589. }
  590. } else {
  591. foreach ( $attributes as $attr_key => $attr_obj ) {
  592. $result[ $attr_key ] = $attr_obj->get_slugs();
  593. }
  594. }
  595. return $result;
  596. }
  597. /**
  598. * Wyszukuje produkty WooCommerce po fragmencie nazwy (fulltext search).
  599. */
  600. function eksrelay_search_products_by_name( string $product_name ): array {
  601. add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'eksrelay_wc_query_like', 10, 2 );
  602. $products = wc_get_products( [
  603. 'status' => 'publish',
  604. 'limit' => -1,
  605. 'like' => $product_name,
  606. ] );
  607. remove_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'eksrelay_wc_query_like', 10 );
  608. return $products;
  609. }
  610. function eksrelay_wc_query_like( array $query, array $query_vars ): array {
  611. if ( ! empty( $query_vars['like'] ) ) {
  612. $query['s'] = esc_attr( $query_vars['like'] );
  613. }
  614. return $query;
  615. }
  616. // ═══════════════════════════════════════════════════════════════════
  617. // Handler: GET /wp-json/eksrelay/v1/shipping-costs?zone_id=X&currency=EUR
  618. //
  619. // Returns shipping methods for a zone with costs in the requested currency.
  620. // WCML stores per-currency costs in wp_options:
  621. // woocommerce_{method_id}_{instance_id}_settings → cost_EUR, cost_CZK, etc.
  622. // ═══════════════════════════════════════════════════════════════════
  623. function eksrelay_shipping_costs( WP_REST_Request $request ) {
  624. $zone_id = intval( $request->get_param( 'zone_id' ) );
  625. $currency = strtoupper( sanitize_text_field( $request->get_param( 'currency' ) ?: 'PLN' ) );
  626. $zone = new WC_Shipping_Zone( $zone_id );
  627. $methods = $zone->get_shipping_methods( true ); // true = enabled only
  628. $result = [];
  629. foreach ( $methods as $method ) {
  630. $instance_id = $method->instance_id;
  631. $method_id = $method->id; // e.g. 'flat_rate', 'free_shipping'
  632. // WCML stores per-currency costs in wp_options
  633. $option_key = "woocommerce_{$method_id}_{$instance_id}_settings";
  634. $settings = get_option( $option_key, [] );
  635. // Base cost is the default (PLN) cost
  636. $base_cost = $settings['cost'] ?? '0';
  637. $cost_key = "cost_{$currency}";
  638. if ( $currency !== 'PLN' && isset( $settings[ $cost_key ] ) && $settings[ $cost_key ] !== '' ) {
  639. $cost = $settings[ $cost_key ];
  640. $cost_currency = $currency;
  641. } else {
  642. $cost = $base_cost;
  643. $cost_currency = 'PLN';
  644. }
  645. $result[] = [
  646. 'instance_id' => $instance_id,
  647. 'method_id' => $method_id,
  648. 'title' => $method->get_title(),
  649. 'cost' => $cost,
  650. 'cost_currency' => $cost_currency,
  651. ];
  652. }
  653. return [
  654. 'zone_id' => $zone_id,
  655. 'currency' => $currency,
  656. 'methods' => $result,
  657. ];
  658. }
  659. /**
  660. * Usuwa znaki diakrytyczne z ciągu znaków.
  661. * Mapowanie identyczne z replace_ascii() w aiac_chat_api.php.
  662. */
  663. function eksrelay_replace_ascii( string $string ): string {
  664. $map = [
  665. 'Š' => 'S', 'š' => 's', 'ë' => 'e', 'Ë' => 'E',
  666. 'ä' => 'a', 'Ä' => 'A', 'ö' => 'o', 'Ö' => 'O',
  667. 'ü' => 'u', 'Ü' => 'U', 'ß' => 'ss',
  668. 'ó' => 'o', 'Ó' => 'O', 'ł' => 'l', 'Ł' => 'L',
  669. 'ń' => 'n', 'Ń' => 'N', 'ć' => 'c', 'Ć' => 'C',
  670. 'ę' => 'e', 'Ę' => 'E', 'ź' => 'z', 'Ź' => 'Z',
  671. 'ż' => 'z', 'Ż' => 'Z', 'á' => 'a', 'Á' => 'A',
  672. 'č' => 'c', 'Č' => 'C', 'ď' => 'd', 'Ď' => 'D',
  673. 'é' => 'e', 'É' => 'E', 'ě' => 'e', 'Ě' => 'E',
  674. 'í' => 'i', 'Í' => 'I', 'ň' => 'n', 'Ň' => 'N',
  675. 'ř' => 'r', 'Ř' => 'R', 'ś' => 's', 'Ś' => 'S',
  676. 'ť' => 't', 'Ť' => 'T', 'ů' => 'u', 'Ů' => 'U',
  677. 'ý' => 'y', 'Ý' => 'Y', 'ą' => 'a', 'Ą' => 'A',
  678. 'ș' => 's', 'Ș' => 'S', 'î' => 'i', 'Î' => 'I',
  679. 'â' => 'a', 'Â' => 'A', 'ț' => 't', 'Ț' => 'T',
  680. 'ğ' => 'g', 'Ğ' => 'G', 'İ' => 'I', 'ı' => 'i',
  681. 'ç' => 'c', 'Ç' => 'C',
  682. ];
  683. return str_replace( array_keys( $map ), array_values( $map ), $string );
  684. }