コード例 #1
0
 public function createCustomerGuestAccount($encoder, $buyerDictionary, $addressDictionary = array())
 {
     global $cookie;
     // taken from AuthController
     // no need to create if already logged in and has a customer id
     if ($cookie->logged && $cookie->id_customer) {
         return;
     }
     // make sure we can create a guest account
     if (!Configuration::get('PS_GUEST_CHECKOUT_ENABLED')) {
         CartAPI_Helpers::dieOnError($encoder, 'RegisterNotAuthorized', CartAPI_Handlers_Helpers::removeHtmlTags(Tools::displayError('You cannot create a guest account.')));
     }
     // prepare the fields inside the POST (so we can use Prestashop's validateController)
     unset($_POST['email']);
     if (isset($buyerDictionary['Email'])) {
         $_POST['email'] = $buyerDictionary['Email'];
     }
     unset($_POST['passwd']);
     $_POST['passwd'] = md5(time() . _COOKIE_KEY_);
     unset($_POST['firstname']);
     if (isset($addressDictionary['FirstName'])) {
         $_POST['firstname'] = $addressDictionary['FirstName'];
     }
     // take from address as backup
     if (isset($buyerDictionary['FirstName'])) {
         $_POST['firstname'] = $buyerDictionary['FirstName'];
     }
     // take from buyer if given
     unset($_POST['lastname']);
     if (isset($addressDictionary['LastName'])) {
         $_POST['lastname'] = $addressDictionary['LastName'];
     }
     // take from address as backup
     if (isset($buyerDictionary['LastName'])) {
         $_POST['lastname'] = $buyerDictionary['LastName'];
     }
     // take from buyer if given
     // verify fields are valid
     $customer = new Customer();
     if (_PS_VERSION_ < '1.5') {
         $errors = $customer->validateControler();
     } else {
         $errors = $customer->validateController();
     }
     if (is_array($errors) && count($errors) > 0) {
         CartAPI_Helpers::dieOnError($encoder, 'RegisterNotAuthorized', CartAPI_Handlers_Helpers::removeHtmlTags($errors[0]));
     }
     // add the new user
     $customer->active = 1;
     $customer->is_guest = 1;
     if (!$customer->add()) {
         CartAPI_Helpers::dieOnError($encoder, 'RegisterNotAuthorized', CartAPI_Handlers_Helpers::removeHtmlTags(Tools::displayError('An error occurred while creating your account.')));
     }
     // sync the cookie
     $loginHandler = CartAPI_Handlers_Helpers::newHandlerInstance($encoder, 'Login');
     $loginHandler->syncCookie($customer);
 }