ajax_get_product_specifications.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. require_once 'conn.php';
  3. require_once 'functions.php';
  4. checklogin("信息管理");
  5. // Get product ID
  6. $product_id = isset($_POST['product_id']) ? (int)$_POST['product_id'] : 0;
  7. if (empty($product_id)) {
  8. echo '<p class="error-message" style="color:red;text-align:center;padding:10px;">无效的产品ID</p>';
  9. exit;
  10. }
  11. // Get all specifications for this product
  12. $sql = "SELECT ps.id as spec_id, ps.spec_name, ps.sort_order, ps.spec_value, ps.spec_code,
  13. ps.min_order_quantity, p.id as product_id, p.unit
  14. FROM product_specifications ps
  15. JOIN products p ON ps.product_id = p.id
  16. WHERE ps.product_id = $product_id
  17. ORDER BY ps.sort_order, ps.id";
  18. $result = $conn->query($sql);
  19. $html = '';
  20. if ($result && $result->num_rows > 0) {
  21. while ($row = $result->fetch_assoc()) {
  22. $product_id = $row['product_id'];
  23. $spec_id = $row['spec_id'];
  24. $spec_name = htmlspecialcharsFix($row['spec_name']);
  25. $spec_value = htmlspecialcharsFix($row['spec_value'] ?? '');
  26. $spec_code = htmlspecialcharsFix($row['spec_code'] ?? '');
  27. $unit = htmlspecialcharsFix($row['unit']);
  28. $min_qty = !empty($row['min_order_quantity']) ? intval($row['min_order_quantity']) : 1;
  29. // 构建完整的规格显示文本
  30. $spec_display = $spec_name;
  31. if (!empty($spec_value)) {
  32. $spec_display .= ': ' . $spec_value;
  33. }
  34. if (!empty($spec_code)) {
  35. $spec_display .= ' <span style="font-size:12px;color:#666;">(编码: ' . $spec_code . ')</span>';
  36. }
  37. $html .= '<div class="specitem">
  38. <div class="specdelete" title="删除此规格">✕</div>
  39. <input type="hidden" name="spec_product_id[]" value="' . $product_id . '">
  40. <input type="hidden" name="spec_id[]" value="' . $spec_id . '">
  41. <span class="spec-label">规格:</span>
  42. <span class="spec-name">' . $spec_display . '</span>
  43. <span class="spec-label" style="margin-left:15px;">起订数量:</span>
  44. <input type="number" class="spec-small-input" name="spec_moq[]" value="' . $min_qty . '" min="1">
  45. <span class="unit">' . $unit . '</span>
  46. <span class="spec-label" style="margin-left:15px;">售价:</span>
  47. <input type="text" class="spec-small-input" name="spec_price[]" value="" required min="0.01">
  48. <span>RMB</span>
  49. </div>';
  50. }
  51. echo $html;
  52. } else {
  53. echo '<p style="padding:10px; background:#fff3f3; color:#d9534f; text-align:center; margin:10px 0; border-radius:4px;">该产品没有规格信息,请先在产品管理中添加规格</p>';
  54. }
  55. $conn->close();
  56. ?>