Advertisement
Guest User

Untitled

a guest
Jan 30th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.13 KB | None | 0 0
  1. <?php
  2. // needs: https://github.com/VIISON/AddressSplitting
  3.  
  4. add_filter('pr_shipping_dhl_label_args', 'fix_dhl_address', 2, 10);
  5.  
  6. function split_address( $address_1, $address_2 ) {
  7.   $split = array();
  8.   // try to split the first adress line
  9.   try {
  10.     $split = \VIISON\AddressSplitter\AddressSplitter::splitAddress( $address_1 );
  11.   } catch ( Exception $e ) {
  12.     // try again with with added field
  13.     try {
  14.       $split = \VIISON\AddressSplitter\AddressSplitter::splitAddress( $address_1 . ' ' .
  15.                                                                       $address_2 );
  16.     // address is probably invalid
  17.     } catch (Exception $e) {
  18.       error_log($e->getMessage());
  19.     }
  20.   }
  21.   return $split;
  22. }
  23.  
  24. function fix_dhl_address($args, $order_id) {
  25.   $split = $this->split_address($args['shipping_address']['address_1'], $args['shipping_address']['address_2']);
  26.  
  27.   if (!empty($split)) {
  28.     $args['shipping_address']['address_1'] = $split['streetName'];
  29.     $args['shipping_address']['address_2'] = $split['houseNumber'];
  30.   } // TODO: handle empty case (invalid address?) or rely on DHL API to handle it?
  31.   return $args;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement