| 1 |
<?php |
| 2 |
|
| 3 |
namespace SuperFrete_API\Controllers; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class CheckoutFields |
| 10 |
{ |
| 11 |
public function __construct() |
| 12 |
{ |
| 13 |
// Classic checkout support |
| 14 |
add_filter('woocommerce_checkout_fields', array($this, 'add_required_checkout_fields'), 20); |
| 15 |
add_filter('woocommerce_billing_fields', array($this, 'customize_billing_fields'), 20); |
| 16 |
add_filter('woocommerce_shipping_fields', array($this, 'customize_shipping_fields'), 20); |
| 17 |
|
| 18 |
// WooCommerce Blocks support |
| 19 |
add_action('woocommerce_blocks_loaded', array($this, 'register_checkout_fields_block')); |
| 20 |
|
| 21 |
// Add helper text about N/A option |
| 22 |
add_action('woocommerce_before_checkout_form', array($this, 'add_number_field_notice')); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Add notice about N/A option for number field |
| 27 |
*/ |
| 28 |
public function add_number_field_notice() |
| 29 |
{ |
| 30 |
?> |
| 31 |
<div class="woocommerce-info superfrete-number-notice"> |
| 32 |
<strong>Nota:</strong> Se o endereço não possui número, digite "N/A" ou "S/N" no campo Número. |
| 33 |
</div> |
| 34 |
<?php |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Customize billing fields |
| 39 |
*/ |
| 40 |
public function customize_billing_fields($fields) |
| 41 |
{ |
| 42 |
// Make number field required if it exists |
| 43 |
if (isset($fields['billing_number'])) { |
| 44 |
$fields['billing_number']['required'] = true; |
| 45 |
$fields['billing_number']['label'] = __('Número', 'superfrete'); |
| 46 |
$fields['billing_number']['placeholder'] = __('Número ou N/A', 'superfrete'); |
| 47 |
$fields['billing_number']['description'] = __('Digite N/A se não houver número', 'superfrete'); |
| 48 |
} |
| 49 |
|
| 50 |
// Make neighborhood field required if it exists |
| 51 |
if (isset($fields['billing_neighborhood'])) { |
| 52 |
$fields['billing_neighborhood']['required'] = true; |
| 53 |
$fields['billing_neighborhood']['label'] = __('Bairro', 'superfrete'); |
| 54 |
} |
| 55 |
|
| 56 |
return $fields; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Customize shipping fields |
| 61 |
*/ |
| 62 |
public function customize_shipping_fields($fields) |
| 63 |
{ |
| 64 |
// Make number field required if it exists |
| 65 |
if (isset($fields['shipping_number'])) { |
| 66 |
$fields['shipping_number']['required'] = true; |
| 67 |
$fields['shipping_number']['label'] = __('Número', 'superfrete'); |
| 68 |
$fields['shipping_number']['placeholder'] = __('Número ou N/A', 'superfrete'); |
| 69 |
$fields['shipping_number']['description'] = __('Digite N/A se não houver número', 'superfrete'); |
| 70 |
} |
| 71 |
|
| 72 |
// Make neighborhood field required if it exists |
| 73 |
if (isset($fields['shipping_neighborhood'])) { |
| 74 |
$fields['shipping_neighborhood']['required'] = true; |
| 75 |
$fields['shipping_neighborhood']['label'] = __('Bairro', 'superfrete'); |
| 76 |
} |
| 77 |
|
| 78 |
return $fields; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Add required checkout fields |
| 83 |
*/ |
| 84 |
public function add_required_checkout_fields($fields) |
| 85 |
{ |
| 86 |
// Add number field to billing if it doesn't exist |
| 87 |
if (!isset($fields['billing']['billing_number'])) { |
| 88 |
$fields['billing']['billing_number'] = array( |
| 89 |
'label' => __('Número', 'superfrete'), |
| 90 |
'placeholder' => __('Número ou N/A', 'superfrete'), |
| 91 |
'required' => true, |
| 92 |
'class' => array('form-row-wide'), |
| 93 |
'priority' => 61, // After address_2 |
| 94 |
'description' => __('Digite N/A se não houver número', 'superfrete'), |
| 95 |
); |
| 96 |
} else { |
| 97 |
// Make it required if it exists |
| 98 |
$fields['billing']['billing_number']['required'] = true; |
| 99 |
$fields['billing']['billing_number']['placeholder'] = __('Número ou N/A', 'superfrete'); |
| 100 |
$fields['billing']['billing_number']['description'] = __('Digite N/A se não houver número', 'superfrete'); |
| 101 |
} |
| 102 |
|
| 103 |
// Add neighborhood field to billing if it doesn't exist |
| 104 |
if (!isset($fields['billing']['billing_neighborhood'])) { |
| 105 |
$fields['billing']['billing_neighborhood'] = array( |
| 106 |
'label' => __('Bairro', 'superfrete'), |
| 107 |
'placeholder' => __('Bairro', 'superfrete'), |
| 108 |
'required' => true, |
| 109 |
'class' => array('form-row-wide'), |
| 110 |
'priority' => 65, // After number |
| 111 |
); |
| 112 |
} else { |
| 113 |
// Make it required if it exists |
| 114 |
$fields['billing']['billing_neighborhood']['required'] = true; |
| 115 |
} |
| 116 |
|
| 117 |
// Add number field to shipping if it doesn't exist |
| 118 |
if (!isset($fields['shipping']['shipping_number'])) { |
| 119 |
$fields['shipping']['shipping_number'] = array( |
| 120 |
'label' => __('Número', 'superfrete'), |
| 121 |
'placeholder' => __('Número ou N/A', 'superfrete'), |
| 122 |
'required' => true, |
| 123 |
'class' => array('form-row-wide'), |
| 124 |
'priority' => 61, // After address_2 |
| 125 |
'description' => __('Digite N/A se não houver número', 'superfrete'), |
| 126 |
); |
| 127 |
} else { |
| 128 |
// Make it required if it exists |
| 129 |
$fields['shipping']['shipping_number']['required'] = true; |
| 130 |
$fields['shipping']['shipping_number']['placeholder'] = __('Número ou N/A', 'superfrete'); |
| 131 |
$fields['shipping']['shipping_number']['description'] = __('Digite N/A se não houver número', 'superfrete'); |
| 132 |
} |
| 133 |
|
| 134 |
// Add neighborhood field to shipping if it doesn't exist |
| 135 |
if (!isset($fields['shipping']['shipping_neighborhood'])) { |
| 136 |
$fields['shipping']['shipping_neighborhood'] = array( |
| 137 |
'label' => __('Bairro', 'superfrete'), |
| 138 |
'placeholder' => __('Bairro', 'superfrete'), |
| 139 |
'required' => true, |
| 140 |
'class' => array('form-row-wide'), |
| 141 |
'priority' => 65, // After number |
| 142 |
); |
| 143 |
} else { |
| 144 |
// Make it required if it exists |
| 145 |
$fields['shipping']['shipping_neighborhood']['required'] = true; |
| 146 |
} |
| 147 |
|
| 148 |
return $fields; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Register checkout fields for WooCommerce Blocks |
| 153 |
*/ |
| 154 |
public function register_checkout_fields_block() |
| 155 |
{ |
| 156 |
if (function_exists('woocommerce_register_additional_checkout_field')) { |
| 157 |
// Register shipping number field for blocks |
| 158 |
woocommerce_register_additional_checkout_field(array( |
| 159 |
'id' => 'shipping/number', |
| 160 |
'label' => __('Número', 'superfrete'), |
| 161 |
'location' => 'address', |
| 162 |
'type' => 'text', |
| 163 |
'required' => true, |
| 164 |
'attributes' => array( |
| 165 |
'data-1p-ignore' => 'true', |
| 166 |
'data-lpignore' => 'true', |
| 167 |
'autocomplete' => 'off', |
| 168 |
), |
| 169 |
)); |
| 170 |
|
| 171 |
// Register shipping neighborhood field for blocks |
| 172 |
woocommerce_register_additional_checkout_field(array( |
| 173 |
'id' => 'shipping/neighborhood', |
| 174 |
'label' => __('Bairro', 'superfrete'), |
| 175 |
'location' => 'address', |
| 176 |
'type' => 'text', |
| 177 |
'required' => true, |
| 178 |
'attributes' => array( |
| 179 |
'data-1p-ignore' => 'true', |
| 180 |
'data-lpignore' => 'true', |
| 181 |
'autocomplete' => 'off', |
| 182 |
), |
| 183 |
)); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
new \SuperFrete_API\Controllers\CheckoutFields(); |