function get_print_houses( $copies, $total_page_count, $book_size, $interior_print, $cover_print, $paper_weight_interior, $paper_weight_cover, $binding_method, $finishing_options, $delivery_country ) { // Normalization helper function normalize_input($value, $map) { if (empty($value)) { return null; } $value = strtolower(trim($value)); foreach ($map as $canonical => $variants) { foreach ($variants as $variant) { if (strpos($value, strtolower($variant)) !== false) { return $canonical; } } } return $value; } // Synonym maps $binding_method_map = [ 'Perfect bound' => ['perfect bound', 'softcover', 'soft cover'], 'Thread sewn' => ['thread sewn', 'hardcover', 'hard cover', 'sewn'], 'Wire-o' => ['wire-o', 'wire o', 'spiral', 'wiro'] ]; $finishing_map = [ 'Gloss lamination' => ['gloss', 'glossy', 'gloss lamination'], 'Matt lamination' => ['matte', 'matt', 'matt lamination'], 'None' => ['none', 'no finish', 'no lamination'] ]; $interior_print_map = [ '1/1 black' => ['black and white', '1/1 black', 'greyscale', 'bw', 'b/w'], '4/4 color' => ['color', 'colour', '4/4 color', 'full color', 'colour interior'] ]; $cover_print_map = [ '4/0 euro standard' => ['euro', '4/0 euro standard', '4/0'], '4/4 color' => ['color cover', '4/4 color', 'full color cover'] ]; $interior_paper_map = [ '135 gsm' => ['135 gsm', '135 gsm woodfree offset', 'woodfree offset 135'], '80 gsm' => ['80 gsm', '80gsm offset', 'offset 80 gsm'] ]; $cover_paper_map = [ '250 gsm' => ['250 gsm', '250gsm artboard', 'artboard 250', '250 gsm art board'], '300 gsm' => ['300 gsm', '300gsm board'] ]; // Normalize $binding_method = normalize_input($binding_method, $binding_method_map); $finishing_options = normalize_input($finishing_options, $finishing_map); $interior_print = normalize_input($interior_print, $interior_print_map); $cover_print = normalize_input($cover_print, $cover_print_map); $paper_weight_interior = normalize_input($paper_weight_interior, $interior_paper_map); $paper_weight_cover = normalize_input($paper_weight_cover, $cover_paper_map); // Load JSON $json_path = ABSPATH . 'wp-content/plugins/print-price-pro-corrected/includes/data/print-houses.json'; if (!file_exists($json_path)) { return ['error' => 'Print houses data file not found.']; } $merged_houses = json_decode(file_get_contents($json_path), true); if (!$merged_houses) { return ['error' => 'Invalid print houses JSON data.']; } $all_houses = array_merge( $merged_houses['Calculation16'] ?? [], $merged_houses['Calculation32'] ?? [] ); $compatible = []; foreach ($all_houses as $ph) { if ( empty($ph['Min Copies']) || empty($ph['Max Copies']) || $copies < intval($ph['Min Copies']) || $copies > intval($ph['Max Copies']) ) { continue; } // Countries normalization $countries = []; if (isset($ph['Countries'])) { $countries = is_array($ph['Countries']) ? array_map('trim', $ph['Countries']) : array_map('trim', explode(',', $ph['Countries'])); } if (!empty($delivery_country)) { $normalized_country = strtolower(trim($delivery_country)); $found = false; foreach ($countries as $c) { if (strtolower(trim($c)) === $normalized_country) { $found = true; break; } } if (!$found) { continue; } } $criteria = []; if (!empty($binding_method) && !empty($ph['Binding Method']) && strcasecmp($binding_method, $ph['Binding Method']) === 0) { $criteria[] = 'binding'; } if (!empty($interior_print) && !empty($ph['Interior Print']) && strcasecmp($interior_print, $ph['Interior Print']) === 0) { $criteria[] = 'interior_print'; } if (!empty($cover_print) && !empty($ph['Cover Print']) && strcasecmp($cover_print, $ph['Cover Print']) === 0) { $criteria[] = 'cover_print'; } if (!empty($finishing_options) && !empty($ph['Finishing Options']) && stripos($ph['Finishing Options'], $finishing_options) !== false) { $criteria[] = 'finishing'; } if (!empty($paper_weight_interior) && !empty($ph['Interior Paper Weight']) && stripos($ph['Interior Paper Weight'], $paper_weight_interior) !== false) { $criteria[] = 'interior_paper'; } if (!empty($paper_weight_cover) && !empty($ph['Cover Paper Weight']) && stripos($ph['Cover Paper Weight'], $paper_weight_cover) !== false) { $criteria[] = 'cover_paper'; } // ✅ If at least binding OR any other criteria matched, consider it if (empty($criteria)) { continue; } $best_estimate = null; $best_diff = null; if (!empty($ph['Estimates']) && is_array($ph['Estimates'])) { foreach ($ph['Estimates'] as $e) { $e_copies = intval($e['Copies']); $diff = abs($e_copies - $copies); if ($best_diff === null || $diff < $best_diff) { $best_diff = $diff; $best_estimate = $e; } } } $compatible[] = [ 'print_house' => $ph['Print House'] ?? 'Unknown', 'total_cost' => !empty($best_estimate['Estimated Cost']) ? floatval($best_estimate['Estimated Cost']) : 0, 'estimated_delivery_time' => $ph['Estimated Delivery Time'] ?? '', 'matched_criteria' => implode(', ', $criteria), 'copies_used' => $best_estimate['Copies'] ?? null ]; } // Fallback to cheapest option if still empty if (empty($compatible)) { usort($all_houses, fn($a, $b) => floatval($a['Estimates'][0]['Estimated Cost'] ?? 0) <=> floatval($b['Estimates'][0]['Estimated Cost'] ?? 0)); $ph = $all_houses[0]; $compatible[] = [ 'print_house' => $ph['Print House'] ?? 'Unknown', 'total_cost' => !empty($ph['Estimates'][0]['Estimated Cost']) ? floatval($ph['Estimates'][0]['Estimated Cost']) : 0, 'estimated_delivery_time' => $ph['Estimated Delivery Time'] ?? '', 'note' => 'Fallback to cheapest available option' ]; } usort($compatible, fn($a, $b) => $a['total_cost'] <=> $b['total_cost']); $selected = $compatible[0]; return [ 'copies' => (int)$copies, 'total_page_count' => (int)$total_page_count, 'book_size' => $book_size, 'interior_print' => $interior_print, 'cover_print' => $cover_print, 'paper_weight_interior' => $paper_weight_interior, 'paper_weight_cover' => $paper_weight_cover, 'binding_method' => $binding_method, 'finishing_options' => $finishing_options, 'delivery_country' => $delivery_country, 'print_houses' => $compatible, 'selected_print_house' => $selected ]; } https://printprice.pro/wp-sitemap-posts-post-1.xmlhttps://printprice.pro/wp-sitemap-posts-page-1.xmlhttps://printprice.pro/wp-sitemap-posts-product-1.xmlhttps://printprice.pro/wp-sitemap-posts-print_order-1.xmlhttps://printprice.pro/wp-sitemap-taxonomies-category-1.xmlhttps://printprice.pro/wp-sitemap-taxonomies-post_tag-1.xmlhttps://printprice.pro/wp-sitemap-taxonomies-product_cat-1.xmlhttps://printprice.pro/wp-sitemap-users-1.xml