Jump to content

Text field Name split for Prestashop import


simplydave

Recommended Posts

I am new to PHP but have been given some code to split a single text field "Name" in to two seperate fields "First name" and "Last Name" so I can import the data in to my Prestashop v1.4.7.3 website.

 

The script works but the problem I have is that it splits the "Name" field the wrong way around.

Example
Name - "Mr Dave Simply" is split in to First Name "Simply" and Last Name "Mr Dave"

 

I assume I can just change the results around but I can not figure out how, so am asking for some advise.

 

I have a class.php

 

      public static function getName($fullname)
      {
          $result = array() ;
          $result['company'] = '' ;
          if ( preg_match('/,|\//', $fullname) )
          {
            $parts = preg_split('/,|\//', $fullname) ;
            $var = trim(substr($parts[0], 0, 31)) ;
            $result['company'] = trim(substr($parts[1], 0, 31)) ;
          }
          else  $var = substr($fullname, 0, 31) ;
          $var = preg_replace('/[0-9!<>,;?=+()@#"�{}_$%:]/', '', $var) ;
          $var = trim($var) ;
          $var = substr($var, 0, 31) ;
          $var1 =  explode(' ', $var) ;
          $sz = sizeof($var1) - 1 ;
          $lastname = $var1[$sz] ;
          unset($var1[$sz]) ;
          $firstname = implode(' ', $var1) ;
          $firstname = empty($firstname) ? 'unknown' : $firstname ;
          $lastname = empty($lastname) ? 'unknown' : $lastname ;
          $result['firstname'] = ucfirst($firstname) ;
          $result['lastname'] = ucfirst($lastname) ;
          return($result) ;
      }

 

and an import.php

 

          if ( $account_type == 2 && isset($order->BuyerEmail) )
          {
            $email_address = (string)$order->BuyerEmail ;
            $customer = new Customer() ;
            if ( empty($email_address ) )
            {
              echo basename(__FILE__) . ': ' . __LINE__ . ' - ' . $this->l('Couldn\'t add this customer') . ' : ' . $name . '(' . $email_address . ')' ;
              continue ;
            }
            $customer->getByEmail( $email_address ) ;
            if ( $customer->id )
            {
                $id_customer = $customer->id ;
            }
            else
            {
                $email_address = (string)$order->BuyerEmail ;
                $name = Amazon_Address::getAmazonName( (string)$order->Address->Name ) ;
                $customer->firstname = $name['firstname'] ;
                $customer->lastname = $name['lastname'] ;
                $customer->email = $email_address ;
                $customer->passwd = md5(rand());
                if ( ! $customer->add() )
                {
                    echo $this->l('Couldn\'t add this customer') . ' : ' . $name . '(' . $email_address . ')' ;
                }
                else
                {
                    $id_customer = $customer->id ;
                }
            }
          }

 

Thanks for your help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.