uc_quote.pages.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @file
  4. * Menu callbacks for shipping quotes requested through AJAX.
  5. */
  6. /**
  7. * Pulls the get_quote_from_* triggers and assembles their returned data.
  8. */
  9. function uc_quote_assemble_quotes($order) {
  10. $products = $order->products;
  11. foreach ($products as $id => $product) {
  12. $node = (array) node_load($product->nid);
  13. foreach ($node as $key => $value) {
  14. if (!isset($product->$key)) {
  15. $product->$key = $value;
  16. }
  17. }
  18. $order->products[$id] = $product;
  19. }
  20. $shipping_types = array();
  21. foreach ($products as $product) {
  22. $shipping_types[] = uc_product_get_shipping_type($product);
  23. }
  24. $shipping_types = array_unique($shipping_types);
  25. $all_types = uc_quote_get_shipping_types();
  26. $shipping_type = '';
  27. // Use the most prominent shipping type (highest weight).
  28. // In theory, you can ship lighter products with heavier by the same
  29. // method, but not vice versa.
  30. $type_weight = -1000; // arbitrary low number
  31. foreach ($shipping_types as $type) {
  32. if ($all_types[$type]['weight'] > $type_weight) {
  33. $shipping_type = $all_types[$type]['id'];
  34. $type_weight = $all_types[$type]['weight'];
  35. }
  36. }
  37. $methods = uc_quote_methods();
  38. foreach ($methods as $id => $method) {
  39. if (!isset($method['quote']) || ($method['quote']['type'] != 'order' && $method['quote']['type'] != $shipping_type)) {
  40. unset($methods[$id]);
  41. }
  42. }
  43. $quote_data = array();
  44. foreach ($methods as $method) {
  45. $set = rules_config_load('get_quote_from_' . $method['id']);
  46. if (!$set || $set->execute($order)) {
  47. $data = uc_quote_action_get_quote($order, $method);
  48. foreach ($data as &$quote) {
  49. if (isset($quote['rate'])) {
  50. $quote['format'] = uc_currency_format($quote['rate']);
  51. }
  52. }
  53. $quote_data[$method['id']] = $data;
  54. }
  55. }
  56. return $quote_data;
  57. }
  58. /**
  59. * Retrieves shipping quote.
  60. *
  61. * @param $order
  62. * The order the quote is for.
  63. * @param $method
  64. * The shipping method to generate the quote.
  65. *
  66. * @return
  67. * Array of shipping quotes.
  68. */
  69. function uc_quote_action_get_quote($order, $method) {
  70. $details = array();
  71. foreach ($order as $key => $value) {
  72. if (substr($key, 0, 9) == 'delivery_') {
  73. $field = substr($key, 9);
  74. $details[$field] = $value;
  75. }
  76. }
  77. ob_start();
  78. // Load include file containing quote callback, if there is one.
  79. if (isset($method['quote']['file'])) {
  80. $inc_file = drupal_get_path('module', $method['module']) . '/' . $method['quote']['file'];
  81. if (is_file($inc_file)) {
  82. require_once($inc_file);
  83. }
  84. }
  85. if (function_exists($method['quote']['callback'])) {
  86. // This feels wrong, but it's the only way I can ensure that shipping
  87. // methods won't mess up the products in their methods.
  88. $products = array();
  89. foreach ($order->products as $key => $item) {
  90. if (uc_order_product_is_shippable($item)) {
  91. $products[$key] = clone $item;
  92. }
  93. }
  94. $quote_data = call_user_func($method['quote']['callback'], $products, $details, $method);
  95. }
  96. $messages = ob_get_contents();
  97. ob_end_clean();
  98. if ($messages && variable_get('uc_quote_log_errors', FALSE)) {
  99. watchdog('quote', '!messages', array('!messages' => $messages), WATCHDOG_WARNING);
  100. watchdog('quote', '<pre>@data</pre>', array('@data' => print_r($quote_data, TRUE)), WATCHDOG_WARNING);
  101. }
  102. return $quote_data;
  103. }