-
Notifications
You must be signed in to change notification settings - Fork 3
/
commerce_license_billing.rules.inc
104 lines (96 loc) · 2.98 KB
/
commerce_license_billing.rules.inc
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
98
99
100
101
102
103
104
<?php
/**
* @file
* Rules integration for the Commerce License Billing module.
*/
/**
* Implements hook_rules_condition_info().
*/
function commerce_license_billing_rules_condition_info() {
$conditions['commerce_license_billing_product_is_postpaid'] = array(
'label' => t('Product is postpaid'),
'parameter' => array(
'commerce_product' => array(
'type' => 'commerce_product',
'label' => t('Product'),
),
),
'group' => t('Commerce License Billing'),
'callbacks' => array(
'execute' => 'commerce_license_billing_product_postpaid_condition',
),
);
$conditions['commerce_license_billing_order_elligible'] = array(
'label' => t('Order is elligible for recurring'),
'parameter' => array(
'commerce_order' => array(
'type' => 'commerce_order',
'label' => t('Order'),
),
),
'group' => t('Commerce License Billing'),
'callbacks' => array(
'execute' => 'commerce_license_billing_order_elligible_condition',
),
);
return $conditions;
}
/**
* Implements hook_rules_action_info().
*/
function commerce_license_billing_rules_action_info() {
$actions['commerce_license_billing_create_recurring_orders'] = array(
'label' => t('Create recurring orders based on the initial order'),
'parameter' => array(
'commerce_order' => array(
'type' => 'commerce_order',
'label' => t('Order'),
),
),
'group' => t('Commerce License Billing'),
'callbacks' => array(
'execute' => 'commerce_license_billing_create_recurring_orders_action',
),
);
return $actions;
}
/**
* Rules condition callback: check if the product is postpaid.
*/
function commerce_license_billing_product_postpaid_condition($product) {
// Only return TRUE if the product has a billing cycle type selected
// (meaning that it has enabled billing).
if (!empty($product->cl_billing_cycle_type)) {
$product_wrapper = entity_metadata_wrapper('commerce_product', $product);
if ($product_wrapper->cl_billing_type->value() == 'postpaid') {
return TRUE;
}
}
return FALSE;
}
/**
* Rules condition callback: check if an order is elligible for recurring.
*/
function commerce_license_billing_order_elligible_condition($order) {
// Prevent recursion.
if ($order->type == 'recurring') {
return FALSE;
}
// Make sure the order has at least one license & billing cycle type selected.
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
if (!empty($line_item_wrapper->commerce_license)) {
$product = $line_item_wrapper->commerce_product->value();
if (!empty($product->cl_billing_cycle_type)) {
return TRUE;
}
}
}
return FALSE;
}
/**
* Rules action callback: create recurring orders based on the initial order.
*/
function commerce_license_billing_create_recurring_orders_action($order) {
commerce_license_billing_create_recurring_orders($order);
}