Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add new column in order_detail table for multicarrier #5

Closed
Show file tree
Hide file tree
Changes from all 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
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 @@
$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 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');
}
}
3 changes: 3 additions & 0 deletions install-dev/data/db_structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3025,3 +3025,6 @@ CREATE TABLE `PREFIX_access` (
KEY `IDX_564352A15FCA037F` (`id_profile`),
KEY `IDX_564352A18C6DE0E5` (`id_authorization_role`)
) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8mb4 COLLATION;

/* Here we doing some stuff for multicarrier */
ALTER TABLE `PREFIX_order_detail` ADD `id_order_carrier` INT(10) UNSIGNED DEFAULT NULL AFTER `id_order`;
Loading