-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuc_free_order.module
97 lines (86 loc) · 3.26 KB
/
uc_free_order.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* @file
* Adds a payment method to handle free orders.
*/
/**
* Implementation of hook_form_FORM_ID_alter().
* for the uc_cart_checkout_form
* In checkout determine whether the free order is available for use.
*/
function uc_free_order_form_uc_cart_checkout_form_alter(&$form, &$form_state, $form_id) {
$order = $form['panes']['payment']['line_items']['#order'];
$subtotal = $order->order_total;
// remove the free order payment method when the total is higher than 0
if ($subtotal >= 0.01) {
unset($form['panes']['payment']['payment_method']['#options']['free_order']);
return;
}
// copy the existing payment method for free order
$free_order = $form['panes']['payment']['payment_method']['#options']['free_order'];
// rewrite the list of options to only include the free order
$form['panes']['payment']['payment_method']['#options'] = array('free_order' => $free_order);
// make the form automatically select the free order without need for user input
$form['panes']['payment']['payment_method']['#default_value'] = 'free_order';
$form['panes']['payment']['payment_method']['#disabled'] = 'TRUE';
// delete any details from old default payment like credit card options
unset($form['panes']['payment']['details']);
}
/**
* Implementation of hook_form_FORM_ID_alter().
* for the uc_cart_checkout_review_form
* In Check out at the review Step more validation for the free order payment method
*/
function uc_free_order_form_uc_cart_checkout_review_form_alter(&$form, &$form_state, $form_id) {
$order = uc_order_load($_SESSION['cart_order']);
// only deal with cases where the payment method was set to free
if ($order->payment_method != 'free_order') {
return;
}
// validation for the amount
if ($order->order_total >= 0.01) {
drupal_set_message(t('We cannot process your order without payment.'), 'error');
drupal_goto('cart/checkout');
}
}
/**
* Implementation of hook_uc_payment_method().
*/
function uc_free_order_uc_payment_method() {
$zero = array('!zero' => uc_currency_format(0));
$methods['free_order'] = array(
'name' => t('Free order'),
'title' => t('Free order - Payment not required.'),
'desc' => t('Allow customers with !zero order totals to checkout without paying.', $zero),
'callback' => 'uc_payment_method_free_order',
'checkout' => TRUE,
'no_gateway' => TRUE,
'weight' => 0,
);
return $methods;
}
/**
* Will show the user a message when the payment method is select.
*/
function uc_payment_method_free_order($op, &$order) {
if ($op == 'cart-details') {
$details = array(
'uc_free_order_cart_details' => array(
'#markup' => t('Continue with checkout to complete your free order.'),
),
);
return $details;
}
}
/**
* Implementation of hook_uc_order().
* Will allow for a payment to be entered on this order that will allow
* other modules to trigger events for conditional actions
*/
function uc_free_order_uc_order($op, &$order) {
// During checkout, if the free order payment method is used, log a $0.00
// payment for the order to trigger any necessary predicates.
if ($op == 'submit' && $order->payment_method == 'free_order') {
uc_payment_enter($order->order_id, 'free_order', 0, 0, NULL, t('Checkout completed for a free order.'));
}
}