uc_coupon_recurring.module 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * @file
  4. * Discount coupon recurring payment integration.
  5. *
  6. * Allows coupons to apply to recurring payments
  7. */
  8. /**
  9. * Implements hook_module_implements_alter().
  10. *
  11. * Ensures that coupons are applied to recurring orders last.
  12. */
  13. function uc_coupon_recurring_module_implements_alter(&$implementations, $hook) {
  14. if ($hook == 'recurring_renewal_pending') {
  15. $group = $implementations['uc_coupon_recurring'];
  16. unset($implementations['uc_coupon_recurring']);
  17. $implementations['uc_coupon_recurring'] = $group;
  18. }
  19. }
  20. /**
  21. * Implements hook_form_FORM_ID_alter() for the uc_coupon_add_form.
  22. */
  23. function uc_coupon_recurring_form_uc_coupon_add_form_alter(&$form, &$form_state) {
  24. $active = !empty($form['#uc_coupon']->data['recurring_payments']);
  25. $form['uc_coupon_recurring'] = array(
  26. '#type' => 'fieldset',
  27. '#title' => t('Recurring payment settings'),
  28. '#description' => t('Controls the way this coupon is applied to recurring payments.'),
  29. '#collapsible' => TRUE,
  30. '#collapsed' => !$active,
  31. );
  32. $form['uc_coupon_recurring']['recurring_payments'] = array(
  33. '#type' => 'textfield',
  34. '#title' => t('Maximum number of recurring payments to discount'),
  35. '#description' => t('Specify the maximum number of payments (after the initial order) to which the discount should be applied. Leave blank (or enter 0) to apply only to the initial order. Enter -1 to apply to all recurring payments. Please note this will be ignored if recurring payments are processed via Paypal WPS.'),
  36. '#default_value' => !empty($form['#uc_coupon']->data['recurring_payments']) ? $form['#uc_coupon']->data['recurring_payments'] : '',
  37. );
  38. $form['#entity_builders'][] = 'uc_coupon_recurring_build_coupon';
  39. }
  40. /**
  41. * Entity builder callback.
  42. */
  43. function uc_coupon_recurring_build_coupon($type, $coupon, $form, $form_state) {
  44. if ($type == 'uc_coupon') {
  45. if (!empty($form_state['values']['recurring_payments'])) {
  46. $coupon->data['recurring_payments'] = $form_state['values']['recurring_payments'];
  47. }
  48. }
  49. }
  50. /**
  51. * Implements hook_recurring_renewal_pending().
  52. *
  53. * Reapplies any coupons that were originally valid for this order.
  54. */
  55. function uc_coupon_recurring_recurring_renewal_pending(&$order, $fee) {
  56. $coupons = array();
  57. if (!empty($order->data['coupons'])) {
  58. $codes = array_keys($order->data['coupons']);
  59. $order->data['coupons'] = array();
  60. foreach ($codes as $code) {
  61. $coupon = uc_coupon_find($code);
  62. $limit = empty($coupon->data['recurring_payments']) ? 0 : $coupon->data['recurring_payments'];
  63. if ($coupon && ($limit < 0 || $limit > $fee->charged_intervals)) {
  64. uc_coupon_prepare($coupon, $code, uc_coupon_calculate_discounts($coupon, $order));
  65. $coupons[] = $coupon;
  66. $order->data['coupons'][$code] = $coupon->discounts;
  67. }
  68. }
  69. if (!empty($coupons)) {
  70. uc_coupon_apply_to_order($order, $coupons);
  71. }
  72. }
  73. }