forked from bockhauzen/rentalshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrentman_project.php
111 lines (99 loc) · 4.56 KB
/
rentman_project.php
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
105
106
107
108
109
110
111
<?php
// ------------- API Request Functions ------------- \\
# Handles API Request for project creation
function add_project($order_id, $contact_id, $transport_id, $fees, $contact_person, $location_contact){
if (apply_filters('rentman/creating_project', true)) {
$url = receive_endpoint();
$token = get_option('plugin-rentman-token');
# Setup New Project Request to send JSON
$message = json_encode(setup_newproject_request($token, $order_id, $contact_id, $transport_id, $fees, $contact_person, $location_contact), JSON_PRETTY_PRINT);
# Send Request & Receive Response
do_request($url, $message);
//echo json_encode(json_decode($received), JSON_PRETTY_PRINT);
}
}
// ------------- Array Creation Functions ------------- \\
# Create array containing all products
function get_material_array($order_id){
$order = new WC_Order($order_id);
$matarr = array();
foreach($order->get_items() as $key => $lineItem){
$name = $lineItem['name'];
$product_id = $lineItem['product_id'];
$product = wc_get_product($product_id);
if (get_post_meta($product_id, 'rentman_imported', true) == true){ # Only Rentman products must be added to the request
array_push($matarr, array(
$name,
$lineItem['qty'],
($lineItem['line_total'] / $lineItem['qty']),
$product->get_sku()));
}
}
return $matarr;
}
# Combine the two arrays into an array with the right format
function planmaterial_array($materials, $planarray, $order_id, $contact_id, $counter){
$staffels = get_staffels($order_id);
$discounts = get_all_discounts($order_id, $contact_id);
$planmatarr = array_fill_keys($planarray['Planningmateriaal'], 'Test');
foreach ($materials as $item){
$planmatarr[$counter] = array(
'values' => array(
'naam' => $item[0],
'aantal' => $item[1],
'aantaltotaal' => $item[1],
'prijs' => $item[2],
'materiaal' => $item[3],
'staffel' => $staffels[$item[3]],
'korting' => isset($discounts[$item[3]]) ? $discounts[$item[3]] : 0),
'parameters' => array(
'extend_sets' => true));
$counter--;
}
return $planmatarr;
}
// ------------- Customizing Checkout Fields ------------- \\
# Adds checkout fields for the external reference, shipping phone number and shipping email
function adjust_checkout($fields){
$fields['billing']['billing_reference'] = array(
'label' => __('External reference', 'rentalshop'),
'placeholder' => __('External reference (optional)', 'rentalshop'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
$fields['shipping']['shipping_email'] = array(
'label' => __('Email address', 'rentalshop'),
'placeholder' => __('Email address', 'rentalshop'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
$fields['shipping']['shipping_phone'] = array(
'label' => __('Phone', 'rentalshop'),
'placeholder' => __('Phone number', 'rentalshop'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
# Adds the rental period data to the order data in the confirmation email
function add_dates_to_email($fields, $sent_to_admin, $order){
$fields['rental_period'] = array(
'label' => __('Rental period', 'rentalshop'),
'value' => get_post_meta($order->id, 'rental_period', true),
);
return $fields;
}
# Adds the rental period data to the order meta
function add_rental_data($order_id){
$dates = get_dates();
$rental = $dates['from_date'] . ' ~ ' . $dates['to_date'];
update_post_meta($order_id, 'rental_period', $rental);
}
# Displays the rental period data on the order details page
function display_dates_in_order($order){
echo '<p><strong>' . __('Rental period', 'rentalshop') . ':</strong> ' . get_post_meta($order->get_id(), 'rental_period', true) . '</p>';
}
?>