first commit
This commit is contained in:
202
classes/checkout/AbstractCheckoutStep.php
Normal file
202
classes/checkout/AbstractCheckoutStep.php
Normal file
@@ -0,0 +1,202 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
abstract class AbstractCheckoutStepCore implements CheckoutStepInterface
|
||||
{
|
||||
private $smarty;
|
||||
private $translator;
|
||||
|
||||
/**
|
||||
* @var CheckoutProcess
|
||||
*/
|
||||
private $checkoutProcess;
|
||||
|
||||
private $title;
|
||||
|
||||
protected $step_is_reachable = false;
|
||||
protected $step_is_complete = false;
|
||||
protected $step_is_current = false;
|
||||
protected $context;
|
||||
|
||||
protected $template;
|
||||
protected $unreachableStepTemplate = 'checkout/_partials/steps/unreachable.tpl';
|
||||
|
||||
/**
|
||||
* @param Context $context
|
||||
* @param TranslatorInterface $translator
|
||||
*/
|
||||
public function __construct(Context $context, TranslatorInterface $translator)
|
||||
{
|
||||
$this->context = $context;
|
||||
$this->smarty = $context->smarty;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
public function setTemplate($templatePath)
|
||||
{
|
||||
$this->template = $templatePath;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTemplate()
|
||||
{
|
||||
if ($this->isReachable()) {
|
||||
return $this->template;
|
||||
} else {
|
||||
return $this->unreachableStepTemplate;
|
||||
}
|
||||
}
|
||||
|
||||
protected function getTranslator()
|
||||
{
|
||||
return $this->translator;
|
||||
}
|
||||
|
||||
protected function renderTemplate($template, array $extraParams = [], array $params = [])
|
||||
{
|
||||
$defaultParams = [
|
||||
'title' => $this->getTitle(),
|
||||
'step_is_complete' => (int) $this->isComplete(),
|
||||
'step_is_reachable' => (int) $this->isReachable(),
|
||||
'step_is_current' => (int) $this->isCurrent(),
|
||||
];
|
||||
|
||||
$scope = $this->smarty->createData(
|
||||
$this->smarty
|
||||
);
|
||||
|
||||
$scope->assign(array_merge($defaultParams, $extraParams, $params));
|
||||
|
||||
$tpl = $this->smarty->createTemplate(
|
||||
$template,
|
||||
$scope
|
||||
);
|
||||
|
||||
return $tpl->fetch();
|
||||
}
|
||||
|
||||
public function setTitle($title)
|
||||
{
|
||||
$this->title = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function setCheckoutProcess(CheckoutProcess $checkoutProcess)
|
||||
{
|
||||
$this->checkoutProcess = $checkoutProcess;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCheckoutProcess()
|
||||
{
|
||||
return $this->checkoutProcess;
|
||||
}
|
||||
|
||||
public function getCheckoutSession()
|
||||
{
|
||||
return $this->getCheckoutProcess()->getCheckoutSession();
|
||||
}
|
||||
|
||||
public function setReachable($step_is_reachable)
|
||||
{
|
||||
$this->step_is_reachable = $step_is_reachable;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isReachable()
|
||||
{
|
||||
return $this->step_is_reachable;
|
||||
}
|
||||
|
||||
public function setComplete($step_is_complete)
|
||||
{
|
||||
$this->step_is_complete = $step_is_complete;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isComplete()
|
||||
{
|
||||
return $this->step_is_complete;
|
||||
}
|
||||
|
||||
public function setCurrent($step_is_current)
|
||||
{
|
||||
$this->step_is_current = $step_is_current;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isCurrent()
|
||||
{
|
||||
return $this->step_is_current;
|
||||
}
|
||||
|
||||
public function getIdentifier()
|
||||
{
|
||||
// SomeClassNameLikeThis => some-class-name-like-this
|
||||
return Tools::camelCaseToKebabCase(get_class($this));
|
||||
}
|
||||
|
||||
public function getDataToPersist()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function restorePersistedData(array $data)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find next step and mark it as current
|
||||
*/
|
||||
public function setNextStepAsCurrent()
|
||||
{
|
||||
$steps = $this->getCheckoutProcess()->getSteps();
|
||||
$next = false;
|
||||
foreach ($steps as $step) {
|
||||
if ($next === true) {
|
||||
$step->step_is_current = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if ($step === $this) {
|
||||
$next = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
95
classes/checkout/AddressValidator.php
Normal file
95
classes/checkout/AddressValidator.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class AddressValidatorCore.
|
||||
*
|
||||
* Validates addresses held by common PrestaShop objects (cart, customer...)
|
||||
*/
|
||||
class AddressValidatorCore
|
||||
{
|
||||
/**
|
||||
* Validates cart addresses
|
||||
* Returns an array of invalid address IDs.
|
||||
*
|
||||
* @param Cart $cart
|
||||
* The cart holding the addresses to be inspected
|
||||
*
|
||||
* @return array
|
||||
* The invalid address ids. Empty if everything is ok.
|
||||
*/
|
||||
public function validateCartAddresses(Cart $cart)
|
||||
{
|
||||
$invalidAddressIds = [];
|
||||
$addressesIds = [
|
||||
$cart->id_address_delivery,
|
||||
$cart->id_address_invoice,
|
||||
];
|
||||
|
||||
foreach ($addressesIds as $idAddress) {
|
||||
$address = new CustomerAddress((int) $idAddress);
|
||||
|
||||
try {
|
||||
$address->validateFields();
|
||||
} catch (PrestaShopException $e) {
|
||||
$invalidAddressIds[] = (int) $idAddress;
|
||||
}
|
||||
}
|
||||
|
||||
return $invalidAddressIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates given customer's addresses
|
||||
* Returns an array of invalid address IDs.
|
||||
*
|
||||
* @param Customer $customer
|
||||
* The customer holding the addresses to be inspected
|
||||
* @param Language $language
|
||||
* The language in which addresses should be validated
|
||||
*
|
||||
* @return array The invalid address ids. Empty if everything is ok.
|
||||
* The invalid address ids. Empty if everything is ok.
|
||||
*/
|
||||
public function validateCustomerAddresses(Customer $customer, Language $language)
|
||||
{
|
||||
$invalidAddresses = [];
|
||||
$addresses = $customer->getAddresses($language->id);
|
||||
|
||||
if (is_array($addresses)) {
|
||||
foreach ($addresses as $address) {
|
||||
try {
|
||||
$adressObject = new CustomerAddress((int) $address['id_address']);
|
||||
$adressObject->validateFields();
|
||||
} catch (PrestaShopException $e) {
|
||||
$invalidAddresses[] = (int) $address['id_address'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $invalidAddresses;
|
||||
}
|
||||
}
|
||||
80
classes/checkout/CartChecksum.php
Normal file
80
classes/checkout/CartChecksum.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
class CartChecksumCore implements ChecksumInterface
|
||||
{
|
||||
public $addressChecksum = null;
|
||||
private $separator = '_';
|
||||
private $subseparator = '-';
|
||||
|
||||
public function __construct(AddressChecksum $addressChecksum)
|
||||
{
|
||||
$this->addressChecksum = $addressChecksum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $cart
|
||||
*
|
||||
* @return string cart SHA1
|
||||
*/
|
||||
public function generateChecksum($cart)
|
||||
{
|
||||
$uniq_id = '';
|
||||
$uniq_id .= $cart->id_shop;
|
||||
$uniq_id .= $this->separator;
|
||||
$uniq_id .= $cart->id_customer;
|
||||
$uniq_id .= $this->separator;
|
||||
$uniq_id .= $cart->id_guest;
|
||||
$uniq_id .= $this->separator;
|
||||
$uniq_id .= $cart->id_currency;
|
||||
$uniq_id .= $this->separator;
|
||||
$uniq_id .= $cart->id_lang;
|
||||
$uniq_id .= $this->separator;
|
||||
|
||||
$uniq_id .= $this->addressChecksum->generateChecksum(new Address($cart->id_address_delivery));
|
||||
$uniq_id .= $this->separator;
|
||||
$uniq_id .= $this->addressChecksum->generateChecksum(new Address($cart->id_address_invoice));
|
||||
$uniq_id .= $this->separator;
|
||||
|
||||
$products = $cart->getProducts($refresh = true);
|
||||
foreach ($products as $product) {
|
||||
$uniq_id .= $product['id_shop']
|
||||
. $this->subseparator
|
||||
. $product['id_product']
|
||||
. $this->subseparator
|
||||
. $product['id_product_attribute']
|
||||
. $this->subseparator
|
||||
. $product['cart_quantity']
|
||||
. $this->subseparator
|
||||
. $product['total_wt'];
|
||||
$uniq_id .= $this->separator;
|
||||
}
|
||||
|
||||
$uniq_id = rtrim($uniq_id, $this->separator);
|
||||
$uniq_id = rtrim($uniq_id, $this->subseparator);
|
||||
|
||||
return sha1($uniq_id);
|
||||
}
|
||||
}
|
||||
311
classes/checkout/CheckoutAddressesStep.php
Normal file
311
classes/checkout/CheckoutAddressesStep.php
Normal file
@@ -0,0 +1,311 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
class CheckoutAddressesStepCore extends AbstractCheckoutStep
|
||||
{
|
||||
protected $template = 'checkout/_partials/steps/addresses.tpl';
|
||||
|
||||
private $addressForm;
|
||||
private $use_same_address = true;
|
||||
private $show_delivery_address_form = false;
|
||||
private $show_invoice_address_form = false;
|
||||
private $form_has_continue_button = false;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
TranslatorInterface $translator,
|
||||
CustomerAddressForm $addressForm
|
||||
) {
|
||||
parent::__construct($context, $translator);
|
||||
$this->addressForm = $addressForm;
|
||||
}
|
||||
|
||||
public function getDataToPersist()
|
||||
{
|
||||
return [
|
||||
'use_same_address' => $this->use_same_address,
|
||||
];
|
||||
}
|
||||
|
||||
public function restorePersistedData(array $data)
|
||||
{
|
||||
if (array_key_exists('use_same_address', $data)) {
|
||||
$this->use_same_address = $data['use_same_address'];
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function handleRequest(array $requestParams = [])
|
||||
{
|
||||
$this->addressForm->setAction($this->getCheckoutSession()->getCheckoutURL());
|
||||
|
||||
if (array_key_exists('use_same_address', $requestParams)) {
|
||||
$this->use_same_address = (bool) $requestParams['use_same_address'];
|
||||
if (!$this->use_same_address) {
|
||||
$this->setCurrent(true);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($requestParams['cancelAddress'])) {
|
||||
if ($requestParams['cancelAddress'] === 'invoice') {
|
||||
if ($this->getCheckoutSession()->getCustomerAddressesCount() < 2) {
|
||||
$this->use_same_address = true;
|
||||
}
|
||||
}
|
||||
$this->setCurrent(true);
|
||||
}
|
||||
|
||||
// Can't really hurt to set the firstname and lastname.
|
||||
$this->addressForm->fillWith([
|
||||
'firstname' => $this->getCheckoutSession()->getCustomer()->firstname,
|
||||
'lastname' => $this->getCheckoutSession()->getCustomer()->lastname,
|
||||
]);
|
||||
|
||||
if (isset($requestParams['saveAddress'])) {
|
||||
$saved = $this->addressForm->fillWith($requestParams)->submit();
|
||||
if (!$saved) {
|
||||
$this->setCurrent(true);
|
||||
$this->getCheckoutProcess()->setHasErrors(true);
|
||||
if ($requestParams['saveAddress'] === 'delivery') {
|
||||
$this->show_delivery_address_form = true;
|
||||
} else {
|
||||
$this->show_invoice_address_form = true;
|
||||
}
|
||||
} else {
|
||||
if ($requestParams['saveAddress'] === 'delivery') {
|
||||
$this->use_same_address = isset($requestParams['use_same_address']);
|
||||
}
|
||||
$id_address = $this->addressForm->getAddress()->id;
|
||||
if ($requestParams['saveAddress'] === 'delivery') {
|
||||
$this->getCheckoutSession()->setIdAddressDelivery($id_address);
|
||||
$idAddressInvoice = $this->use_same_address ? $id_address : null;
|
||||
$this->getCheckoutSession()->setIdAddressInvoice($idAddressInvoice);
|
||||
} else {
|
||||
$this->getCheckoutSession()->setIdAddressInvoice($id_address);
|
||||
}
|
||||
}
|
||||
} elseif (isset($requestParams['newAddress'])) {
|
||||
// while a form is open, do not go to next step
|
||||
$this->setCurrent(true);
|
||||
if ($requestParams['newAddress'] === 'delivery') {
|
||||
$this->show_delivery_address_form = true;
|
||||
} else {
|
||||
$this->show_invoice_address_form = true;
|
||||
}
|
||||
$this->addressForm->fillWith($requestParams);
|
||||
$this->form_has_continue_button = $this->use_same_address;
|
||||
} elseif (isset($requestParams['editAddress'])) {
|
||||
// while a form is open, do not go to next step
|
||||
$this->setCurrent(true);
|
||||
if ($requestParams['editAddress'] === 'delivery') {
|
||||
$this->show_delivery_address_form = true;
|
||||
} else {
|
||||
$this->show_invoice_address_form = true;
|
||||
}
|
||||
$this->addressForm->loadAddressById($requestParams['id_address']);
|
||||
} elseif (isset($requestParams['deleteAddress'])) {
|
||||
$addressPersister = new CustomerAddressPersister(
|
||||
$this->context->customer,
|
||||
$this->context->cart,
|
||||
Tools::getToken(true, $this->context)
|
||||
);
|
||||
|
||||
$deletionResult = (bool) $addressPersister->delete(
|
||||
new Address((int) Tools::getValue('id_address'), $this->context->language->id),
|
||||
Tools::getValue('token')
|
||||
);
|
||||
if ($deletionResult) {
|
||||
$this->context->controller->success[] = $this->getTranslator()->trans(
|
||||
'Address successfully deleted!',
|
||||
[],
|
||||
'Shop.Notifications.Success'
|
||||
);
|
||||
$this->context->controller->redirectWithNotifications(
|
||||
$this->getCheckoutSession()->getCheckoutURL()
|
||||
);
|
||||
} else {
|
||||
$this->getCheckoutProcess()->setHasErrors(true);
|
||||
$this->context->controller->errors[] = $this->getTranslator()->trans(
|
||||
'Could not delete address.',
|
||||
[],
|
||||
'Shop.Notifications.Error'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($requestParams['confirm-addresses'])) {
|
||||
if (isset($requestParams['id_address_delivery'])) {
|
||||
$id_address = $requestParams['id_address_delivery'];
|
||||
|
||||
if (!Customer::customerHasAddress($this->getCheckoutSession()->getCustomer()->id, $id_address)) {
|
||||
$this->getCheckoutProcess()->setHasErrors(true);
|
||||
} else {
|
||||
if ($this->getCheckoutSession()->getIdAddressDelivery() != $id_address) {
|
||||
$this->setCurrent(true);
|
||||
$this->getCheckoutProcess()->invalidateAllStepsAfterCurrent();
|
||||
}
|
||||
|
||||
$this->getCheckoutSession()->setIdAddressDelivery($id_address);
|
||||
if ($this->use_same_address) {
|
||||
$this->getCheckoutSession()->setIdAddressInvoice($id_address);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($requestParams['id_address_invoice'])) {
|
||||
$id_address = $requestParams['id_address_invoice'];
|
||||
if (!Customer::customerHasAddress($this->getCheckoutSession()->getCustomer()->id, $id_address)) {
|
||||
$this->getCheckoutProcess()->setHasErrors(true);
|
||||
} else {
|
||||
$this->getCheckoutSession()->setIdAddressInvoice($id_address);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$this->getCheckoutProcess()->hasErrors()) {
|
||||
$this->setNextStepAsCurrent();
|
||||
$this->setComplete(
|
||||
$this->getCheckoutSession()->getIdAddressInvoice() &&
|
||||
$this->getCheckoutSession()->getIdAddressDelivery()
|
||||
);
|
||||
|
||||
// if we just pushed the invoice address form, we are using another address for invoice
|
||||
// (param 'id_address_delivery' is only pushed in invoice address form)
|
||||
if (isset($requestParams['saveAddress'], $requestParams['id_address_delivery'])) {
|
||||
$this->use_same_address = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$addresses_count = $this->getCheckoutSession()->getCustomerAddressesCount();
|
||||
|
||||
if ($addresses_count === 0) {
|
||||
$this->show_delivery_address_form = true;
|
||||
} elseif ($addresses_count < 2 && !$this->use_same_address) {
|
||||
$this->show_invoice_address_form = true;
|
||||
$this->setComplete(false);
|
||||
}
|
||||
|
||||
if ($this->show_invoice_address_form) {
|
||||
// show continue button because form is at the end of the step
|
||||
$this->form_has_continue_button = true;
|
||||
} elseif ($this->show_delivery_address_form) {
|
||||
// only show continue button if we're sure
|
||||
// our form is at the bottom of the step
|
||||
if ($this->use_same_address || $addresses_count < 2) {
|
||||
$this->form_has_continue_button = true;
|
||||
}
|
||||
}
|
||||
|
||||
$this->setTitle($this->getTranslator()->trans('Addresses', [], 'Shop.Theme.Checkout'));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTemplateParameters()
|
||||
{
|
||||
$idAddressDelivery = (int) $this->getCheckoutSession()->getIdAddressDelivery();
|
||||
$idAddressInvoice = (int) $this->getCheckoutSession()->getIdAddressInvoice();
|
||||
$params = [
|
||||
'address_form' => $this->addressForm->getProxy(),
|
||||
'use_same_address' => $this->use_same_address,
|
||||
'use_different_address_url' => $this->context->link->getPageLink(
|
||||
'order',
|
||||
true,
|
||||
null,
|
||||
['use_same_address' => 0]
|
||||
),
|
||||
'new_address_delivery_url' => $this->context->link->getPageLink(
|
||||
'order',
|
||||
true,
|
||||
null,
|
||||
['newAddress' => 'delivery']
|
||||
),
|
||||
'new_address_invoice_url' => $this->context->link->getPageLink(
|
||||
'order',
|
||||
true,
|
||||
null,
|
||||
['newAddress' => 'invoice']
|
||||
),
|
||||
'id_address' => (int) Tools::getValue('id_address'),
|
||||
'id_address_delivery' => $idAddressDelivery,
|
||||
'id_address_invoice' => $idAddressInvoice,
|
||||
'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,
|
||||
];
|
||||
|
||||
/** @var OrderControllerCore $controller */
|
||||
$controller = $this->context->controller;
|
||||
if (isset($controller)) {
|
||||
$warnings = $controller->checkoutWarning;
|
||||
$addressWarning = isset($warnings['address'])
|
||||
? $warnings['address']
|
||||
: false;
|
||||
$invalidAddresses = isset($warnings['invalid_addresses'])
|
||||
? $warnings['invalid_addresses']
|
||||
: [];
|
||||
|
||||
$errors = [];
|
||||
if (in_array($idAddressDelivery, $invalidAddresses)) {
|
||||
$errors['delivery_address_error'] = $addressWarning;
|
||||
}
|
||||
|
||||
if (in_array($idAddressInvoice, $invalidAddresses)) {
|
||||
$errors['invoice_address_error'] = $addressWarning;
|
||||
}
|
||||
|
||||
if ($this->show_invoice_address_form
|
||||
|| $idAddressInvoice != $idAddressDelivery
|
||||
|| !empty($errors['invoice_address_error'])
|
||||
) {
|
||||
$this->use_same_address = false;
|
||||
}
|
||||
|
||||
// Add specific parameters
|
||||
$params = array_replace(
|
||||
$params,
|
||||
[
|
||||
'not_valid_addresses' => implode(',', $invalidAddresses),
|
||||
'use_same_address' => $this->use_same_address,
|
||||
],
|
||||
$errors
|
||||
);
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
public function render(array $extraParams = [])
|
||||
{
|
||||
return $this->renderTemplate(
|
||||
$this->getTemplate(),
|
||||
$extraParams,
|
||||
$this->getTemplateParameters()
|
||||
);
|
||||
}
|
||||
}
|
||||
215
classes/checkout/CheckoutDeliveryStep.php
Normal file
215
classes/checkout/CheckoutDeliveryStep.php
Normal file
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
use PrestaShop\PrestaShop\Adapter\Product\PriceFormatter;
|
||||
|
||||
class CheckoutDeliveryStepCore extends AbstractCheckoutStep
|
||||
{
|
||||
protected $template = 'checkout/_partials/steps/shipping.tpl';
|
||||
|
||||
private $recyclablePackAllowed = false;
|
||||
private $giftAllowed = false;
|
||||
private $giftCost = 0;
|
||||
private $includeTaxes = false;
|
||||
private $displayTaxesLabel = false;
|
||||
|
||||
public function setRecyclablePackAllowed($recyclablePackAllowed)
|
||||
{
|
||||
$this->recyclablePackAllowed = $recyclablePackAllowed;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isRecyclablePackAllowed()
|
||||
{
|
||||
return $this->recyclablePackAllowed;
|
||||
}
|
||||
|
||||
public function setGiftAllowed($giftAllowed)
|
||||
{
|
||||
$this->giftAllowed = $giftAllowed;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isGiftAllowed()
|
||||
{
|
||||
return $this->giftAllowed;
|
||||
}
|
||||
|
||||
public function setGiftCost($giftCost)
|
||||
{
|
||||
$this->giftCost = $giftCost;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getGiftCost()
|
||||
{
|
||||
return $this->giftCost;
|
||||
}
|
||||
|
||||
public function setIncludeTaxes($includeTaxes)
|
||||
{
|
||||
$this->includeTaxes = $includeTaxes;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getIncludeTaxes()
|
||||
{
|
||||
return $this->includeTaxes;
|
||||
}
|
||||
|
||||
public function setDisplayTaxesLabel($displayTaxesLabel)
|
||||
{
|
||||
$this->displayTaxesLabel = $displayTaxesLabel;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDisplayTaxesLabel()
|
||||
{
|
||||
return $this->displayTaxesLabel;
|
||||
}
|
||||
|
||||
public function getGiftCostForLabel()
|
||||
{
|
||||
if ($this->getGiftCost() != 0) {
|
||||
$taxLabel = '';
|
||||
$priceFormatter = new PriceFormatter();
|
||||
|
||||
if ($this->getIncludeTaxes() && $this->getDisplayTaxesLabel()) {
|
||||
$taxLabel .= $this->getTranslator()->trans('tax incl.', [], 'Shop.Theme.Checkout');
|
||||
} elseif ($this->getDisplayTaxesLabel()) {
|
||||
$taxLabel .= $this->getTranslator()->trans('tax excl.', [], 'Shop.Theme.Checkout');
|
||||
}
|
||||
|
||||
return $this->getTranslator()->trans(
|
||||
'(additional cost of %giftcost% %taxlabel%)',
|
||||
[
|
||||
'%giftcost%' => $priceFormatter->convertAndFormat($this->getGiftCost()),
|
||||
'%taxlabel%' => $taxLabel,
|
||||
],
|
||||
'Shop.Theme.Checkout'
|
||||
);
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
public function handleRequest(array $requestParams = [])
|
||||
{
|
||||
if (isset($requestParams['delivery_option'])) {
|
||||
$this->setComplete(false);
|
||||
$this->getCheckoutSession()->setDeliveryOption(
|
||||
$requestParams['delivery_option']
|
||||
);
|
||||
$this->getCheckoutSession()->setRecyclable(
|
||||
isset($requestParams['recyclable']) ? $requestParams['recyclable'] : false
|
||||
);
|
||||
|
||||
$useGift = isset($requestParams['gift']) ? $requestParams['gift'] : false;
|
||||
$this->getCheckoutSession()->setGift(
|
||||
$useGift,
|
||||
($useGift && isset($requestParams['gift_message'])) ? $requestParams['gift_message'] : ''
|
||||
);
|
||||
}
|
||||
|
||||
if (isset($requestParams['delivery_message'])) {
|
||||
$this->getCheckoutSession()->setMessage($requestParams['delivery_message']);
|
||||
}
|
||||
|
||||
if ($this->isReachable() && isset($requestParams['confirmDeliveryOption'])) {
|
||||
// we're done if
|
||||
// - the step was reached (= all previous steps complete)
|
||||
// - user has clicked on "continue"
|
||||
// - there are delivery options
|
||||
// - the is a selected delivery option
|
||||
// - the module associated to the delivery option confirms
|
||||
$deliveryOptions = $this->getCheckoutSession()->getDeliveryOptions();
|
||||
$this->setNextStepAsCurrent();
|
||||
$this->setComplete(
|
||||
!empty($deliveryOptions)
|
||||
&& $this->getCheckoutSession()->getSelectedDeliveryOption()
|
||||
&& $this->isModuleComplete($requestParams)
|
||||
);
|
||||
}
|
||||
|
||||
$this->setTitle($this->getTranslator()->trans('Shipping Method', [], 'Shop.Theme.Checkout'));
|
||||
|
||||
Hook::exec('actionCarrierProcess', ['cart' => $this->getCheckoutSession()->getCart()]);
|
||||
}
|
||||
|
||||
public function render(array $extraParams = [])
|
||||
{
|
||||
return $this->renderTemplate(
|
||||
$this->getTemplate(),
|
||||
$extraParams,
|
||||
[
|
||||
'hookDisplayBeforeCarrier' => Hook::exec('displayBeforeCarrier', ['cart' => $this->getCheckoutSession()->getCart()]),
|
||||
'hookDisplayAfterCarrier' => Hook::exec('displayAfterCarrier', ['cart' => $this->getCheckoutSession()->getCart()]),
|
||||
'id_address' => $this->getCheckoutSession()->getIdAddressDelivery(),
|
||||
'delivery_options' => $this->getCheckoutSession()->getDeliveryOptions(),
|
||||
'delivery_option' => $this->getCheckoutSession()->getSelectedDeliveryOption(),
|
||||
'recyclable' => $this->getCheckoutSession()->isRecyclable(),
|
||||
'recyclablePackAllowed' => $this->isRecyclablePackAllowed(),
|
||||
'delivery_message' => $this->getCheckoutSession()->getMessage(),
|
||||
'gift' => [
|
||||
'allowed' => $this->isGiftAllowed(),
|
||||
'isGift' => $this->getCheckoutSession()->getGift()['isGift'],
|
||||
'label' => $this->getTranslator()->trans(
|
||||
'I would like my order to be gift wrapped %cost%',
|
||||
['%cost%' => $this->getGiftCostForLabel()],
|
||||
'Shop.Theme.Checkout'
|
||||
),
|
||||
'message' => $this->getCheckoutSession()->getGift()['message'],
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
protected function isModuleComplete($requestParams)
|
||||
{
|
||||
$deliveryOptions = $this->getCheckoutSession()->getDeliveryOptions();
|
||||
$currentDeliveryOption = $deliveryOptions[$this->getCheckoutSession()->getSelectedDeliveryOption()];
|
||||
if (!$currentDeliveryOption['is_module']) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$isComplete = true;
|
||||
Hook::exec(
|
||||
'actionValidateStepComplete',
|
||||
[
|
||||
'step_name' => 'delivery',
|
||||
'request_params' => $requestParams,
|
||||
'completed' => &$isComplete,
|
||||
],
|
||||
Module::getModuleIdByName($currentDeliveryOption['external_module_name'])
|
||||
);
|
||||
|
||||
return $isComplete;
|
||||
}
|
||||
}
|
||||
110
classes/checkout/CheckoutPaymentStep.php
Normal file
110
classes/checkout/CheckoutPaymentStep.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
class CheckoutPaymentStepCore extends AbstractCheckoutStep
|
||||
{
|
||||
protected $template = 'checkout/_partials/steps/payment.tpl';
|
||||
private $selected_payment_option;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
TranslatorInterface $translator,
|
||||
PaymentOptionsFinder $paymentOptionsFinder,
|
||||
ConditionsToApproveFinder $conditionsToApproveFinder
|
||||
) {
|
||||
parent::__construct($context, $translator);
|
||||
$this->paymentOptionsFinder = $paymentOptionsFinder;
|
||||
$this->conditionsToApproveFinder = $conditionsToApproveFinder;
|
||||
}
|
||||
|
||||
public function handleRequest(array $requestParams = [])
|
||||
{
|
||||
$cart = $this->getCheckoutSession()->getCart();
|
||||
$allProductsInStock = $cart->isAllProductsInStock();
|
||||
$allProductsExist = $cart->checkAllProductsAreStillAvailableInThisState();
|
||||
$allProductsHaveMinimalQuantity = $cart->checkAllProductsHaveMinimalQuantities();
|
||||
|
||||
if ($allProductsInStock !== true || $allProductsExist !== true || $allProductsHaveMinimalQuantity !== true) {
|
||||
$cartShowUrl = $this->context->link->getPageLink(
|
||||
'cart',
|
||||
null,
|
||||
$this->context->language->id,
|
||||
[
|
||||
'action' => 'show',
|
||||
],
|
||||
false,
|
||||
null,
|
||||
false
|
||||
);
|
||||
Tools::redirect($cartShowUrl);
|
||||
}
|
||||
|
||||
if (isset($requestParams['select_payment_option'])) {
|
||||
$this->selected_payment_option = $requestParams['select_payment_option'];
|
||||
}
|
||||
|
||||
$this->setTitle(
|
||||
$this->getTranslator()->trans(
|
||||
'Payment',
|
||||
[],
|
||||
'Shop.Theme.Checkout'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $extraParams
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render(array $extraParams = [])
|
||||
{
|
||||
$isFree = 0 == (float) $this->getCheckoutSession()->getCart()->getOrderTotal(true, Cart::BOTH);
|
||||
$paymentOptions = $this->paymentOptionsFinder->present($isFree);
|
||||
$conditionsToApprove = $this->conditionsToApproveFinder->getConditionsToApproveForTemplate();
|
||||
$deliveryOptions = $this->getCheckoutSession()->getDeliveryOptions();
|
||||
$deliveryOptionKey = $this->getCheckoutSession()->getSelectedDeliveryOption();
|
||||
|
||||
if (isset($deliveryOptions[$deliveryOptionKey])) {
|
||||
$selectedDeliveryOption = $deliveryOptions[$deliveryOptionKey];
|
||||
} else {
|
||||
$selectedDeliveryOption = 0;
|
||||
}
|
||||
unset($selectedDeliveryOption['product_list']);
|
||||
|
||||
$assignedVars = [
|
||||
'is_free' => $isFree,
|
||||
'payment_options' => $paymentOptions,
|
||||
'conditions_to_approve' => $conditionsToApprove,
|
||||
'selected_payment_option' => $this->selected_payment_option,
|
||||
'selected_delivery_option' => $selectedDeliveryOption,
|
||||
'show_final_summary' => Configuration::get('PS_FINAL_SUMMARY_ENABLED'),
|
||||
];
|
||||
|
||||
return $this->renderTemplate($this->getTemplate(), $extraParams, $assignedVars);
|
||||
}
|
||||
}
|
||||
116
classes/checkout/CheckoutPersonalInformationStep.php
Normal file
116
classes/checkout/CheckoutPersonalInformationStep.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
class CheckoutPersonalInformationStepCore extends AbstractCheckoutStep
|
||||
{
|
||||
protected $template = 'checkout/_partials/steps/personal-information.tpl';
|
||||
private $loginForm;
|
||||
private $registerForm;
|
||||
|
||||
private $show_login_form = false;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
TranslatorInterface $translator,
|
||||
CustomerLoginForm $loginForm,
|
||||
CustomerForm $registerForm
|
||||
) {
|
||||
parent::__construct($context, $translator);
|
||||
$this->loginForm = $loginForm;
|
||||
$this->registerForm = $registerForm;
|
||||
}
|
||||
|
||||
public function handleRequest(array $requestParameters = [])
|
||||
{
|
||||
// personal info step is always reachable
|
||||
$this->setReachable(true);
|
||||
|
||||
$this->registerForm
|
||||
->fillFromCustomer(
|
||||
$this
|
||||
->getCheckoutProcess()
|
||||
->getCheckoutSession()
|
||||
->getCustomer()
|
||||
);
|
||||
|
||||
if (isset($requestParameters['submitCreate'])) {
|
||||
$this->registerForm->fillWith($requestParameters);
|
||||
if ($this->registerForm->submit()) {
|
||||
$this->setNextStepAsCurrent();
|
||||
$this->setComplete(true);
|
||||
} else {
|
||||
$this->setComplete(false);
|
||||
$this->setCurrent(true);
|
||||
$this->getCheckoutProcess()->setHasErrors(true)->setNextStepReachable();
|
||||
}
|
||||
} elseif (isset($requestParameters['submitLogin'])) {
|
||||
$this->loginForm->fillWith($requestParameters);
|
||||
if ($this->loginForm->submit()) {
|
||||
$this->setNextStepAsCurrent();
|
||||
$this->setComplete(true);
|
||||
} else {
|
||||
$this->getCheckoutProcess()->setHasErrors(true);
|
||||
$this->show_login_form = true;
|
||||
}
|
||||
} elseif (array_key_exists('login', $requestParameters)) {
|
||||
$this->show_login_form = true;
|
||||
$this->setCurrent(true);
|
||||
}
|
||||
|
||||
$this->logged_in = $this
|
||||
->getCheckoutProcess()
|
||||
->getCheckoutSession()
|
||||
->customerHasLoggedIn();
|
||||
|
||||
if ($this->logged_in && !$this->getCheckoutSession()->getCustomer()->is_guest) {
|
||||
$this->setComplete(true);
|
||||
}
|
||||
|
||||
$this->setTitle(
|
||||
$this->getTranslator()->trans(
|
||||
'Personal Information',
|
||||
[],
|
||||
'Shop.Theme.Checkout'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function render(array $extraParams = [])
|
||||
{
|
||||
return $this->renderTemplate(
|
||||
$this->getTemplate(),
|
||||
$extraParams,
|
||||
[
|
||||
'show_login_form' => $this->show_login_form,
|
||||
'login_form' => $this->loginForm->getProxy(),
|
||||
'register_form' => $this->registerForm->getProxy(),
|
||||
'guest_allowed' => $this->getCheckoutSession()->isGuestAllowed(),
|
||||
'empty_cart_on_logout' => !Configuration::get('PS_CART_FOLLOWING'),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
299
classes/checkout/CheckoutProcess.php
Normal file
299
classes/checkout/CheckoutProcess.php
Normal file
@@ -0,0 +1,299 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
use PrestaShop\PrestaShop\Core\Foundation\Templating\RenderableInterface;
|
||||
use PrestaShop\PrestaShop\Core\Foundation\Templating\RenderableProxy;
|
||||
|
||||
class CheckoutProcessCore implements RenderableInterface
|
||||
{
|
||||
private $smarty;
|
||||
|
||||
/**
|
||||
* @var CheckoutSession
|
||||
*/
|
||||
private $checkoutSession;
|
||||
/** @var array CheckoutStepInterface[] */
|
||||
private $steps = [];
|
||||
/** @var bool */
|
||||
private $has_errors;
|
||||
|
||||
/** @var string */
|
||||
private $template = 'checkout/checkout-process.tpl';
|
||||
/** @var Context */
|
||||
protected $context;
|
||||
|
||||
/**
|
||||
* @param Context $context
|
||||
* @param CheckoutSession $checkoutSession
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
CheckoutSession $checkoutSession
|
||||
) {
|
||||
$this->context = $context;
|
||||
$this->smarty = $context->smarty;
|
||||
$this->checkoutSession = $checkoutSession;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTemplate()
|
||||
{
|
||||
return $this->template;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $requestParameters
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function handleRequest(array $requestParameters = [])
|
||||
{
|
||||
foreach ($this->getSteps() as $step) {
|
||||
$step->handleRequest($requestParameters);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return CheckoutSession
|
||||
*/
|
||||
public function getCheckoutSession()
|
||||
{
|
||||
return $this->checkoutSession;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CheckoutStepInterface $step
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addStep(CheckoutStepInterface $step)
|
||||
{
|
||||
$step->setCheckoutProcess($this);
|
||||
$this->steps[] = $step;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return CheckoutStepInterface[]
|
||||
*/
|
||||
public function getSteps()
|
||||
{
|
||||
return $this->steps;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $steps
|
||||
*/
|
||||
public function setSteps(array $steps)
|
||||
{
|
||||
$this->steps = $steps;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $templatePath
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplate($templatePath)
|
||||
{
|
||||
$this->template = $templatePath;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $extraParams
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws SmartyException
|
||||
*/
|
||||
public function render(array $extraParams = [])
|
||||
{
|
||||
$scope = $this->smarty->createData(
|
||||
$this->smarty
|
||||
);
|
||||
|
||||
$params = [
|
||||
'steps' => array_map(function (CheckoutStepInterface $step) {
|
||||
return [
|
||||
'identifier' => $step->getIdentifier(),
|
||||
'ui' => new RenderableProxy($step),
|
||||
];
|
||||
}, $this->getSteps()),
|
||||
];
|
||||
|
||||
$scope->assign(array_merge($extraParams, $params));
|
||||
|
||||
$tpl = $this->smarty->createTemplate(
|
||||
$this->template,
|
||||
$scope
|
||||
);
|
||||
|
||||
return $tpl->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $has_errors
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setHasErrors($has_errors = true)
|
||||
{
|
||||
$this->has_errors = $has_errors;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasErrors()
|
||||
{
|
||||
return $this->has_errors;
|
||||
}
|
||||
|
||||
public function getDataToPersist()
|
||||
{
|
||||
$data = [];
|
||||
foreach ($this->getSteps() as $step) {
|
||||
$defaultStepData = [
|
||||
'step_is_reachable' => $step->isReachable(),
|
||||
'step_is_complete' => $step->isComplete(),
|
||||
];
|
||||
|
||||
$stepData = array_merge($defaultStepData, $step->getDataToPersist());
|
||||
|
||||
$data[$step->getIdentifier()] = $stepData;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function restorePersistedData(array $data)
|
||||
{
|
||||
foreach ($this->getSteps() as $step) {
|
||||
$id = $step->getIdentifier();
|
||||
if (array_key_exists($id, $data)) {
|
||||
$stepData = $data[$id];
|
||||
$step
|
||||
->setReachable($stepData['step_is_reachable'])
|
||||
->setComplete($stepData['step_is_complete'])
|
||||
->restorePersistedData($stepData);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setNextStepReachable()
|
||||
{
|
||||
foreach ($this->getSteps() as $step) {
|
||||
if (!$step->isReachable()) {
|
||||
$step->setReachable(true);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (!$step->isComplete()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function markCurrentStep()
|
||||
{
|
||||
$steps = $this->getSteps();
|
||||
|
||||
foreach ($steps as $step) {
|
||||
if ($step->isCurrent()) {
|
||||
// If a step marked itself as current
|
||||
// then we assume it has a good reason
|
||||
// to do so and we don't auto-advance.
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($steps as $position => $step) {
|
||||
$nextStep = ($position < count($steps) - 1) ? $steps[$position + 1] : null;
|
||||
|
||||
if ($step->isReachable() && (!$step->isComplete() || ($nextStep && !$nextStep->isReachable()))) {
|
||||
$step->setCurrent(true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function invalidateAllStepsAfterCurrent()
|
||||
{
|
||||
$markAsUnreachable = false;
|
||||
foreach ($this->getSteps() as $step) {
|
||||
if ($markAsUnreachable) {
|
||||
$step->setComplete(false)->setReachable(false);
|
||||
}
|
||||
|
||||
if ($step->isCurrent()) {
|
||||
$markAsUnreachable = true;
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return CheckoutStepInterface
|
||||
*
|
||||
* @throws \RuntimeException if no current step is found
|
||||
*/
|
||||
public function getCurrentStep()
|
||||
{
|
||||
foreach ($this->getSteps() as $step) {
|
||||
if ($step->isCurrent()) {
|
||||
return $step;
|
||||
}
|
||||
}
|
||||
|
||||
throw new \RuntimeException('There should be at least one current step');
|
||||
}
|
||||
}
|
||||
199
classes/checkout/CheckoutSession.php
Normal file
199
classes/checkout/CheckoutSession.php
Normal file
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
class CheckoutSessionCore
|
||||
{
|
||||
/** @var Context */
|
||||
protected $context;
|
||||
/** @var DeliveryOptionsFinder */
|
||||
protected $deliveryOptionsFinder;
|
||||
|
||||
/**
|
||||
* @param Context $context
|
||||
* @param DeliveryOptionsFinder $deliveryOptionsFinder
|
||||
*/
|
||||
public function __construct(Context $context, DeliveryOptionsFinder $deliveryOptionsFinder)
|
||||
{
|
||||
$this->context = $context;
|
||||
$this->deliveryOptionsFinder = $deliveryOptionsFinder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function customerHasLoggedIn()
|
||||
{
|
||||
return $this->context->customer->isLogged();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Customer
|
||||
*/
|
||||
public function getCustomer()
|
||||
{
|
||||
return $this->context->customer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Cart
|
||||
*/
|
||||
public function getCart()
|
||||
{
|
||||
return $this->context->cart;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getCustomerAddressesCount()
|
||||
{
|
||||
return count($this->getCustomer()->getSimpleAddresses(
|
||||
$this->context->language->id,
|
||||
true // no cache
|
||||
));
|
||||
}
|
||||
|
||||
public function setIdAddressDelivery($id_address)
|
||||
{
|
||||
$this->context->cart->updateAddressId($this->context->cart->id_address_delivery, $id_address);
|
||||
$this->context->cart->id_address_delivery = $id_address;
|
||||
$this->context->cart->save();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setIdAddressInvoice($id_address)
|
||||
{
|
||||
$this->context->cart->id_address_invoice = $id_address;
|
||||
$this->context->cart->save();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getIdAddressDelivery()
|
||||
{
|
||||
return $this->context->cart->id_address_delivery;
|
||||
}
|
||||
|
||||
public function getIdAddressInvoice()
|
||||
{
|
||||
return $this->context->cart->id_address_invoice;
|
||||
}
|
||||
|
||||
public function setMessage($message)
|
||||
{
|
||||
$this->_updateMessage($message);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getMessage()
|
||||
{
|
||||
if ($message = Message::getMessageByCartId($this->context->cart->id)) {
|
||||
return $message['message'];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function _updateMessage($messageContent)
|
||||
{
|
||||
if ($messageContent) {
|
||||
if ($oldMessage = Message::getMessageByCartId((int) $this->context->cart->id)) {
|
||||
$message = new Message((int) $oldMessage['id_message']);
|
||||
$message->message = $messageContent;
|
||||
$message->update();
|
||||
} else {
|
||||
$message = new Message();
|
||||
$message->message = $messageContent;
|
||||
$message->id_cart = (int) $this->context->cart->id;
|
||||
$message->id_customer = (int) $this->context->cart->id_customer;
|
||||
$message->add();
|
||||
}
|
||||
} else {
|
||||
if ($oldMessage = Message::getMessageByCartId($this->context->cart->id)) {
|
||||
$message = new Message($oldMessage['id_message']);
|
||||
$message->delete();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function setDeliveryOption($option)
|
||||
{
|
||||
$this->context->cart->setDeliveryOption($option);
|
||||
|
||||
return $this->context->cart->update();
|
||||
}
|
||||
|
||||
public function getSelectedDeliveryOption()
|
||||
{
|
||||
return $this->deliveryOptionsFinder->getSelectedDeliveryOption();
|
||||
}
|
||||
|
||||
public function getDeliveryOptions()
|
||||
{
|
||||
return $this->deliveryOptionsFinder->getDeliveryOptions();
|
||||
}
|
||||
|
||||
public function setRecyclable($option)
|
||||
{
|
||||
$this->context->cart->recyclable = (int) $option;
|
||||
|
||||
return $this->context->cart->update();
|
||||
}
|
||||
|
||||
public function isRecyclable()
|
||||
{
|
||||
return $this->context->cart->recyclable;
|
||||
}
|
||||
|
||||
public function setGift($gift, $gift_message)
|
||||
{
|
||||
$this->context->cart->gift = (int) $gift;
|
||||
$this->context->cart->gift_message = $gift_message;
|
||||
|
||||
return $this->context->cart->update();
|
||||
}
|
||||
|
||||
public function getGift()
|
||||
{
|
||||
return [
|
||||
'isGift' => $this->context->cart->gift,
|
||||
'message' => $this->context->cart->gift_message,
|
||||
];
|
||||
}
|
||||
|
||||
public function isGuestAllowed()
|
||||
{
|
||||
return Configuration::get('PS_GUEST_CHECKOUT_ENABLED');
|
||||
}
|
||||
|
||||
public function getCheckoutURL()
|
||||
{
|
||||
return $this->context->link->getPageLink('order');
|
||||
}
|
||||
}
|
||||
49
classes/checkout/CheckoutStepInterface.php
Normal file
49
classes/checkout/CheckoutStepInterface.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
use PrestaShop\PrestaShop\Core\Foundation\Templating\RenderableInterface;
|
||||
|
||||
interface CheckoutStepInterface extends RenderableInterface
|
||||
{
|
||||
public function getTitle();
|
||||
|
||||
public function handleRequest(array $requestParameters = []);
|
||||
|
||||
public function setCheckoutProcess(CheckoutProcess $checkoutProcess);
|
||||
|
||||
public function isReachable();
|
||||
|
||||
public function isComplete();
|
||||
|
||||
public function isCurrent();
|
||||
|
||||
public function getIdentifier();
|
||||
|
||||
public function getDataToPersist();
|
||||
|
||||
public function restorePersistedData(array $data);
|
||||
|
||||
public function getTemplate();
|
||||
}
|
||||
106
classes/checkout/ConditionsToApproveFinder.php
Normal file
106
classes/checkout/ConditionsToApproveFinder.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
use PrestaShop\PrestaShop\Core\Checkout\TermsAndConditions;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
class ConditionsToApproveFinderCore
|
||||
{
|
||||
private $translator;
|
||||
private $context;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
TranslatorInterface $translator
|
||||
) {
|
||||
$this->context = $context;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TermsAndConditions
|
||||
*/
|
||||
private function getDefaultTermsAndConditions()
|
||||
{
|
||||
$cms = new CMS(Configuration::get('PS_CONDITIONS_CMS_ID'), $this->context->language->id);
|
||||
$link = $this->context->link->getCMSLink($cms, $cms->link_rewrite, (bool) Configuration::get('PS_SSL_ENABLED'));
|
||||
|
||||
$termsAndConditions = new TermsAndConditions();
|
||||
$termsAndConditions
|
||||
->setText(
|
||||
$this->translator->trans('I agree to the [terms of service] and will adhere to them unconditionally.', [], 'Shop.Theme.Checkout'),
|
||||
$link
|
||||
)
|
||||
->setIdentifier('terms-and-conditions');
|
||||
|
||||
return $termsAndConditions;
|
||||
}
|
||||
|
||||
private function getConditionsToApprove()
|
||||
{
|
||||
$allConditions = [];
|
||||
$hookedConditions = Hook::exec('termsAndConditions', [], null, true);
|
||||
if (!is_array($hookedConditions)) {
|
||||
$hookedConditions = [];
|
||||
}
|
||||
foreach ($hookedConditions as $hookedCondition) {
|
||||
if ($hookedCondition instanceof TermsAndConditions) {
|
||||
$allConditions[] = $hookedCondition;
|
||||
} elseif (is_array($hookedCondition)) {
|
||||
foreach ($hookedCondition as $hookedConditionObject) {
|
||||
if ($hookedConditionObject instanceof TermsAndConditions) {
|
||||
$allConditions[] = $hookedConditionObject;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Configuration::get('PS_CONDITIONS')) {
|
||||
array_unshift($allConditions, $this->getDefaultTermsAndConditions());
|
||||
}
|
||||
|
||||
/*
|
||||
* If two TermsAndConditions objects have the same identifier,
|
||||
* the one at the end of the list overrides the first one.
|
||||
* This allows a module to override the default checkbox
|
||||
* in a consistent manner.
|
||||
*/
|
||||
$reducedConditions = [];
|
||||
foreach ($allConditions as $condition) {
|
||||
if ($condition instanceof TermsAndConditions) {
|
||||
$reducedConditions[$condition->getIdentifier()] = $condition;
|
||||
}
|
||||
}
|
||||
|
||||
return $reducedConditions;
|
||||
}
|
||||
|
||||
public function getConditionsToApproveForTemplate()
|
||||
{
|
||||
return array_map(function (TermsAndConditions $condition) {
|
||||
return $condition->format();
|
||||
}, $this->getConditionsToApprove());
|
||||
}
|
||||
}
|
||||
141
classes/checkout/DeliveryOptionsFinder.php
Normal file
141
classes/checkout/DeliveryOptionsFinder.php
Normal file
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
use PrestaShop\PrestaShop\Adapter\Presenter\Object\ObjectPresenter;
|
||||
use PrestaShop\PrestaShop\Adapter\Product\PriceFormatter;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
class DeliveryOptionsFinderCore
|
||||
{
|
||||
private $context;
|
||||
private $objectPresenter;
|
||||
private $translator;
|
||||
private $priceFormatter;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
TranslatorInterface $translator,
|
||||
ObjectPresenter $objectPresenter,
|
||||
PriceFormatter $priceFormatter
|
||||
) {
|
||||
$this->context = $context;
|
||||
$this->objectPresenter = $objectPresenter;
|
||||
$this->translator = $translator;
|
||||
$this->priceFormatter = $priceFormatter;
|
||||
}
|
||||
|
||||
private function isFreeShipping($cart, array $carrier)
|
||||
{
|
||||
$free_shipping = false;
|
||||
|
||||
if ($carrier['is_free']) {
|
||||
$free_shipping = true;
|
||||
} else {
|
||||
foreach ($cart->getCartRules() as $rule) {
|
||||
if ($rule['free_shipping'] && !$rule['carrier_restriction']) {
|
||||
$free_shipping = true;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $free_shipping;
|
||||
}
|
||||
|
||||
public function getSelectedDeliveryOption()
|
||||
{
|
||||
return current($this->context->cart->getDeliveryOption(null, false, false));
|
||||
}
|
||||
|
||||
public function getDeliveryOptions()
|
||||
{
|
||||
$delivery_option_list = $this->context->cart->getDeliveryOptionList();
|
||||
$include_taxes = !Product::getTaxCalculationMethod((int) $this->context->cart->id_customer) && (int) Configuration::get('PS_TAX');
|
||||
$display_taxes_label = (Configuration::get('PS_TAX') && !Configuration::get('AEUC_LABEL_TAX_INC_EXC'));
|
||||
|
||||
$carriers_available = [];
|
||||
|
||||
if (isset($delivery_option_list[$this->context->cart->id_address_delivery])) {
|
||||
foreach ($delivery_option_list[$this->context->cart->id_address_delivery] as $id_carriers_list => $carriers_list) {
|
||||
foreach ($carriers_list as $carriers) {
|
||||
if (is_array($carriers)) {
|
||||
foreach ($carriers as $carrier) {
|
||||
$carrier = array_merge($carrier, $this->objectPresenter->present($carrier['instance']));
|
||||
$delay = $carrier['delay'][$this->context->language->id];
|
||||
unset($carrier['instance'], $carrier['delay']);
|
||||
$carrier['delay'] = $delay;
|
||||
if ($this->isFreeShipping($this->context->cart, $carriers_list)) {
|
||||
$carrier['price'] = $this->translator->trans(
|
||||
'Free',
|
||||
[],
|
||||
'Shop.Theme.Checkout'
|
||||
);
|
||||
} else {
|
||||
if ($include_taxes) {
|
||||
$carrier['price'] = $this->priceFormatter->format($carriers_list['total_price_with_tax']);
|
||||
if ($display_taxes_label) {
|
||||
$carrier['price'] = $this->translator->trans(
|
||||
'%price% tax incl.',
|
||||
['%price%' => $carrier['price']],
|
||||
'Shop.Theme.Checkout'
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$carrier['price'] = $this->priceFormatter->format($carriers_list['total_price_without_tax']);
|
||||
if ($display_taxes_label) {
|
||||
$carrier['price'] = $this->translator->trans(
|
||||
'%price% tax excl.',
|
||||
['%price%' => $carrier['price']],
|
||||
'Shop.Theme.Checkout'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count($carriers) > 1) {
|
||||
$carrier['label'] = $carrier['price'];
|
||||
} else {
|
||||
$carrier['label'] = $carrier['name'] . ' - ' . $carrier['delay'] . ' - ' . $carrier['price'];
|
||||
}
|
||||
|
||||
// If carrier related to a module, check for additionnal data to display
|
||||
$carrier['extraContent'] = '';
|
||||
if ($carrier['is_module']) {
|
||||
if ($moduleId = Module::getModuleIdByName($carrier['external_module_name'])) {
|
||||
$carrier['extraContent'] = Hook::exec('displayCarrierExtraContent', ['carrier' => $carrier], $moduleId);
|
||||
}
|
||||
}
|
||||
|
||||
$carriers_available[$id_carriers_list] = $carrier;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $carriers_available;
|
||||
}
|
||||
}
|
||||
102
classes/checkout/PaymentOptionsFinder.php
Normal file
102
classes/checkout/PaymentOptionsFinder.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright since 2007 PrestaShop SA and Contributors
|
||||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/OSL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to https://devdocs.prestashop.com/ for more information.
|
||||
*
|
||||
* @author PrestaShop SA and Contributors <contact@prestashop.com>
|
||||
* @copyright Since 2007 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
||||
*/
|
||||
use PrestaShop\PrestaShop\Core\Payment\PaymentOption;
|
||||
use PrestaShop\PrestaShop\Core\Payment\PaymentOptionFormDecorator;
|
||||
use PrestaShopBundle\Service\Hook\HookFinder;
|
||||
|
||||
class PaymentOptionsFinderCore extends HookFinder
|
||||
{
|
||||
/**
|
||||
* Collects available payment options from three different hooks.
|
||||
*
|
||||
* @return array An array of available payment options
|
||||
*
|
||||
* @see HookFinder::find()
|
||||
*/
|
||||
public function find() //getPaymentOptions()
|
||||
{
|
||||
// Payment options coming from intermediate, deprecated version of the Advanced API
|
||||
$this->hookName = 'displayPaymentEU';
|
||||
$rawDisplayPaymentEUOptions = parent::find();
|
||||
$paymentOptions = array_map(
|
||||
['PrestaShop\PrestaShop\Core\Payment\PaymentOption', 'convertLegacyOption'],
|
||||
$rawDisplayPaymentEUOptions
|
||||
);
|
||||
|
||||
// Advanced payment options coming from regular Advanced API
|
||||
$this->hookName = 'advancedPaymentOptions';
|
||||
$paymentOptions = array_merge($paymentOptions, parent::find());
|
||||
|
||||
// Payment options coming from regular Advanced API
|
||||
$this->hookName = 'paymentOptions';
|
||||
$this->expectedInstanceClasses = ['PrestaShop\PrestaShop\Core\Payment\PaymentOption'];
|
||||
$paymentOptions = array_merge($paymentOptions, parent::find());
|
||||
|
||||
// Safety check
|
||||
foreach ($paymentOptions as $moduleName => $paymentOption) {
|
||||
if (!is_array($paymentOption)) {
|
||||
unset($paymentOptions[$moduleName]);
|
||||
}
|
||||
}
|
||||
|
||||
return $paymentOptions;
|
||||
}
|
||||
|
||||
public function findFree()
|
||||
{
|
||||
$freeOption = new PaymentOption();
|
||||
$freeOption->setModuleName('free_order')
|
||||
->setCallToActionText(Context::getContext()->getTranslator()->trans('Free order', [], 'Admin.Orderscustomers.Feature'))
|
||||
->setAction(Context::getContext()->link->getPageLink('order-confirmation', null, null, 'free_order=1'));
|
||||
|
||||
return ['free_order' => [$freeOption]];
|
||||
}
|
||||
|
||||
public function present($free = false) //getPaymentOptionsForTemplate()
|
||||
{
|
||||
$id = 0;
|
||||
|
||||
$find = $free ? $this->findFree() : $this->find();
|
||||
|
||||
return array_map(function (array $options) use (&$id) {
|
||||
return array_map(function (PaymentOption $option) use (&$id) {
|
||||
++$id;
|
||||
$formattedOption = $option->toArray();
|
||||
$formattedOption['id'] = 'payment-option-' . $id;
|
||||
|
||||
if ($formattedOption['form']) {
|
||||
$decorator = new PaymentOptionFormDecorator();
|
||||
$formattedOption['form'] = $decorator->addHiddenSubmitButton(
|
||||
$formattedOption['form'],
|
||||
$formattedOption['id']
|
||||
);
|
||||
}
|
||||
|
||||
return $formattedOption;
|
||||
}, $options);
|
||||
}, $find);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user