Notepad Printing
Showing the single result
add_action('wp_ajax_get_dynamic_price', 'get_dynamic_price_callback'); add_action('wp_ajax_nopriv_get_dynamic_price', 'get_dynamic_price_callback'); function get_dynamic_price_callback() { global $wpdb; $table = 'print_orders'; $input = array_map('sanitize_text_field', $_POST); // --- ARTWORK --- if (isset($input['artwork'])) { $input['artwork'] = str_replace(['“', '”', '‘', '’'], ['"', '"', "'", "'"], $input['artwork']); $val = strtolower($input['artwork']); if (str_contains($val, 'print ready')) { $input['artwork'] = 'print ready'; } elseif (str_contains($val, 'need my artwork')) { $input['artwork'] = 'need my artwork'; } elseif (str_contains($val, 'you must supply')) { $input['artwork'] = 'you must supply'; } else { $input['artwork'] = trim($val); } } // --- PAGES --- if (isset($input['pages']) && preg_match('/\d+/', $input['pages'], $match)) { $input['pages'] = (int) $match[0]; // convert "100 pages" → 100 } // --- PRINTING --- if (isset($input['printing'])) { $val = strtolower($input['printing']); if (str_contains($val, 'pdf')) { $input['printing'] = 'FullColour PDF Proof'; } elseif (str_contains($val, 'full') && str_contains($val, 'b')) { $input['printing'] = 'full color b df require'; } elseif (str_contains($val, 'spot') && str_contains($val, '1')) { $input['printing'] = '1spot'; } elseif (str_contains($val, 'spot') && str_contains($val, '2')) { $input['printing'] = '2spot'; } else { $input['printing'] = strtolower(trim($val)); } } // --- PROOFING --- if (isset($input['proofing'])) { $val = strtolower($input['proofing']); if (str_contains($val, 'pdf')) { $input['proofing'] = 'pdf require'; } elseif (str_contains($val, 'hard')) { $input['proofing'] = 'hard copy required'; } else { $input['proofing'] = strtolower(trim($val)); } } // --- SIZE --- if (isset($input['size'])) { $val = strtolower($input['size']); if (str_contains($val, '845') || str_contains($val, '2100')) { $input['size'] = '854E2100'; } elseif (str_contains($val, '35')) { $input['size'] = '35 mm'; } elseif (str_contains($val, 'a4')) { $input['size'] = 'A4 Gloss'; } elseif (str_contains($val, 'square')) { $input['size'] = 'square'; } } // --- BASE --- if (isset($input['base'])) { $val = strtolower($input['base']); if (str_contains($val, 'silver')) { $input['base'] = 'You Must Sit Silver Base'; } } // --- WHERE CLAUSE SETUP --- $fields = [ "artwork", "base", "binding_edge", "booklet_size", "colum1", "colum2", "envelope_type", "hard_copy", "hard_copy_proof", "laminate", "pages", "paper", "printing", "proofing", "quantity", "scored", "size", "metform_id" ]; $where_clauses = []; $params = []; foreach ($fields as $field) { if (!empty($input[$field])) { if (in_array($field, ['quantity', 'metform_id', 'pages'])) { $where_clauses[] = "$field = %d"; $params[] = (int) $input[$field]; } else { $where_clauses[] = "LOWER($field) LIKE %s"; $params[] = '%' . strtolower($input[$field]) . '%'; } } } if (empty($where_clauses)) { wp_send_json_error("No matching criteria."); } $where_sql = implode(" AND ", $where_clauses); $sql = "SELECT total FROM $table WHERE $where_sql LIMIT 1"; $prepared = $wpdb->prepare($sql, ...$params); // --- DEBUG OUTPUT (REMOVE AFTER TESTING) --- wp_send_json_success([ 'input' => $input, 'sql_template' => $sql, 'params' => $params ]); // --- ACTUAL QUERY --- $price = $wpdb->get_var($prepared); if ($price !== null) { wp_send_json_success(["price" => $price]); } else { wp_send_json_error("No match found."); } }