uc_flatrate.module 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * @file
  4. * Shipping quote module that defines a flat shipping rate for each product.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function uc_flatrate_menu() {
  10. $items = array();
  11. $items['admin/store/settings/quotes/methods/flatrate/add'] = array(
  12. 'title' => 'Add flat rate quote',
  13. 'description' => 'Create a new flat rate shipping quote.',
  14. 'page callback' => 'drupal_get_form',
  15. 'page arguments' => array('uc_flatrate_admin_method_edit_form'),
  16. 'access arguments' => array('configure quotes'),
  17. 'type' => MENU_LOCAL_ACTION,
  18. 'file' => 'uc_flatrate.admin.inc',
  19. );
  20. $items['admin/store/settings/quotes/methods/flatrate/%'] = array(
  21. 'title' => 'Edit flat rate method',
  22. 'description' => 'Edit an existing flat rate shipping quote.',
  23. 'page callback' => 'drupal_get_form',
  24. 'page arguments' => array('uc_flatrate_admin_method_edit_form', 6),
  25. 'access arguments' => array('configure quotes'),
  26. 'file' => 'uc_flatrate.admin.inc',
  27. );
  28. $items['admin/store/settings/quotes/flatrate/%/delete'] = array(
  29. 'title' => 'Delete flat rate method',
  30. 'description' => 'Delete a flat rate shipping quote.',
  31. 'page callback' => 'drupal_get_form',
  32. 'page arguments' => array('uc_flatrate_admin_method_confirm_delete', 5),
  33. 'access arguments' => array('configure quotes'),
  34. 'file' => 'uc_flatrate.admin.inc',
  35. );
  36. return $items;
  37. }
  38. /**
  39. * Implements hook_form_alter().
  40. *
  41. * Adds a form element for the shipping rate of a product.
  42. */
  43. function uc_flatrate_form_alter(&$form, &$form_state, $form_id) {
  44. if (uc_product_is_product_form($form)) {
  45. $weight = variable_get('uc_quote_method_weight', array());
  46. $result = db_query("SELECT mid, title, product_rate FROM {uc_flatrate_methods}");
  47. foreach ($result as $method) {
  48. // Ensure default weight is set.
  49. $weight += array('flatrate_' . $method->mid => 0);
  50. if (!isset($form['shipping']['flatrate'])) {
  51. $form['shipping']['flatrate'] = array(
  52. '#type' => 'fieldset',
  53. '#title' => t('Flat shipping rates'),
  54. '#description' => t("Overrides the default shipping rate per product for each flat rate shipping method. Leave field empty to use the method's default value."),
  55. '#tree' => TRUE,
  56. '#collapsible' => TRUE,
  57. '#collapsed' => FALSE,
  58. '#weight' => 0,
  59. );
  60. }
  61. $form['shipping']['flatrate'][$method->mid] = array(
  62. '#type' => 'uc_price',
  63. '#title' => check_plain($method->title),
  64. '#default_value' => isset($form['#node']->flatrate[$method->mid]) ? $form['#node']->flatrate[$method->mid] : '',
  65. '#description' => t('Default rate: %price', array('%price' => uc_currency_format($method->product_rate))),
  66. '#weight' => $weight['flatrate_' . $method->mid],
  67. '#empty_zero' => FALSE,
  68. );
  69. }
  70. }
  71. }
  72. /**
  73. * Implements hook_node_insert().
  74. */
  75. function uc_flatrate_node_insert($node) {
  76. uc_flatrate_node_update($node);
  77. }
  78. /**
  79. * Implements hook_node_update().
  80. */
  81. function uc_flatrate_node_update($node) {
  82. if (uc_product_is_product($node->type)) {
  83. if (isset($node->flatrate) && is_array($node->flatrate)) {
  84. if (empty($node->revision)) {
  85. db_delete('uc_flatrate_products')
  86. ->condition('vid', $node->vid)
  87. ->execute();
  88. }
  89. $query = db_insert('uc_flatrate_products')
  90. ->fields(array('vid', 'nid', 'mid', 'rate'));
  91. foreach ($node->flatrate as $mid => $rate) {
  92. if (is_numeric($rate) && $rate >= 0) {
  93. $query->values(array(
  94. 'vid' => $node->vid,
  95. 'nid' => $node->nid,
  96. 'mid' => $mid,
  97. 'rate' => $rate,
  98. ));
  99. }
  100. }
  101. $query->execute();
  102. }
  103. }
  104. }
  105. /**
  106. * Implements hook_node_load().
  107. */
  108. function uc_flatrate_node_load($nodes, $types) {
  109. $vids = array();
  110. $product_types = uc_product_types();
  111. foreach ($nodes as &$node) {
  112. if (in_array($node->type, $product_types)) {
  113. $vids[$node->nid] = $node->vid;
  114. }
  115. }
  116. if ($vids) {
  117. $result = db_query("SELECT nid, mid, rate FROM {uc_flatrate_products} WHERE vid IN (:vids)", array(':vids' => $vids));
  118. foreach ($result as $method) {
  119. $nodes[$method->nid]->flatrate[$method->mid] = $method->rate;
  120. }
  121. }
  122. }
  123. /**
  124. * Implements hook_node_delete().
  125. */
  126. function uc_flatrate_node_delete($node) {
  127. db_delete('uc_flatrate_products')
  128. ->condition('nid', $node->nid)
  129. ->execute();
  130. }
  131. /**
  132. * Implements hook_node_revision_delete().
  133. */
  134. function uc_flatrate_node_revision_delete($node) {
  135. db_delete('uc_flatrate_products')
  136. ->condition('vid', $node->vid)
  137. ->execute();
  138. }
  139. /**
  140. * Implements hook_uc_shipping_method().
  141. */
  142. function uc_flatrate_uc_shipping_method() {
  143. $methods = array();
  144. $result = db_query("SELECT mid, title, label, base_rate, product_rate FROM {uc_flatrate_methods}");
  145. foreach ($result as $method) {
  146. $methods['flatrate_' . $method->mid] = array(
  147. 'id' => 'flatrate_' . $method->mid,
  148. 'module' => 'uc_flatrate',
  149. 'title' => $method->title,
  150. 'description' => t('!base_rate + !product_rate per item', array('!base_rate' => uc_currency_format($method->base_rate), '!product_rate' => uc_currency_format($method->product_rate))),
  151. 'operations' => array(
  152. 'edit' => array(
  153. 'title' => t('edit'),
  154. 'href' => 'admin/store/settings/quotes/methods/flatrate/' . $method->mid,
  155. ),
  156. 'delete' => array(
  157. 'title' => t('delete'),
  158. 'href' => 'admin/store/settings/quotes/flatrate/' . $method->mid . '/delete',
  159. ),
  160. ),
  161. 'quote' => array(
  162. 'type' => 'order',
  163. 'callback' => 'uc_flatrate_quote',
  164. 'accessorials' => array(
  165. $method->label,
  166. ),
  167. ),
  168. 'enabled' => TRUE,
  169. );
  170. }
  171. return $methods;
  172. }
  173. /**
  174. * Standard callback to return a shipping rate via the flat rate method.
  175. *
  176. * @param $products
  177. * The order's products.
  178. * @param $details
  179. * Other order details including a shipping address.
  180. * @param $method
  181. * The shipping method to use to create the quote.
  182. *
  183. * @return
  184. * An array containing the shipping quote for the order.
  185. */
  186. function uc_flatrate_quote($products, $details, $method) {
  187. $method = explode('_', $method['id']);
  188. $mid = $method[1];
  189. if ($method = db_query("SELECT * FROM {uc_flatrate_methods} WHERE mid = :mid", array(':mid' => $mid))->fetchObject()) {
  190. // Start at the base rate.
  191. $rate = $method->base_rate;
  192. foreach ($products as $product) {
  193. if (!isset($product->flatrate[$mid])) {
  194. // Add the method's default product rate.
  195. $rate += $method->product_rate * $product->qty;
  196. }
  197. else {
  198. // Add the product-specific rate.
  199. $rate += $product->flatrate[$mid] * $product->qty;
  200. }
  201. }
  202. $quotes[] = array(
  203. 'rate' => $rate,
  204. 'label' => check_plain($method->label),
  205. 'option_label' => check_plain($method->label),
  206. );
  207. }
  208. return $quotes;
  209. }