Example #1
0
 /**
  * Creates a thirdparty in Dolibarr via webservice using WooCommerce user data
  *
  * @param int $user_id A Wordpress user ID
  *
  * @return array() $result The SOAP response
  */
 public function dolibarr_create_thirdparty($user_id)
 {
     /*
      * We use non WSDL mode to workaround Dolibarr broken declaration marking all the fields as required
      * when they're not.
      */
     try {
         $soap_client = new SoapClient(null, array('location' => $this->ws_endpoint . self::THIRDPARTY_ENDPOINT, 'uri' => 'http://www.dolibar.org/ns/'));
     } catch (SoapFault $exception) {
         $this->logger->add('doliwoo', $exception->getMessage());
         // Do nothing.
         return null;
     }
     $ref = get_user_meta($user_id, 'billing_company', true);
     $individual = 0;
     if ('' == $ref) {
         // We could not find a company, let's get an indivual
         $ref = get_user_meta($user_id, 'billing_last_name', true);
         $individual = 1;
     }
     $new_thirdparty = new Dolibarr_Thirdparty();
     $new_thirdparty->ref = $ref;
     // Company name or individual last name
     $new_thirdparty->individual = $individual;
     // Individual
     $new_thirdparty->firstname = get_user_meta($user_id, 'billing_first_name', true);
     $new_thirdparty->status = '1';
     // Active
     $new_thirdparty->client = '1';
     // Is a client
     $new_thirdparty->supplier = '0';
     // Is not a supplier
     $new_thirdparty->address = get_user_meta($user_id, 'billing_address', true);
     $new_thirdparty->zip = get_user_meta($user_id, 'billing_postcode', true);
     $new_thirdparty->town = get_user_meta($user_id, 'billing_city', true);
     $new_thirdparty->country_code = get_user_meta($user_id, 'billing_country', true);
     $new_thirdparty->phone = get_user_meta($user_id, 'billing_phone', true);
     $new_thirdparty->email = get_user_meta($user_id, 'billing_email', true);
     try {
         $result = $soap_client->createThirdParty($this->ws_auth, $new_thirdparty);
     } catch (SoapFault $exception) {
         $this->logger->add('doliwoo', 'createThirdParty request: ' . $exception->getMessage());
         // Do nothing.
         return null;
     }
     if (!('OK' == $result['result']->result_code)) {
         $this->logger->add('doliwoo', 'createThirdParty response: ' . $result['result']->result_code . ': ' . $result['result']->result_label);
         // Do nothing
         return null;
     }
     return $result;
 }