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
|
<?php
namespace Automattic\WooCommerce\Blocks\Domain\Services;
use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields;
/** * Service class managing checkout fields and its related extensibility points in the admin area. */ class CheckoutFieldsAdmin {
/** * Checkout field controller. * * @var CheckoutFields */ private $checkout_fields_controller;
/** * Sets up core fields. * * @param CheckoutFields $checkout_fields_controller Instance of the checkout field controller. */ public function __construct( CheckoutFields $checkout_fields_controller ) { $this->checkout_fields_controller = $checkout_fields_controller; }
/** * Initialize hooks. This is not run Store API requests. */ public function init() { add_filter( 'woocommerce_admin_billing_fields', array( $this, 'admin_address_fields' ), 10, 3 ); add_filter( 'woocommerce_admin_billing_fields', array( $this, 'admin_contact_fields' ), 10, 3 ); add_filter( 'woocommerce_admin_shipping_fields', array( $this, 'admin_address_fields' ), 10, 3 ); add_filter( 'woocommerce_admin_shipping_fields', array( $this, 'admin_order_fields' ), 10, 3 ); }
/** * Converts the shape of a checkout field to match whats needed in the WooCommerce meta boxes. * * @param array $field The field to format. * @param string $key The field key. This will be used for the ID of the field when passed to the meta box. * @return array Formatted field. */ protected function format_field_for_meta_box( $field, $key ) { $formatted_field = array( 'id' => $key, 'label' => $field['label'], 'value' => $field['value'], 'type' => $field['type'], 'update_callback' => array( $this, 'update_callback' ), 'show' => true, 'wrapper_class' => 'form-field-wide', );
if ( 'select' === $field['type'] ) { $formatted_field['options'] = array_column( $field['options'], 'label', 'value' ); }
if ( 'checkbox' === $field['type'] ) { $formatted_field['checked_value'] = '1'; $formatted_field['unchecked_value'] = '0'; }
return $formatted_field; }
/** * Updates a field value for an order. * * @param string $key The field key. * @param mixed $value The field value. * @param \WC_Order $order The order to update the field for. */ public function update_callback( $key, $value, $order ) { list( $group, $key ) = explode( '/', $key, 2 ); $group = CheckoutFields::get_group_name( $group ); $this->checkout_fields_controller->persist_field_for_order( $key, $value, $order, $group, false ); }
/** * Injects address fields in WC admin orders screen. * * @param array $fields The fields to show. * @param \WC_Order|boolean $order The order to show the fields for. * @param string $context The context to show the fields for. * @return array */ public function admin_address_fields( $fields, $order = null, $context = 'edit' ) { if ( ! $order instanceof \WC_Order ) { return $fields; }
$group_name = doing_action( 'woocommerce_admin_billing_fields' ) ? 'billing' : 'shipping'; $additional_fields = $this->checkout_fields_controller->get_order_additional_fields_with_values( $order, 'address', $group_name, $context ); foreach ( $additional_fields as $key => $field ) { $prefixed_key = CheckoutFields::get_group_key( $group_name ) . $key; $additional_fields[ $key ] = $this->format_field_for_meta_box( $field, $prefixed_key ); }
array_splice( $fields, array_search( 'state', array_keys( $fields ), true ) + 1, 0, $additional_fields );
return $fields; }
/** * Injects contact fields in WC admin orders screen. * * @param array $fields The fields to show. * @param \WC_Order|boolean $order The order to show the fields for. * @param string $context The context to show the fields for. * @return array */ public function admin_contact_fields( $fields, $order = null, $context = 'edit' ) { if ( ! $order instanceof \WC_Order ) { return $fields; }
$additional_fields = $this->checkout_fields_controller->get_order_additional_fields_with_values( $order, 'contact', 'other', $context );
foreach ( $additional_fields as $key => $field ) { $prefixed_key = CheckoutFields::get_group_key( 'other' ) . $key; $additional_fields[ $key ] = $this->format_field_for_meta_box( $field, $prefixed_key ); }
return array_merge( $fields, $additional_fields ); }
/** * Injects additional fields in WC admin orders screen. * * @param array $fields The fields to show. * @param \WC_Order|boolean $order The order to show the fields for. * @param string $context The context to show the fields for. * @return array */ public function admin_order_fields( $fields, $order = null, $context = 'edit' ) { if ( ! $order instanceof \WC_Order ) { return $fields; }
$additional_fields = $this->checkout_fields_controller->get_order_additional_fields_with_values( $order, 'order', 'other', $context );
foreach ( $additional_fields as $key => $field ) { $prefixed_key = CheckoutFields::get_group_key( 'other' ) . $key; $additional_fields[ $key ] = $this->format_field_for_meta_box( $field, $prefixed_key ); }
return array_merge( $fields, $additional_fields ); } }
|