-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpronamic-customer-type-for-woocommerce.php
213 lines (175 loc) · 6.65 KB
/
pronamic-customer-type-for-woocommerce.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
* Pronamic Customer Type for WooCommerce
*
* @package pronamic-woocommerce-customer-type
*
* Plugin Name: Pronamic Customer Type for WooCommerce
* Description: Streamline your sales process! This plugin allows customers to clearly indicate a personal or business purchase.
*
* Version: 1.0.0
*
* Author: Pronamic
* Author URI: https://www.pronamic.eu/
*
* Text Domain: pronamic-customer-type-for-woocommerce
* Domain Path: /languages/
*/
namespace Pronamic\WooCommerceCustomerType;
use WC_Order;
/**
* Plugin class.
*/
class Plugin {
/**
* Setup.
*
* @return void
*/
public function setup() {
\add_filter( 'woocommerce_billing_fields', [ $this, 'add_customer_type_field' ] );
\add_filter( 'woocommerce_billing_fields', [ $this, 'set_billing_company_requirement' ] );
\add_action( 'woocommerce_after_checkout_form', [ $this, 'after_checkout_form' ] );
\add_action( 'woocommerce_checkout_process', [ $this, 'process_checkout' ] );
\add_action( 'woocommerce_checkout_create_order', [ $this, 'checkout_create_order' ], 10, 2 );
\add_action( 'woocommerce_admin_order_data_after_order_details', [ $this, 'admin_order_data_after_order_details' ] );
}
/**
* Add customer type field.
*
* @link https://woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
* @link https://github.com/woocommerce/woocommerce/blob/7.7.0/plugins/woocommerce/includes/class-wc-countries.php#L1642-L1648
* @param array $fields Fields.
* @return array
*/
public function add_customer_type_field( $fields ) {
$fields['pronamic_customer_type'] = [
'label' => \__( 'Customer type', 'pronamic-woocommerce-customer-type' ),
'required' => true,
'type' => 'radio',
'class' => [
'form-row-wide',
'pronamic-radio',
],
'options' => [
'business' => \__( 'Business', 'pronamic-woocommerce-customer-type' ),
'private' => \__( 'Private', 'pronamic-woocommerce-customer-type' ),
],
'default' => 'business',
'priority' => 0,
];
return $fields;
}
/**
* Set billing company requirement.
*
* @link https://woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
* @link https://github.com/woocommerce/woocommerce/blob/7.7.0/plugins/woocommerce/includes/class-wc-countries.php#L1642-L1648
* @param array $fields Fields.
* @return array
*/
public function set_billing_company_requirement( $fields ) {
if ( ! \array_key_exists( 'billing_company', $fields ) ) {
return $fields;
}
$required = true;
// phpcs:disable WordPress.Security.NonceVerification.Missing -- Nonce verification is done upstream by WooCommerce?
if ( \array_key_exists( 'pronamic_customer_type', $_POST ) ) {
$customer_type = \sanitize_text_field( \wp_unslash( $_POST['pronamic_customer_type'] ) );
$required = ( 'business' === $customer_type );
}
/// phpcs:enable WordPress.Security.NonceVerification.Missing
$fields['billing_company']['required'] = $required;
return $fields;
}
/**
* After checkout form.
*
* @link https://github.com/woocommerce/woocommerce/blob/7.7.0/plugins/woocommerce/templates/checkout/form-checkout.php#L66
* @return void
*/
public function after_checkout_form() {
?>
<script type="text/javascript">
function toggle( type ) {
document.querySelector( '#billing_company_field' ).style.display = ( 'business' === type ) ? 'revert' : 'none';
document.querySelector( '#woocommerce_eu_vat_number_field' ).style.display = ( 'business' === type ) ? 'revert' : 'none';
}
document.querySelector( '#pronamic_customer_type_private' ).addEventListener( 'change',function() {
toggle( this.checked ? 'private' : '' );
} );
document.querySelector( '#pronamic_customer_type_business' ).addEventListener( 'change',function() {
toggle( this.checked ? 'business' : '' );
} );
</script>
<?php
}
/**
* Process checkout.
*
* @link https://woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#adding-a-custom-special-field
* @link https://github.com/woocommerce/woocommerce/blob/7.6.0/plugins/woocommerce/includes/class-wc-checkout.php#L1236
* @return void
* @throws \Exception Throws an exception if the customer type could not be determined.
*/
public function process_checkout() {
$customer_type = '';
// phpcs:disable WordPress.Security.NonceVerification.Missing -- Nonce verification is done upstream by WooCommerce?
if ( \array_key_exists( 'pronamic_customer_type', $_POST ) ) {
$customer_type = \sanitize_text_field( \wp_unslash( $_POST['pronamic_customer_type'] ) );
}
// phpcs:enable WordPress.Security.NonceVerification.Missing
if ( ! \in_array( $customer_type, [ 'business', 'private' ], true ) ) {
throw new \Exception(
\esc_html__( 'Due to a technical problem, it is unclear whether this concerns a business or private order, try again or contact us.', 'pronamic-woocommerce-customer-type' )
);
}
}
/**
* Update order meta.
*
* @link https://github.com/woocommerce/woocommerce/blob/7.6.0/plugins/woocommerce/includes/class-wc-checkout.php#L441-L446
* @link https://woocommerce.com/document/developing-using-woocommerce-crud-objects/
* @link https://github.com/woocommerce/woocommerce/wiki/Order-and-Order-Line-Item-Data
* @param WC_Order $order Order.
* @param array $data Post data.
* @return void
*/
public function checkout_create_order( $order, $data ) {
if ( ! \array_key_exists( 'pronamic_customer_type', $data ) ) {
return;
}
$customer_type = $data['pronamic_customer_type'];
$order->update_meta_data( '_pronamic_customer_type', $customer_type );
}
/**
* Admin order data after order details.
*
* @link https://github.com/woocommerce/woocommerce/blob/7.6.0/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-data.php#L349
* @param WC_Order $order WooCommerce order.
* @return void
*/
public function admin_order_data_after_order_details( $order ) {
$customer_type = $order->get_meta( '_pronamic_customer_type' );
?>
<p class="form-field form-field-wide">
<?php \esc_html_e( 'Customer type:', 'pronamic-woocommerce-customer-type' ); ?>
<?php
switch ( $customer_type ) {
case 'business':
echo \esc_html( \_x( 'Business', 'customer-type', 'pronamic-woocommerce-customer-type' ) );
break;
case 'private':
echo \esc_html( \_x( 'Private', 'customer-type', 'pronamic-woocommerce-customer-type' ) );
break;
default:
\esc_html_e( 'Unkown', 'pronamic-woocommerce-customer-type' );
break;
}
?>
</p>
<?php
}
}
$pronamic_woocommserce_customer_type_plugin = new Plugin();
$pronamic_woocommserce_customer_type_plugin->setup();