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
<?php
namespace SIW\WooCommerce\Checkout;
class Fields{
public static function init() {
$self = new self();
add_filter( 'woocommerce_default_address_fields', [ $self , 'set_default_address_fields'], 10, 2 );
add_filter( 'woocommerce_billing_fields', [ $self, 'set_billing_fields'], 10, 2 );
add_filter( 'woocommerce_shipping_fields', '__return_empty_array' );
add_filter( 'woocommerce_checkout_fields', [ $self, 'add_checkout_fields'] );
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
add_filter( 'woocommerce_get_country_locale', [ $self, 'remove_locale_postcode_priority'] );
add_filter( 'woocommerce_country_locale_field_selectors', [ $self, 'remove_locale_field_selectors']);
add_action( 'woocommerce_checkout_create_order', [ $self, 'save_checkout_fields'], 10, 2 );
}
public function remove_locale_field_selectors( array $locale_fields ) {
unset( $locale_fields['address_2'] );
unset( $locale_fields['state'] );
return $locale_fields;
}
public function remove_locale_postcode_priority( array $locale ) {
unset( $locale['NL']['postcode'] );
return $locale;
}
protected function get_checkout_fields( $checkout_fields = [] ) {
$checkout_fields = wp_parse_args_recursive( siw_get_data( 'workcamps/checkout-fields' ), $checkout_fields );
return $checkout_fields;
}
public function save_checkout_fields( \WC_Order $order, array $data ) {
$checkout_fields = $this->get_checkout_fields();
foreach ( $checkout_fields as $section => $fields ) {
foreach ( $fields as $key => $field ) {
if ( isset( $data[ $key ] ) ) {
$order->update_meta_data( $key, $data[ $key ] );
}
}
}
if ( ! empty( $data['terms'] ) ) {
$order->update_meta_data( '_terms', $data['terms'] );
}
}
public function set_default_address_fields( array $standard_address_fields ) {
unset( $standard_address_fields['address_2'] );
unset( $standard_address_fields['company'] );
unset( $standard_address_fields['state'] );
$standard_address_fields = array_map( function( $field ) {
unset( $field['class']);
return $field;
}, $standard_address_fields);
$address_fields = siw_get_data( 'workcamps/address-fields' );
$address_fields = wp_parse_args_recursive( $address_fields, $standard_address_fields );
return $address_fields;
}
public function set_billing_fields( array $billing_fields, string $country ) {
$billing_fields['billing_phone']['class'] = ['form-row-first'];
$billing_fields['billing_email']['class'] = ['form-row-last'];
return $billing_fields;
}
public function add_checkout_fields( $checkout_fields ) {
$checkout_fields = $this->get_checkout_fields( $checkout_fields );
return $checkout_fields;
}
}