Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 6 additions & 22 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ jobs:
matrix:
prestashop:
# Keep the checkout ref and Docker image tag aligned when adding more supported versions.
- label: latest
image_tag: latest
core_ref: ''
- label: 9.1.0
image_tag: 9.1.0
core_ref: 9.1.0
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -74,15 +74,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v5

- name: Checkout PrestaShop core (default branch)
if: ${{ matrix.prestashop.core_ref == '' }}
uses: actions/checkout@v5
with:
repository: PrestaShop/PrestaShop
path: prestashop-core

- name: Checkout PrestaShop core (${{ matrix.prestashop.core_ref }})
if: ${{ matrix.prestashop.core_ref != '' }}
uses: actions/checkout@v5
with:
repository: PrestaShop/PrestaShop
Expand All @@ -106,9 +98,9 @@ jobs:
matrix:
prestashop:
# Keep the checkout ref and Docker image tag aligned when adding more supported versions.
- label: latest
image_tag: latest
core_ref: ''
- label: 9.1.0
image_tag: 9.1.0
core_ref: 9.1.0
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -118,15 +110,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v5

- name: Checkout PrestaShop core (default branch)
if: ${{ matrix.prestashop.core_ref == '' }}
uses: actions/checkout@v5
with:
repository: PrestaShop/PrestaShop
path: prestashop-core

- name: Checkout PrestaShop core (${{ matrix.prestashop.core_ref }})
if: ${{ matrix.prestashop.core_ref != '' }}
uses: actions/checkout@v5
with:
repository: PrestaShop/PrestaShop
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/config_*.xml
/translations/*.php
/node_modules
/tests/e2e/node_modules
/tests/e2e/artifacts
/tests/e2e/.env
/vendor
/views/node_modules
/.php_cs.cache
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
},
"classmap": [
"ps_onepagecheckout.php",
"controllers/"
"controllers/",
"src/Checkout/Ajax/"
]
},
"scripts": {
Expand Down
19 changes: 19 additions & 0 deletions controllers/admin/AdminPsOnePageCheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public function initContent()
{
parent::initContent();

if (!$this->viewAccess()) {
return;
}

$configurationContent = $this->getBackOfficeConfigurationContent();
if ($configurationContent !== '') {
$this->content .= $configurationContent;
Expand All @@ -36,6 +40,11 @@ protected function getBackOfficeConfigurationContent(): string
return $this->module->getBackOfficeConfigurationContent();
}

public function viewAccess($disable = false)
{
return $this->hasLegacyViewAccess((bool) $disable) && $this->hasModuleConfigurePermission();
}

public function getTwig(): ?Environment
{
try {
Expand All @@ -50,4 +59,14 @@ public function getTwig(): ?Environment

return $legacyControllerContext->getTwig();
}

protected function hasLegacyViewAccess(bool $disable = false): bool
{
return parent::viewAccess($disable);
}

protected function hasModuleConfigurePermission(): bool
{
return $this->module->getPermission('configure', $this->context->employee ?? null);
}
}
62 changes: 62 additions & 0 deletions controllers/front/AbstractOpcJsonFrontController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

abstract class Ps_OnepagecheckoutAbstractOpcJsonFrontController extends ModuleFrontController
{
/** @var bool */
public $ssl = true;

public function initContent()
{
parent::initContent();

$this->renderJsonResponse($this->handleOpcRequest());
}

/**
* @return array<string,mixed>
*/
abstract protected function handleOpcRequest(): array;

protected function isOpcAvailable(): bool
{
return $this->module instanceof Ps_Onepagecheckout
&& $this->module->isOnePageCheckoutEnabled();
}

/**
* @return array<string,mixed>
*/
protected function buildTechnicalErrorResponse(): array
{
return $this->getTechnicalErrorResponseExtra() + [
'success' => false,
'errors' => [
'' => [
$this->trans('One-page checkout is currently unavailable.', [], 'Shop.Notifications.Error'),
],
],
];
}

/**
* @return array<string,mixed>
*/
protected function getTechnicalErrorResponseExtra(): array
{
return [];
}

/**
* @param array<string,mixed> $response
*/
protected function renderJsonResponse(array $response): void
{
if (ob_get_level() > 0) {
ob_end_clean();
}

header('Content-Type: application/json');
$this->ajaxRender(json_encode($response));
exit;
}
}
66 changes: 66 additions & 0 deletions controllers/front/addresseslist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* AJAX endpoint for module-owned OPC addresses list.
*/

use PrestaShop\Module\PsOnePageCheckout\Checkout\Ajax\CheckoutCustomerContextResolver;
use PrestaShop\Module\PsOnePageCheckout\Checkout\Ajax\OnePageCheckoutAddressesListHandler;

require_once __DIR__ . '/AbstractOpcJsonFrontController.php';

class Ps_OnepagecheckoutAddressesListModuleFrontController extends Ps_OnepagecheckoutAbstractOpcJsonFrontController
{
/**
* @return array<string,mixed>
*/
protected function handleOpcRequest(): array
{
if (!$this->isOpcAvailable()) {
return $this->buildTechnicalErrorResponse();
}

try {
$handler = new OnePageCheckoutAddressesListHandler(
$this->context,
new CheckoutCustomerContextResolver($this->context)
);
$response = $handler->handle();
if (empty($response['success'])) {
return $response;
}

return [
'success' => true,
'address_count' => (int) ($response['address_count'] ?? 0),
'delivery_html' => $this->render(
'checkout/_partials/one-page-checkout/address-list',
[
'customer' => $response['customer'] ?? [],
'prefix' => '',
'selected_address' => (int) ($response['selected_delivery_address'] ?? 0),
]
),
'billing_html' => $this->render(
'checkout/_partials/one-page-checkout/address-list',
[
'customer' => $response['customer'] ?? [],
'prefix' => 'invoice_',
'selected_address' => (int) ($response['selected_invoice_address'] ?? 0),
]
),
];
} catch (Throwable $exception) {
PrestaShopLogger::addLog(
sprintf('ps_onepagecheckout addressesList runtime exception: %s', $exception->getMessage()),
3,
null,
'Module',
(int) $this->module->id,
true
);

return $this->buildTechnicalErrorResponse();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,20 @@
* AJAX endpoint for module-owned OPC address form refresh.
*/

use PrestaShop\Module\PsOnePageCheckout\Checkout\Ajax\CheckoutCustomerContextResolver;
use PrestaShop\Module\PsOnePageCheckout\Checkout\Ajax\OnePageCheckoutAddressFormHandler;
use PrestaShop\Module\PsOnePageCheckout\Form\OnePageCheckoutFormFactory;

class Ps_OnepagecheckoutAddressFormModuleFrontController extends ModuleFrontController
{
/** @var bool */
public $ssl = true;

public function initContent()
{
parent::initContent();

$response = $this->handleAddressFormRefresh();
$this->renderJsonResponse($response);
}
require_once __DIR__ . '/AbstractOpcJsonFrontController.php';

class Ps_OnepagecheckoutAddressFormModuleFrontController extends Ps_OnepagecheckoutAbstractOpcJsonFrontController
{
/**
* @return array<string,mixed>
*/
protected function handleAddressFormRefresh(): array
protected function handleOpcRequest(): array
{
if (!$this->module instanceof Ps_Onepagecheckout || !$this->module->isOnePageCheckoutEnabled()) {
if (!$this->isOpcAvailable()) {
return $this->buildTechnicalErrorResponse();
}

Expand All @@ -35,8 +27,8 @@ protected function handleAddressFormRefresh(): array
$templateVariables = $handler->getTemplateVariables(Tools::getAllValues());

return [
'address_form' => $this->render(
'checkout/_partials/one-page-checkout-form',
'addresses_section' => $this->render(
'checkout/_partials/one-page-checkout/addresses-section',
$templateVariables
),
];
Expand All @@ -63,34 +55,10 @@ protected function getOpcFormFactory(): OnePageCheckoutFormFactory

protected function createAddressFormHandler(OnePageCheckoutFormFactory $opcFormFactory): OnePageCheckoutAddressFormHandler
{
return new OnePageCheckoutAddressFormHandler($opcFormFactory->create());
}

/**
* @return array<string,mixed>
*/
protected function buildTechnicalErrorResponse(): array
{
return [
'success' => false,
'errors' => [
'' => [
$this->trans('One-page checkout is currently unavailable.', [], 'Shop.Notifications.Error'),
],
],
];
}

/**
* @param array<string,mixed> $response
*/
protected function renderJsonResponse(array $response): void
{
if (ob_get_level() > 0) {
ob_end_clean();
}

header('Content-Type: application/json');
$this->ajaxRender(json_encode($response));
return new OnePageCheckoutAddressFormHandler(
$opcFormFactory->create(),
$this->context,
new CheckoutCustomerContextResolver($this->context)
);
}
}
47 changes: 47 additions & 0 deletions controllers/front/carriers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* AJAX endpoint for module-owned OPC carriers list.
*/

use PrestaShop\Module\PsOnePageCheckout\Checkout\Ajax\OnePageCheckoutCarriersHandler;

require_once __DIR__ . '/AbstractOpcJsonFrontController.php';

class Ps_OnepagecheckoutCarriersModuleFrontController extends Ps_OnepagecheckoutAbstractOpcJsonFrontController
{
/**
* @return array<string,mixed>
*/
protected function handleOpcRequest(): array
{
if (!$this->isOpcAvailable()) {
return $this->buildTechnicalErrorResponse();
}

$handler = new OnePageCheckoutCarriersHandler($this->context, $this->module->getTranslator());
$response = $handler->handle(Tools::getAllValues());

if (!empty($response['success'])) {
$response['carriers_html'] = $this->render(
'checkout/_partials/one-page-checkout/carriers',
[
'delivery_options' => $response['delivery_options'] ?? [],
'delivery_option' => $response['delivery_option'] ?? '',
]
);
if (isset($response['cart_preview'])) {
$response['preview'] = $this->render(
'checkout/_partials/cart-summary',
[
'cart' => $response['cart_preview'],
'static_token' => Tools::getToken(false),
]
);
unset($response['cart_preview']);
}
}

return $response;
}
}
Loading
Loading