uc_coupon.rules.inc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @file
  4. * Rules integration for uc_coupon
  5. */
  6. /**
  7. * Implements hook_rules_data_info().
  8. */
  9. function uc_coupon_rules_data_info() {
  10. $types['uc_coupon'] = array(
  11. 'label' => t('Ubercart discount coupon'),
  12. 'wrap' => FALSE,
  13. 'group' => t('Ubercart'),
  14. );
  15. }
  16. /**
  17. * Implements hook_rules_condition_info().
  18. */
  19. function uc_coupon_rules_condition_info() {
  20. $conditions['uc_coupon_condition_order_has_coupon'] = array(
  21. 'label' => t('Check if an order has a coupon applied'),
  22. 'group' => t('Order'),
  23. 'parameter' => array(
  24. 'order' => array(
  25. 'type' => 'uc_order',
  26. 'label' => t('Order'),
  27. 'optional' => TRUE,
  28. 'default value' => NULL,
  29. 'allow null' => TRUE
  30. ),
  31. 'codes' => array(
  32. 'type' => 'list<text>',
  33. 'label' => t('Codes'),
  34. 'description' => t('Enter coupon codes that this condition will apply to, one per line. Wildcards are allowed, e.g. COUPON* will match all codes beginning with COUPON. Leave blank to apply to any order with a coupon.'),
  35. 'restriction' => 'input',
  36. ),
  37. ),
  38. );
  39. $conditions['uc_coupon_condition_is_bulk'] = array(
  40. 'label' => t('Check if a coupon is a bulk coupon'),
  41. 'group' => t('Coupon'),
  42. 'parameter' => array(
  43. 'coupon' => array(
  44. 'type' => 'uc_coupon',
  45. 'label' => t('Coupon'),
  46. ),
  47. ),
  48. );
  49. return $conditions;
  50. }
  51. /**
  52. * Check if an order has a coupon applied.
  53. */
  54. function uc_coupon_condition_order_has_coupon($order, $codes=array()) {
  55. $check_codes = array();
  56. // If the order is in checkout by the current user, use the current session coupons.
  57. if (_uc_coupon_is_checkout_order($order)) {
  58. $coupons = uc_coupon_session_validate();
  59. foreach ($coupons as $coupon) {
  60. $check_codes[$coupon->code] = $coupon->code;
  61. }
  62. }
  63. // Now add any codes alreay in the order object (these take precedence).
  64. elseif (isset($order->data['coupons'])) {
  65. $check_codes = drupal_map_assoc(array_keys($order->data['coupons']));
  66. }
  67. if (count($check_codes) > 0) {
  68. $codes = array_filter($codes);
  69. // If no codes specified, match any coupon.
  70. if (count($codes)==0) {
  71. return TRUE;
  72. }
  73. // Check codes for exact or wildcard matches.
  74. foreach ($codes as $code) {
  75. foreach (array_keys($check_codes) as $check_code) {
  76. if (preg_match('/^' . str_replace('\*', '.*?', preg_quote(strtoupper(trim($code)), '/')) . '$/', $check_code)) {
  77. return TRUE;
  78. }
  79. }
  80. }
  81. }
  82. return FALSE;
  83. }
  84. /**
  85. * Check that a coupon is a bulk coupon.
  86. */
  87. function uc_coupon_condition_is_bulk($coupon) {
  88. return $coupon->bulk;
  89. }