uc_earlybird.module 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * @file
  4. * Functions for uc_earlybird module.
  5. */
  6. /**
  7. * Implements hook_line_item().
  8. */
  9. function uc_earlybird_uc_line_item() {
  10. $items[] = array(
  11. 'id' => 'earlybird',
  12. 'title' => t('Early bird discount'),
  13. 'default' => FALSE,
  14. 'stored' => TRUE,
  15. 'calculated' => TRUE,
  16. 'add_list' => TRUE,
  17. 'display_only' => FALSE,
  18. );
  19. return $items;
  20. }
  21. /**
  22. * Implements hook_form_alter().
  23. *
  24. * Add the discount form to the node edit form, under product settings.
  25. *
  26. * Alter the cart form to show the discount.
  27. */
  28. function uc_earlybird_form_alter(&$form, &$form_state, $form_id) {
  29. if (strpos($form_id, '_node_form') !== FALSE) {
  30. $product_types = uc_product_types();
  31. if (in_array($form['#node']->type, $product_types)) {
  32. $node = $form['#node'];
  33. $form['base']['earlybird'] = array(
  34. '#title' => t('Early bird discount'),
  35. '#type' => 'fieldset',
  36. '#weight' => $form['base']['dimensions']['#weight'] + 1,
  37. );
  38. $form['base']['earlybird']['earlybird_date'] = array(
  39. '#title' => 'Discounted on or before',
  40. '#type' => 'date',
  41. );
  42. if (isset($node->earlybird)) {
  43. $form['base']['earlybird']['earlybird_date']['#default_value'] = array(
  44. 'year' => date('Y', $node->earlybird['date']),
  45. 'month' => date('n', $node->earlybird['date']),
  46. 'day' => date('j', $node->earlybird['date']),
  47. );
  48. }
  49. $form['base']['earlybird']['earlybird_discount'] = array(
  50. '#title' => 'Discount amount',
  51. '#type' => 'textfield',
  52. '#size' => 3,
  53. '#description' => 'Enter in a dollar amount or percentage, e.g., 5% or 10',
  54. '#default_value' => isset($node->earlybird) ? $node->earlybird['discount'] : NULL,
  55. );
  56. }
  57. }
  58. if ($form_id == 'uc_cart_view_form') {
  59. foreach ($form['items'] as $key => &$val) {
  60. if (is_numeric($key)) {
  61. if (isset($val['nid'])) {
  62. $sql = "select * from {uc_earlybird} where nid = :nid";
  63. $result = db_query($sql, array(':nid' => $val['nid']['#value']));
  64. if ($earlybird = $result->fetchObject()) {
  65. if ($earlybird->date >= REQUEST_TIME && intval($earlybird->discount) > 0) {
  66. $discount = _uc_earlybird_discount_text($earlybird->discount, $val['#entity']->qty);
  67. $discountText = array(
  68. t('Early bird: !discount off at checkout', array('!discount' => $discount)),
  69. );
  70. $val['desc']['#markup'] .= theme('item_list', array(
  71. 'items' => $discountText,
  72. 'title' => NULL,
  73. 'type' => 'ul',
  74. 'attributes' => array('class' => 'uc-earlybird-teaser'),
  75. ));
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. /**
  84. * Implements hook_node_load().
  85. *
  86. * Load discount information to the node.
  87. */
  88. function uc_earlybird_node_load($nodes, $types) {
  89. $sql = 'select * from {uc_earlybird} where nid IN (:nids)';
  90. $result = db_query($sql, array(':nids' => array_keys($nodes)));
  91. while ($row = $result->fetchAssoc()) {
  92. $nodes[$row['nid']]->earlybird = $row;
  93. }
  94. }
  95. /**
  96. * Implements hook_node_insert().
  97. *
  98. * Update discount information.
  99. */
  100. function uc_earlybird_node_insert($node) {
  101. uc_earlybird_node_update($node);
  102. }
  103. /**
  104. * Implements hook_node_update().
  105. */
  106. function uc_earlybird_node_update($node) {
  107. if (isset($node->earlybird_discount)) {
  108. // Insert/update the product's discount.
  109. extract($node->earlybird_date);
  110. $date = mktime(23, 59, 59, $month, $day, $year);
  111. if (strpos($node->earlybird_discount, '%') !== FALSE) {
  112. $discount = floatval($node->earlybird_discount) . '%';
  113. }
  114. else {
  115. $discount = floatval($node->earlybird_discount);
  116. }
  117. if (db_query('select 1 from {uc_earlybird} where nid = :nid', array(':nid' => $node->nid))->fetchField()) {
  118. // Node already has earlybird data.
  119. $update = array('nid');
  120. }
  121. else {
  122. $update = array();
  123. }
  124. $earlybird->nid = $node->nid;
  125. $earlybird->date = $date;
  126. $earlybird->discount = $discount;
  127. drupal_write_record('uc_earlybird', $earlybird, $update);
  128. }
  129. }
  130. /**
  131. * Implements hook_add_to_cart().
  132. *
  133. * Check time and show notification.
  134. */
  135. function uc_earlybird_uc_add_to_cart($nid, $qty, $data) {
  136. $result = db_query('select node.title, eb.* from {uc_earlybird} eb
  137. inner join {node} node on node.nid = eb.nid
  138. where eb.nid = :nid', array(':nid' => $nid));
  139. if ($earlybird = $result->fetchObject()) {
  140. if (REQUEST_TIME <= $earlybird->date) {
  141. if (intval($earlybird->discount) > 0) {
  142. $discount_text = _uc_earlybird_discount_text($earlybird->discount, $qty);
  143. drupal_set_message(t('You will receive a discount of %discount when you checkout %title.', array('%title' => $earlybird->title, '%discount' => $discount_text)));
  144. }
  145. }
  146. }
  147. }
  148. /**
  149. * Display discount properly.
  150. */
  151. function _uc_earlybird_discount_text($discount, $qty = 1) {
  152. if (strpos($discount, '%') !== FALSE) {
  153. }
  154. else {
  155. $discount *= $qty;
  156. $discount = uc_currency_format($discount);
  157. }
  158. return $discount;
  159. }
  160. /**
  161. * Calculate early bird discount for a product. Check time.
  162. */
  163. function _uc_earlybird_calculate_discount($product) {
  164. $result = db_query('select * from {uc_earlybird} where nid = :nid', array(':nid' => $product->nid));
  165. if ($earlybird = $result->fetchObject()) {
  166. if (REQUEST_TIME <= $earlybird->date) {
  167. if (strpos($earlybird->discount, '%') !== FALSE) {
  168. return - (floatval($earlybird->discount) / 100 * $product->price);
  169. }
  170. else {
  171. return - (floatval($earlybird->discount));
  172. }
  173. }
  174. }
  175. }
  176. /**
  177. * Implements hook_order().
  178. *
  179. * Adds the line item discount for earlybird discounts.
  180. */
  181. function uc_earlybird_uc_order($op, &$arg1, $arg2) {
  182. if ($op == 'save') {
  183. $type = 'earlybird';
  184. foreach ($arg1->line_items as $key => $line_item) {
  185. if ($line_item['type'] == $type) {
  186. uc_order_delete_line_item($line_item['line_item_id']);
  187. }
  188. }
  189. db_delete('uc_order_line_items')
  190. ->condition('order_id', $arg1->order_id)
  191. ->condition('type', $key)
  192. ->execute();
  193. $discount = 0;
  194. $numDiscounts = 0;
  195. foreach ($arg1->products as $product) {
  196. $discount = _uc_earlybird_calculate_discount($product);
  197. if ($discount < 0) {
  198. uc_order_line_item_add($arg1->order_id, $type, 'Early bird discount', $discount * $product->qty, NULL, NULL);
  199. }
  200. }
  201. }
  202. }