-
Notifications
You must be signed in to change notification settings - Fork 1
/
wordpress-plugin-woocommerce-purchase-order.php
131 lines (118 loc) · 4.79 KB
/
wordpress-plugin-woocommerce-purchase-order.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/*
Plugin Name: WooCommerce Checkout Purchase Order Field
Version: 1.0
Description: Adds a purchase order field to the checkout.
Author: Nick Breen
Author URI: http://foobar.net.nz
Plugin URI: https://github.com/nickbreen/wordpress-plugin-woocommerce-purchase-order
*/
// Our new fields.
$fields = array(
'purchase_order_reference' => array(
'type' => 'text', //type of field (text, textarea, password, select)
'label' => 'Purchase Order Reference', //label for the input field
// 'placeholder' => '', //placeholder for the input
// 'class' => '', //class for the input
// 'required' => false, //true or false, whether or not the field is require
// 'clear' => false, //true or false, applies a clear fix to the field/label
// 'label_class' => '', //class for the label element
// 'options' => '', //for select boxes, array of options (key => value pairs)
),
);
// We'll put out new fields into the checkout form's $form section
$form = 'order';
$form_fields = array(
$form => $fields,
);
// Label for the admin edit order section
$order_details_label = 'Order Details';
// Validation filters for our new fields.
$field_filters = array(
'purchase_order_reference' => array(
'filter' => FILTER_CALLBACK,
'flags' => FILTER_REQUIRE_SCALAR,
'options' => function ($value) {
// if the string does not contain anything unusual
return filter_var($value, FILTER_SANITIZE_STRING) === $value ? $value : false;
},
'notice' => sprintf('<strong>%s</strong> is invalid.', $fields['purchase_order_reference']['label']),
),
);
add_filter('woocommerce_checkout_fields', function ($fields) use ($form_fields) {
return array_merge_recursive($fields, $form_fields);
});
add_action('woocommerce_checkout_process', function () use ($field_filters) {
$filtered_fields = filter_input_array(INPUT_POST, $field_filters, true);
foreach ($filtered_fields as $field => $result) {
// $result === NULL if unspecified, which is OK
if (false === $result) { // if filter failed, which is not OK
wc_add_notice($field_filters[$field]['notice'], 'error');
}
}
});
add_action('woocommerce_checkout_update_order_meta', function ($order_id) use ($fields, $field_filters) {
$filtered_fields = filter_input_array(INPUT_POST, $field_filters, true);
foreach ($filtered_fields as $field => $result) {
if (false !== $result && null !== $result) {
update_post_meta($order_id, $field, $result);
}
}
});
add_filter('woocommerce_email_order_meta_fields', function ($email_fields, $sent_to_admin, $order) use ($fields) {
foreach ($fields as $field => $def) {
$email_fields[] = array(
'label' => $def['label'],
'value' => get_post_meta($order->id, $field, true),
);
}
return $email_fields;
}, 10, 3);
add_action('woocommerce_admin_order_data_after_order_details', function ($order) use ($fields, $order_details_label) {
foreach ($fields as $field => $def) {
if ($value = get_post_meta($order->id, $field, true)) {
$s .= sprintf(
'<p class="form-field form-field-wide"><label for="%1$s">%2$s:</label>'.
'<input type="text" id="%1$s" value="%3$s" readonly/></p>',
esc_attr($field),
esc_html($def['label']),
esc_attr($value)
);
}
}
if ($s) {
printf(
'<div class="clear"></div>'.
'<h4 style="clear: both">%s</h4>'.
'<div class="address">%s</div>',
$order_details_label,
$s
);
}
});
add_action('woocommerce_thankyou', function ($order_id) use ($fields) {
foreach ($fields as $field => $def) {
if ($value = get_post_meta($order_id, $field, true)) {
$s .= sprintf('<tr><th>%s:</th><td>%s<td/></tr>', esc_html($def['label']), esc_html($value));
}
}
if ($s) {
printf('<table class="shop_table order_details">%s</table>', $s);
}
}, 5);
// Also add the field to the PDF invoice/packing-slip if the plugin is available.
// See http://docs.wpovernight.com/woocommerce-pdf-invoices-packing-slips/pdf-template-action-hooks/
add_action('wpo_wcpdf_after_order_data', function ($template_type, $order) use ($fields) {
foreach ($fields as $field => $def) {
if ($value = get_post_meta($order->id, $field, true)) {
printf('<tr><th>%s:</th><td>%s<td/></tr>', esc_html($def['label']), esc_html($value));
}
}
}, 10, 2);
add_action('wpo_wcpdf_before_item_meta', function ($template_type, $item, $order) {
printf(
'<span class="item-meta"><dl><dt>%s:</dt><dd><p>%s</p></dd></dl></span>',
'Unit Price',
$item['ex_single_price']
);
}, 10, 3);