jacekwilczynski

Dostosowania WooCommerce

Jul 26th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1. <?php
  2.  
  3. // Fill missing postcode and city
  4. add_action('woocommerce_new_order', function (int $orderId) {
  5.     $order = wc_get_order($orderId);
  6.     if ( ! $order->get_billing_postcode()) {
  7.         $order->set_billing_postcode('00-000');
  8.     }
  9.     if ( ! $order->get_billing_city()) {
  10.         $order->set_billing_city(__('Produkt Wirtualny'));
  11.     }
  12.     $order->set_billing_country('PL');
  13.     $order->save();
  14. });
  15.  
  16. // Reconfigure checkout fields
  17. add_filter('woocommerce_checkout_fields', function (array $fields) {
  18.     $fields['billing']['billing_first_name']['priority'] = 1;
  19.     $fields['billing']['billing_last_name']['priority']  = 2;
  20.     $fields['billing']['billing_email']['priority']      = 3;
  21.     $fields['billing']['billing_phone']['priority']      = 4;
  22.     $fields['billing']['billing_faktura']['priority']    = 5;
  23.     $fields['billing']['billing_company']['priority']    = 6;
  24.     $fields['billing']['billing_company']['required']    = false;
  25.     $fields['billing']['billing_nip']['priority']        = 7;
  26.     $fields['billing']['billing_address_1']['priority']  = 8;
  27.     $fields['billing']['billing_address_1']['required']  = false;
  28.     $fields['billing']['billing_address_2']['priority']  = 9;
  29.     $fields['billing']['billing_address_2']['required']  = false;
  30.     $fields['billing']['billing_postcode']['priority']   = 10;
  31.     $fields['billing']['billing_postcode']['required']   = false;
  32.     $fields['billing']['billing_postcode']['validate']   = [];
  33.     $fields['billing']['billing_city']['priority']       = 11;
  34.     $fields['billing']['billing_city']['required']       = false;
  35.     unset($fields['billing']['billing_state']);
  36.     unset($fields['order']['order_comments']);
  37.  
  38.     return $fields;
  39. });
  40.  
  41. // Add NIP field validation
  42. add_action('woocommerce_after_checkout_validation', function (array $data, WP_Error $errors) {
  43.     $isValidNip = function (string $nip): bool {
  44.         $nip = str_replace('-', '', $nip);
  45.  
  46.         if ( ! preg_match('/^\d{10}$/', $nip)) {
  47.             return false;
  48.         }
  49.  
  50.         $checksum = 6 * (int)$nip[0] +
  51.                     5 * (int)$nip[1] +
  52.                     7 * (int)$nip[2] +
  53.                     2 * (int)$nip[3] +
  54.                     3 * (int)$nip[4] +
  55.                     4 * (int)$nip[5] +
  56.                     5 * (int)$nip[6] +
  57.                     6 * (int)$nip[7] +
  58.                     7 * (int)$nip[8];
  59.  
  60.         return (int)$nip[9] === $checksum % 11;
  61.     };
  62.  
  63.     $nip = $data['billing_nip'] ?? '';
  64.     if ($nip && ! $isValidNip($nip)) {
  65.         $errors->add('invalid_nip', __('Nieprawidłowy NIP.'));
  66.     }
  67. }, 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment