Skip to content

Commit

Permalink
feat: display all available carrier in the delivery option
Browse files Browse the repository at this point in the history
  • Loading branch information
Nakahiru committed Jan 30, 2025
1 parent 47d5dcb commit 0451524
Showing 1 changed file with 106 additions and 74 deletions.
180 changes: 106 additions & 74 deletions classes/checkout/DeliveryOptionsFinder.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
Expand All @@ -23,6 +24,7 @@
* @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\Contracts\Translation\TranslatorInterface;
Expand All @@ -46,23 +48,22 @@ public function __construct(
$this->priceFormatter = $priceFormatter;
}

private function isFreeShipping($cart, array $carrier)
private function isFreeShipping(array $deliveryOption)
{
$free_shipping = false;
if ($deliveryOption['is_free']) {
return true;
}

if ($carrier['is_free']) {
$free_shipping = true;
} else {
foreach ($cart->getCartRules() as $rule) {
if ($rule['free_shipping'] && !$rule['carrier_restriction']) {
$free_shipping = true;
$cart = $this->context->cart;

break;
}
foreach ($cart->getCartRules() as $rule) {
if ($rule['free_shipping'] && !$rule['carrier_restriction']) {
return true;
break;

Check failure on line 62 in classes/checkout/DeliveryOptionsFinder.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Unreachable statement - code above always terminates.

Check failure on line 62 in classes/checkout/DeliveryOptionsFinder.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Unreachable statement - code above always terminates.

Check failure on line 62 in classes/checkout/DeliveryOptionsFinder.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.3)

Unreachable statement - code above always terminates.

Check failure on line 62 in classes/checkout/DeliveryOptionsFinder.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.4)

Unreachable statement - code above always terminates.
}
}

return $free_shipping;
return false;
}

public function getSelectedDeliveryOption()
Expand All @@ -72,71 +73,102 @@ public function getSelectedDeliveryOption()

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') && $this->context->country->display_tax_label && !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'])) {
// Hook called only for the module concerned
$carrier['extraContent'] = Hook::exec('displayCarrierExtraContent', ['carrier' => $carrier], $moduleId);
}
}

$carriers_available[$id_carriers_list] = $carrier;
}
}
}
$deliveryOptions = $this->context->cart->getDeliveryOptionList();
$currentAddressDeliveryOptions = $deliveryOptions[$this->context->cart->id_address_delivery];

if (empty($deliveryOptions[$this->context->cart->id_address_delivery])) {
return [];
}

$formattedDeliveryOptions = [];

foreach ($currentAddressDeliveryOptions as $deliveryOptionId => $deliveryOption) {
$formattedDeliveryOption = end($deliveryOption['carrier_list']);
$formattedDeliveryOption = array_merge($formattedDeliveryOption, $this->objectPresenter->present($formattedDeliveryOption['instance']));
unset($formattedDeliveryOption['instance']);

$carriersDetails = $this->getCarriersDetails($deliveryOption);
$formattedDeliveryOption = array_merge($formattedDeliveryOption, $carriersDetails);

$formattedDeliveryOption['price'] = $this->getPriceToDisplay($deliveryOption);
$formattedDeliveryOption['label'] = $formattedDeliveryOption['price'];

$formattedDeliveryOptions[$deliveryOptionId] = $formattedDeliveryOption;
}

return $formattedDeliveryOptions;
}

private function getCarriersDetails($deliveryOption)
{
$carriers = $deliveryOption['carrier_list'];

if (count($carriers) === 1) {
return [
'id' => $carriers[0]['instance']->id,
'label' => $carriers[0]['label'],
'name' => $carriers[0]['instance']->name,
'delay' => $carriers[0]['instance']->delay[$this->context->language->id],
];
}

$names = [];
$delays = [];
$extraContent = [];

foreach ($carriers as $carrier) {
$names[] = $carrier['instance']->name;
$delays[] = $carrier['instance']->delay[$this->context->language->id];
$extraContent[$carrier['instance']->id] = Hook::exec('displayCarrierExtraContent', ['carrier' => $carrier['instance']], Module::getModuleIdByName($carrier['instance']->id));
}

return [
'id' => end($carriers)['instance']->id,
'name' => implode(', ', $names),
'delay' => implode(', ', $delays),
'extraContent' => $extraContent,
];
}

private function getPriceToDisplay($deliveryOption)
{
if ($this->isFreeShipping($deliveryOption)) {
return $this->translator->trans(
'Free',
[],
'Shop.Theme.Checkout'
);
}

if ($this->shouldIncludeTaxes()) {
$price = $this->priceFormatter->format($deliveryOption['total_price_with_tax']);
if ($this->shouldDisplayTaxesLabel()) {
$label = '%price% tax incl.';
}
} else {
$price = $this->priceFormatter->format($deliveryOption['total_price_without_tax']);
if ($this->shouldDisplayTaxesLabel()) {
$label = '%price% tax excl.';
}
}

return $carriers_available;
return $this->translator->trans(
$label ?? '%price%',
['%price%' => $price],
'Shop.Theme.Checkout'
);
}

private function shouldIncludeTaxes()
{
return !Product::getTaxCalculationMethod((int) $this->context->cart->id_customer)
&& (int) Configuration::get('PS_TAX');
}

private function shouldDisplayTaxesLabel()
{
return Configuration::get('PS_TAX')
&& $this->context->country->display_tax_label
&& !Configuration::get('AEUC_LABEL_TAX_INC_EXC');
}
}

0 comments on commit 0451524

Please sign in to comment.