uc_coupon.api.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Ubercart Discount Coupon module api/hooks.
  6. * Version 7.x-2.x
  7. */
  8. /**
  9. * hook_uc_coupon_usage_alter().
  10. *
  11. * Allow modules to alter the usage count for a coupon.
  12. *
  13. * @param $usage
  14. * An associative array consiting of the following keys:
  15. * - 'user' => The number of times this coupon has been used by the specified user.
  16. * - 'codes' => An associative array listing the total number of uses for each code (for all users).
  17. * @param $cid
  18. * The coupon-id of the coupon in question.
  19. * @param $uid
  20. * The user-id whose usage is to be checked.
  21. */
  22. function hook_uc_coupon_usage_alter(&$usage, $cid, $uid) {
  23. // See if this coupon has been used in our table.
  24. $rows = db_query('SELECT code, uses FROM {extra_coupon_usage} WHERE cid = :cid', array(':cid' => $cid));
  25. foreach ($rows as $row) {
  26. if (isset($usage[$row->code])) {
  27. $usage[$row->code] += $row->uses;
  28. }
  29. else {
  30. $usage[$row->code] = $row->uses;
  31. }
  32. }
  33. }
  34. /**
  35. * hook_uc_coupon_actions().
  36. *
  37. * Allows modules to add to the list of actions available when coupons are listed in a table.
  38. *
  39. * @param $coupon
  40. * The coupon being displayed.
  41. * @return
  42. * An associative array describing the actions available. Must contain the followoing keys:
  43. * - 'url': The url where the action is processed.
  44. * - 'icon': The icon to display for this action.
  45. * - 'title': The text to display as a title for the action (usually as hover text over the icon).
  46. */
  47. function hook_uc_coupon_actions($coupon) {
  48. $actions = array();
  49. // Provide a "mark coupon as used" action.
  50. if (user_access('mark coupon as used')) {
  51. $actions[] = array(
  52. 'url' => 'admin/store/coupons/mark-as-used/' . $coupon->cid,
  53. 'icon' => drupal_get_path('module', 'mymodule') . 'mark_as_used.gif',
  54. 'title' => t('Mark coupon: @name as used', array('@name' => $coupon->name)),
  55. );
  56. };
  57. return $actions;
  58. }
  59. /**
  60. * hook_uc_coupon_revalidate().
  61. *
  62. * Invoked whenever the coupons added to the current session are about to be validated.
  63. *
  64. * Modules implementing this hook may add or remove coupon codes from the session via calls to
  65. * uc_coupon_session_add() or uc_coupon_session_clear().
  66. *
  67. * @param $order
  68. * The order against which the coupon will be validated.
  69. */
  70. function hook_uc_coupon_revalidate($order) {
  71. // Add a code if there are both a widget and a doohickey in the cart.
  72. $gotwidget = FALSE;
  73. $gotdoohickey = FALSE;
  74. foreach ($order->products as $product) {
  75. $node = node_load($product->nid);
  76. if ($node) {
  77. if ($node->type == 'widget') {
  78. $gotwidget = TRUE;
  79. }
  80. elseif ($node->type == 'doohickey') {
  81. $gotdoohickey = TRUE;
  82. }
  83. if ($gotwidget && $gotdoohickey) {
  84. uc_coupon_session_add('JACKPOT', 'auto');
  85. return;
  86. }
  87. }
  88. }
  89. }
  90. /**
  91. * hook_uc_coupon_apply().
  92. *
  93. * Invoked whenever a valid coupon is applied to an order. May be invoked than once for the same
  94. * coupon if it becomes invalid due to change in cart contents.
  95. *
  96. * Note that you should not do anything in your hoook implementation which causes the
  97. * current cart contents to be rebuilt. This includes calls to uc_cart_add_item() without
  98. * explicitly setting the $rebuild argument to false. And, since coupons may be submitted
  99. * on the checkout page after the cart contents are frozen, updates to the cart contents
  100. * may fail under some circumstances anyway.
  101. *
  102. * @param $coupon
  103. * The fully validated coupon which was applied.
  104. */
  105. function hook_uc_coupon_apply($coupon) {
  106. // Grant a role to the active user when a particular coupon is applied.
  107. // This could be used in conjunction with a node access module to expose products
  108. // only when certain coupon codes are entered.
  109. global $user;
  110. if ($coupon->cid == 99 && $user->uid != 0) {
  111. $roles = $user->roles + array(99 => 'My Special Role');
  112. user_save($user, array('roles' => $roles));
  113. }
  114. }
  115. /**
  116. * hook_uc_coupon_remove().
  117. *
  118. * Invoked whenever a previously valid coupon is removed.
  119. *
  120. * Note that you should not do anything in your hoook implementation which causes the
  121. * current cart contents to be rebuilt. This includes calls to uc_cart_add_item() without
  122. * explicitly setting the $rebuild argument to false. And, since coupons may be submitted
  123. * on the checkout page after the cart contents are frozen, updates to the cart contents
  124. * may fail under some circumstances anyway.
  125. *
  126. * @param $coupon
  127. * The coupon which was removed.
  128. */
  129. function hook_uc_coupon_remove($coupon) {
  130. // Revoke a role for the active user when a particular coupon is removed.
  131. global $user;
  132. if ($coupon->cid == 99 && $user->uid != 0) {
  133. $roles = $user->roles;
  134. $key = array_search('My Special Role', $roles);
  135. if ($key !== FALSE) {
  136. unset($roles[$key]);
  137. user_save($user, array('roles' => $roles));
  138. }
  139. }
  140. }
  141. /**
  142. * Add extra validation to a coupon.
  143. *
  144. * @param $coupon
  145. * The coupon object to validate, with special fields set as follows:
  146. * - $coupon->code: The specific code to be applied (even for bulk coupons).
  147. * - $coupon->amount: If $order !== FALSE, the discount that should be applied.
  148. * - $coupon->usage: Coupon usage data from uc_coupon_count_usage().
  149. * @param $order
  150. * The order against which this coupon is to be applied, or FALSE to bypass order validation.
  151. * @param $account
  152. * The account of the user trying to use the coupon, or FALSE to bypass user validation.
  153. *
  154. * @return
  155. * TRUE if the coupon should be accepted.
  156. * NULL to allow other modules to determine validation.
  157. * Otherwise, a string describing the reason for failure.
  158. */
  159. function hook_uc_coupon_validate(&$coupon, $order, $account) {
  160. // Check for allowed combinations.
  161. if (!empty($order->data['coupons'])) {
  162. foreach (array_keys($order->data['coupons']) as $code) {
  163. $other = uc_coupon_find($code);
  164. $other_listed = !empty($coupon->data['combinations']) && in_array($other->cid, $coupon->data['combinations']);
  165. $this_ok = (isset($coupon->data['negate_combinations']) xor $other_listed);
  166. $this_listed = !empty($other->data['combinations']) && in_array($coupon->cid, $other->data['combinations']);
  167. $other_ok = (isset($other->data['negate_combinations']) xor $this_listed);
  168. if (!$this_ok || !$other_ok) {
  169. return t('This coupon combination is not allowed.');
  170. }
  171. }
  172. }
  173. // Check maximum usage per code.
  174. if ($coupon->max_uses > 0 && !empty($coupon->usage['codes'][$coupon->code]) && $coupon->usage['codes'][$coupon->code] >= $coupon->max_uses) {
  175. return t('This coupon has reached the maximum redemption limit.');
  176. }
  177. // Check maximum usage per user.
  178. if ($account && isset($coupon->data['max_uses_per_user']) && $coupon->usage['user'] >= $coupon->data['max_uses_per_user']) {
  179. return t('This coupon has reached the maximum redemption limit.');
  180. }
  181. // Check user ID.
  182. if ($account && isset($coupon->data['users'])) {
  183. if (in_array("$account->uid", $coupon->data['users'], TRUE) xor !isset($coupon->data['negate_users'])) {
  184. return t('Your user ID is not allowed to use this coupon.');
  185. }
  186. }
  187. // Check roles.
  188. if ($account && isset($coupon->data['roles'])) {
  189. $role_found = FALSE;
  190. foreach ($coupon->data['roles'] as $role) {
  191. if (in_array($role, $account->roles)) {
  192. $role_found = TRUE;
  193. break;
  194. }
  195. }
  196. if ($role_found xor !isset($coupon->data['negate_roles'])) {
  197. return t('You do not have the correct permission to use this coupon.');
  198. }
  199. }
  200. }