From 84d647fc4bbb36ec2d692f7c5949ea8430f6220f Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Tue, 15 Mar 2022 16:08:23 +0530 Subject: [PATCH] Change guest account transformation flow --- controllers/front/AuthController.php | 33 +++++- .../authentication.tpl | 7 +- .../css/authentication.css | 6 ++ .../js/authentication.js | 102 ++++++++++-------- 4 files changed, 104 insertions(+), 44 deletions(-) diff --git a/controllers/front/AuthController.php b/controllers/front/AuthController.php index 96c3ea223..7df1c19de 100644 --- a/controllers/front/AuthController.php +++ b/controllers/front/AuthController.php @@ -44,6 +44,10 @@ class AuthControllerCore extends FrontController { parent::init(); + $this->confirmations = array(); + $this->informations = array(); + $this->errors = array(); + if (!Tools::getIsset('step') && $this->context->customer->isLogged() && !$this->ajax) { Tools::redirect('index.php?controller='.(($this->authRedirection !== false) ? urlencode($this->authRedirection) : 'my-account')); } @@ -161,6 +165,10 @@ class AuthControllerCore extends FrontController )); $return = array( + 'hasConfirmation' => !empty($this->confirmations), + 'confirmations' => $this->confirmations, + 'hasInformation' => !empty($this->informations), + 'informations' => $this->informations, 'hasError' => !empty($this->errors), 'errors' => $this->errors, 'page' => $this->context->smarty->fetch($this->template), @@ -258,6 +266,10 @@ class AuthControllerCore extends FrontController $this->processSubmitAccount(); } + if (Tools::isSubmit('submitTransformAccount')) { + $this->processTransformAccount(); + } + if (Tools::isSubmit('SubmitLogin')) { $this->processSubmitLogin(); } @@ -746,9 +758,11 @@ class AuthControllerCore extends FrontController if (!Validate::isEmail($email = trim(Tools::getValue('email_create'))) || empty($email)) { $this->errors[] = Tools::displayError('Invalid email address.'); } elseif (Customer::customerExists($email)) { - $this->errors[] = Tools::displayError('An account using this email address has already been registered. Please enter a valid password or request a new one. ', false); + $this->errors[] = Tools::displayError('An account using this email address has already been registered. Please choose another one or sign in.', false); $_POST['email'] = trim(Tools::getValue('email_create')); unset($_POST['email_create']); + } elseif ($id_customer = Customer::customerExists($email, true, false)) { + $this->informations[] = Tools::displayError('You are already registered as a guest with this email address.').' '.Tools::displayError('').' to transform your account.'; } else { $this->create_account = true; $this->context->smarty->assign('email_create', Tools::safeOutput($email)); @@ -756,6 +770,23 @@ class AuthControllerCore extends FrontController } } + /** + * Process submit for account transformation + */ + protected function processTransformAccount() + { + if (!Validate::isEmail($email = trim(Tools::getValue('email_create'))) || empty($email)) { + $this->errors[] = Tools::displayError('Invalid email address.'); + } elseif ($id_customer = Customer::customerExists($email, true, false)) { + $customer = new Customer($id_customer); + if ($customer->transformToCustomer($this->context->language->id)) { + $this->confirmations[] = 'TRANSFORM_OK'; + } else { + $this->errors[] = Tools::displayError('Your guest account could not be transformed to a customer account.'); + } + } + } + /** * Update context after customer creation * @param Customer $customer Created customer diff --git a/themes/hotel-reservation-theme/authentication.tpl b/themes/hotel-reservation-theme/authentication.tpl index 611cfd4e6..8751d9d70 100644 --- a/themes/hotel-reservation-theme/authentication.tpl +++ b/themes/hotel-reservation-theme/authentication.tpl @@ -50,11 +50,17 @@ {/if}-->
+
+ +

{l s='Create an account'}

{l s='Please enter your email address to create an account.'}

+
@@ -68,7 +74,6 @@ {l s='Create an account'} -
diff --git a/themes/hotel-reservation-theme/css/authentication.css b/themes/hotel-reservation-theme/css/authentication.css index 9b157920d..d89e3b596 100644 --- a/themes/hotel-reservation-theme/css/authentication.css +++ b/themes/hotel-reservation-theme/css/authentication.css @@ -12,6 +12,12 @@ margin-bottom: 8px; } #create-account_form .form-group { margin: 0 0 20px 0; } + #create-account_form .btn-transform { + padding: 0; + font-size: 16px; + cursor: pointer; + vertical-align: initial; + text-decoration: underline; } #login_form { min-height: 297px; } diff --git a/themes/hotel-reservation-theme/js/authentication.js b/themes/hotel-reservation-theme/js/authentication.js index 38a461c1a..78c3cc503 100644 --- a/themes/hotel-reservation-theme/js/authentication.js +++ b/themes/hotel-reservation-theme/js/authentication.js @@ -22,17 +22,31 @@ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ -$(document).ready(function(){ - $(document).on('submit', '#create-account_form', function(e){ + +$(document).ready(function() { + $(document).on('click', '#SubmitCreate', function(e) { e.preventDefault(); - submitFunction(); + submitFunction('SubmitCreate'); + }); + + $(document).on('click', '.btn-transform', function(e) { + e.preventDefault(); + submitFunction('submitTransformAccount'); }); $('.is_customer_param').hide(); }); -function submitFunction() -{ +function submitFunction(submitAction) { $('#create_account_error').html('').hide(); + var data = { + controller: 'authentication', + ajax: true, + email_create: $('#email_create').val(), + back: $('input[name=back]').val(), + token: token + }; + data[submitAction] = 1; + $.ajax({ type: 'POST', url: baseUri + '?rand=' + new Date().getTime(), @@ -40,62 +54,66 @@ function submitFunction() cache: false, dataType : "json", headers: { "cache-control": "no-cache" }, - data: - { - controller: 'authentication', - SubmitCreate: 1, - ajax: true, - email_create: $('#email_create').val(), - back: $('input[name=back]').val(), - token: token - }, - success: function(jsonData) - { - if (jsonData.hasError) - { - var errors = ''; - for(error in jsonData.errors) - //IE6 bug fix - if(error != 'indexOf') - errors += '
  • ' + jsonData.errors[error] + '
  • '; - $('#create_account_error').html('
      ' + errors + '
    ').show(); + data: data, + success: function(jsonData) { + // display confirmations + if (jsonData.hasConfirmation) { + if (in_array('TRANSFORM_OK', jsonData.confirmations)) { + $('#guest_transform_success').show('slow'); + } + return; } - else - { + if (jsonData.hasInformation) { + var informations = ''; + for(information in jsonData.informations) { + //IE6 bug fix + if(information != 'indexOf') { + informations += '
  • ' + jsonData.informations[information] + '
  • '; + } + } + $('#create_account_information').html('
      ' + informations + '
    ').show(); + return; + } + if (jsonData.hasError) { + var errors = ''; + for(error in jsonData.errors) { + //IE6 bug fix + if(error != 'indexOf') { + errors += '
  • ' + jsonData.errors[error] + '
  • '; + } + } + $('#create_account_error').html('
      ' + errors + '
    ').show(); + } else { // adding a div to display a transition $('#center_column').html('
    ' + $('#center_column').html() + '
    '); - $('#noSlide').fadeOut('slow', function() - { + $('#noSlide').fadeOut('slow', function() { $('#noSlide').html(jsonData.page); - $(this).fadeIn('slow', function() - { - if (typeof bindUniform !=='undefined') + $(this).fadeIn('slow', function() { + if (typeof bindUniform !=='undefined') { bindUniform(); - if (typeof bindStateInputAndUpdate !=='undefined') + } + if (typeof bindStateInputAndUpdate !=='undefined') { bindStateInputAndUpdate(); + } document.location = '#account-creation'; }); }); } }, - error: function(XMLHttpRequest, textStatus, errorThrown) - { + error: function(XMLHttpRequest, textStatus, errorThrown) { error = "TECHNICAL ERROR: unable to load form.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus; - if (!!$.prototype.fancybox) - { - $.fancybox.open([ - { + if (!!$.prototype.fancybox) { + $.fancybox.open([{ type: 'inline', autoScale: true, minHeight: 30, content: "

    " + error + '

    ' - }], - { + }], { padding: 0 }); - } - else + } else { alert(error); + } } }); } \ No newline at end of file