Skip to content

Commit

Permalink
FO: Fix checkout exception when adding required field
Browse files Browse the repository at this point in the history
  • Loading branch information
fatmaBouchekoua authored and LittleBigDev committed Oct 17, 2017
1 parent 4fca11c commit 2b57651
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 12 deletions.
7 changes: 6 additions & 1 deletion classes/AddressChecksumCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class AddressChecksumCore implements ChecksumInterface
* @param Address $address
*
* @return string SHA1 checksum for the Address
* @throws Exception
*/
public function generateChecksum($address)
{
Expand All @@ -45,7 +46,11 @@ public function generateChecksum($address)
}

$uniqId = '';
$fields = $address->getFields();
try {
$fields = $address->getFields();
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
foreach ($fields as $name => $value) {
$uniqId .= $value.self::SEPARATOR;
}
Expand Down
19 changes: 16 additions & 3 deletions classes/checkout/CartChecksum.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,23 @@ public function generateChecksum($cart)
$uniq_id .= $this->separator;
$uniq_id .= $cart->id_lang;
$uniq_id .= $this->separator;

$uniq_id .= $this->addressChecksum->generateChecksum(new Address($cart->id_address_delivery));
try {
$uniq_id .= $this->addressChecksum->generateChecksum(new Address($cart->id_address_delivery));
} catch (Exception $e) {
return array(
'id_address' => $cart->id_address_delivery,
'exception' => $e->getMessage(),
);
}
$uniq_id .= $this->separator;
$uniq_id .= $this->addressChecksum->generateChecksum(new Address($cart->id_address_invoice));
try {
$uniq_id .= $this->addressChecksum->generateChecksum(new Address($cart->id_address_invoice));
} catch (Exception $e) {
return array(
'id_address' => $cart->id_address_invoice,
'exception' => $e->getMessage(),
);
}
$uniq_id .= $this->separator;

$products = $cart->getProducts($refresh = true);
Expand Down
23 changes: 21 additions & 2 deletions classes/checkout/CheckoutAddressesStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,37 @@ public function handleRequest(array $requestParams = array())

public function getTemplateParameters()
{
$id_address_delivery = $this->getCheckoutSession()->getIdAddressDelivery();
$id_address_invoice = $this->getCheckoutSession()->getIdAddressInvoice();
$addressWarning = false;
$errorKey = 'address_error';
if (isset($this->context->controller->checkoutWarning['address']) &&
$id_address_delivery == $this->context->controller->checkoutWarning['address']['id_address']) {
$addressWarning = $this->context->controller->checkoutWarning['address'];
$errorKey = 'delivery_address_error';
} elseif (isset($this->context->controller->checkoutWarning['address']) &&
$id_address_invoice == $this->context->controller->checkoutWarning['address']['id_address']) {
$addressWarning = $this->context->controller->checkoutWarning['address'];
$errorKey = 'invoice_address_error';
$this->use_same_address = false;
}

if ($this->use_same_address && $id_address_invoice != $id_address_delivery) {
$this->use_same_address = false;
}
return array(
'address_form' => $this->addressForm->getProxy(),
'use_same_address' => $this->use_same_address,
'use_same_address' => $this->use_same_address,
'use_different_address_url' => $this->context->link->getPageLink('order', true, null, array('use_same_address' => 0)),
'new_address_delivery_url' => $this->context->link->getPageLink('order', true, null, array('newAddress' => 'delivery')),
'new_address_invoice_url' => $this->context->link->getPageLink('order', true, null, array('newAddress' => 'invoice')),
'id_address_delivery' => $this->getCheckoutSession()->getIdAddressDelivery(),
'id_address_invoice' => $this->getCheckoutSession()->getIdAddressInvoice(),
'id_address_delivery' => $id_address_delivery,
'id_address_invoice' => $id_address_invoice,
'show_delivery_address_form' => $this->show_delivery_address_form,
'show_invoice_address_form' => $this->show_invoice_address_form,
'form_has_continue_button' => $this->form_has_continue_button,
$errorKey => $addressWarning,
);
}

Expand Down
12 changes: 10 additions & 2 deletions controllers/front/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class OrderControllerCore extends FrontController
public $ssl = true;
public $php_self = 'order';
public $page_name = 'checkout';
public $checkoutWarning = false;

protected $checkoutProcess;

Expand Down Expand Up @@ -150,7 +151,7 @@ protected function bootstrap()
protected function saveDataToPersist(CheckoutProcess $process)
{
$data = $process->getDataToPersist();
$data['checksum'] = $this->cartChecksum->generateChecksum($this->context->cart);
$data['checksum'] = is_array($checksum = $this->cartChecksum->generateChecksum($this->context->cart)) ? null : $checksum;

Db::getInstance()->execute(
'UPDATE '._DB_PREFIX_.'cart SET checkout_session_data = "'.pSQL(json_encode($data)).'"
Expand All @@ -168,7 +169,14 @@ protected function restorePersistedData(CheckoutProcess $process)
$data = [];
}

$checksum = $this->cartChecksum->generateChecksum($this->context->cart);
if (is_array($checksum = $this->cartChecksum->generateChecksum($this->context->cart))) {
$this->checkoutWarning['address'] = array(
'id_address' => $checksum['id_address'],
'exception' => $checksum['exception'],
);
$checksum = null;
}

if (isset($data['checksum']) && $data['checksum'] === $checksum) {
$process->restorePersistedData($data);
}
Expand Down
25 changes: 21 additions & 4 deletions themes/_core/js/checkout-address.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,26 @@ export default function () {
$('#checkout-addresses-step').trigger('click');
prestashop.emit('editAddress');
});

$('#delivery-addresses, #invoice-addresses input[type=radio]').on('click', function () {
$('.address-item').removeClass('selected');
$('.address-item:has(input[type=radio]:checked)').addClass('selected');
$('.js-address-selector input[type=radio]:not(:checked)').on('click', function () {
$('button[name=confirm-addresses]').prop("disabled", "");
if (0 < $('.js-address-error').length) {
$('.js-address-error').hide();
var idFailureAddress = $(".js-address-error").prop('id').split('-').pop();
$('#id-address-delivery-address-' + idFailureAddress + ' a.edit-address').prop('style', 'color: #7a7a7a !important');
$('#id-address-invoice-address-' + idFailureAddress + ' a.edit-address').prop('style', 'color: #7a7a7a !important');
}
});
}

$(document).ready(() => {
if (0 < $('.js-address-error').length) {
var idFailureAddress = $(".js-address-error").prop('id').split('-').pop();
if ($(".js-address-error").attr('name').split('-').pop() == "delivery") {
$('#id-address-delivery-address-' + idFailureAddress + ' a.edit-address').prop('style', 'color: #2fb5d2 !important');
} else {
$('#id-address-invoice-address-' + idFailureAddress + ' a.edit-address').prop('style', 'color: #2fb5d2 !important');
}
$('button[name=confirm-addresses]').prop('disabled', 'disabled');
}
})
;
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
}
</div>

{if isset($delivery_address_error)}
<p class="alert alert-danger js-address-error" name="alert-delivery" id="id-failure-address-{$delivery_address_error.id_address}">{$delivery_address_error.exception}</p>
{/if}

<p class="add-address">
<a href="{$new_address_delivery_url}"><i class="material-icons">&#xE145;</i>{l s='add new address' d='Shop.Theme.Actions'}</a>
</p>
Expand Down Expand Up @@ -104,6 +108,10 @@
}
</div>

{if isset($invoice_address_error)}
<p class="alert alert-danger js-address-error" name="alert-invoice" id="id-failure-address-{$invoice_address_error.id_address}">{$invoice_address_error.exception}</p>
{/if}

<p class="add-address">
<a href="{$new_address_invoice_url}"><i class="material-icons">&#xE145;</i>{l s='add new address' d='Shop.Theme.Actions'}</a>
</p>
Expand Down

0 comments on commit 2b57651

Please sign in to comment.