Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Fill missing postcode and city
- add_action('woocommerce_new_order', function (int $orderId) {
- $order = wc_get_order($orderId);
- if ( ! $order->get_billing_postcode()) {
- $order->set_billing_postcode('00-000');
- }
- if ( ! $order->get_billing_city()) {
- $order->set_billing_city(__('Produkt Wirtualny'));
- }
- $order->set_billing_country('PL');
- $order->save();
- });
- // Reconfigure checkout fields
- add_filter('woocommerce_checkout_fields', function (array $fields) {
- $fields['billing']['billing_first_name']['priority'] = 1;
- $fields['billing']['billing_last_name']['priority'] = 2;
- $fields['billing']['billing_email']['priority'] = 3;
- $fields['billing']['billing_phone']['priority'] = 4;
- $fields['billing']['billing_faktura']['priority'] = 5;
- $fields['billing']['billing_company']['priority'] = 6;
- $fields['billing']['billing_company']['required'] = false;
- $fields['billing']['billing_nip']['priority'] = 7;
- $fields['billing']['billing_address_1']['priority'] = 8;
- $fields['billing']['billing_address_1']['required'] = false;
- $fields['billing']['billing_address_2']['priority'] = 9;
- $fields['billing']['billing_address_2']['required'] = false;
- $fields['billing']['billing_postcode']['priority'] = 10;
- $fields['billing']['billing_postcode']['required'] = false;
- $fields['billing']['billing_postcode']['validate'] = [];
- $fields['billing']['billing_city']['priority'] = 11;
- $fields['billing']['billing_city']['required'] = false;
- unset($fields['billing']['billing_state']);
- unset($fields['order']['order_comments']);
- return $fields;
- });
- // Add NIP field validation
- add_action('woocommerce_after_checkout_validation', function (array $data, WP_Error $errors) {
- $isValidNip = function (string $nip): bool {
- $nip = str_replace('-', '', $nip);
- if ( ! preg_match('/^\d{10}$/', $nip)) {
- return false;
- }
- $checksum = 6 * (int)$nip[0] +
- 5 * (int)$nip[1] +
- 7 * (int)$nip[2] +
- 2 * (int)$nip[3] +
- 3 * (int)$nip[4] +
- 4 * (int)$nip[5] +
- 5 * (int)$nip[6] +
- 6 * (int)$nip[7] +
- 7 * (int)$nip[8];
- return (int)$nip[9] === $checksum % 11;
- };
- $nip = $data['billing_nip'] ?? '';
- if ($nip && ! $isValidNip($nip)) {
- $errors->add('invalid_nip', __('Nieprawidłowy NIP.'));
- }
- }, 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment