Skip to content

Commit

Permalink
refactor: move shipping cost methods to trait
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Nov 8, 2023
1 parent 5c492e1 commit 2da524a
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 160 deletions.
31 changes: 2 additions & 29 deletions myparcelnl.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use MyParcelNL\PrestaShop\Hooks\HasPdkRenderHooks;
use MyParcelNL\PrestaShop\Hooks\HasPdkScriptHooks;
use MyParcelNL\PrestaShop\Hooks\HasPsCarrierHooks;
use MyParcelNL\PrestaShop\Service\ModuleService;
use MyParcelNL\PrestaShop\Hooks\HasPsShippingCostHooks;
use function MyParcelNL\PrestaShop\bootPdk;

defined('_PS_VERSION_') or exit();
Expand All @@ -36,6 +36,7 @@ class MyParcelNL extends CarrierModule
use HasPdkRenderHooks;
use HasPdkScriptHooks;
use HasPsCarrierHooks;
use HasPsShippingCostHooks;

/**
* @var bool
Expand Down Expand Up @@ -106,34 +107,6 @@ public function getContent(): string
return '';
}

/**
* @param \Cart $params
* @param \int $shipping_cost
*
* @return float|int
*/
public function getOrderShippingCost($params, $shipping_cost)
{
if (! $this->hasPdk) {
return 0;
}

/** @var \MyParcelNL\PrestaShop\Service\ModuleService $moduleService */
$moduleService = Pdk::get(ModuleService::class);

return $moduleService->getOrderShippingCost($params, $shipping_cost);
}

/**
* @param \Cart $params
*
* @return bool
*/
public function getOrderShippingCostExternal($params): bool
{
return true;
}

/**
* @return bool
*/
Expand Down
55 changes: 1 addition & 54 deletions src/Hooks/HasPsShippingCostHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@

namespace MyParcelNL\PrestaShop\Hooks;

use Cart;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\PrestaShop\Repository\PsCartDeliveryOptionsRepository;
use Tools;

trait HasPsShippingCostHooks
{
/**
Expand All @@ -20,63 +15,15 @@ trait HasPsShippingCostHooks
public function getOrderShippingCost($cart, $shippingCost)
{
return $shippingCost;
// if (! $this->hasPdk || ! empty($this->context->controller->requestOriginalShippingCost)) {
// return $shippingCost;
// }
//
// $carrier = Pdk::get(PsCarrierServiceInterface::class)
// ->getMyParcelCarrier($cart->id_carrier);
//
// if (! $carrier) {
// return $shippingCost;
// }
//
// $deliveryOptions = $this->getDeliveryOptions($cart);
//
// if (! $deliveryOptions) {
// return $shippingCost;
// }
//
// /** @var \MyParcelNL\Pdk\App\DeliveryOptions\Contract\DeliveryOptionsFeesServiceInterface $service */
// $service = Pdk::get(DeliveryOptionsFeesServiceInterface::class);
//
// $fees = $service->getFees(new DeliveryOptions(['carrier' => $carrier]));
//
// $totalPrice = $fees
// ->reduce(function ($carry, $fee) {
// return $carry + $fee->getAmount();
// }, $shippingCost);
//
// return $totalPrice;
}

/**
* @param \Cart $params
*
* @return bool
*/
public function getOrderShippingCostExternal($params)
public function getOrderShippingCostExternal($params): bool
{
return true;
}

private function getDeliveryOptions(Cart $cart): ?array
{
$deliveryOptions = Tools::getValue(Pdk::get('checkoutHiddenInputName'), null);

if ($deliveryOptions) {
return json_decode($deliveryOptions, true);
}

/** @var \MyParcelNL\PrestaShop\Repository\PsCartDeliveryOptionsRepository $repository */
$repository = Pdk::get(PsCartDeliveryOptionsRepository::class);

$options = $repository->findOneBy(['cartId' => $cart->id]);

if (! $options) {
return null;
}

return $options->getData();
}
}
77 changes: 0 additions & 77 deletions src/Service/ModuleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace MyParcelNL\PrestaShop\Service;

use Cart;
use Module;
use MyParcelNL\Pdk\Facade\Logger;
use MyParcelNL\Pdk\Facade\Pdk;
Expand All @@ -20,82 +19,6 @@ public function getInstance(): Module
return Pdk::get('moduleInstance');
}

/**
* Adds prices of MyParcel delivery options to the shipping cost.
*
* @param \Cart $cart
* @param float|int $shippingCost
*
* @return float|int
* @todo Implement this method correctly using pdk stuff
* @see \MyParcelNL\Pdk\App\DeliveryOptions\Contract\DeliveryOptionsFeesServiceInterface
*/
public function getOrderShippingCost(Cart $cart, $shippingCost)
{
return $shippingCost;

// $module = Pdk::get('moduleInstance');
//
// /** @noinspection PhpCastIsUnnecessaryInspection */
// $carrierId = (int) $cart->id_carrier;
//
// if ($module->id_carrier !== $carrierId || ! empty($this->context->controller->requestOriginalShippingCost)) {
// return $shippingCost;
// }
//
// $myParcelCost = 0;
// $deliveryOptions = Tools::getValue('myparcel-delivery-options', false);
//
// if ($deliveryOptions) {
// $deliveryOptions = json_decode($deliveryOptions, true);
// } else {
// $deliveryOptions = DeliveryOptionsManager::getFromCart((int) $cart->id);
//
// if ($deliveryOptions) {
// $deliveryOptions = $deliveryOptions->toArray();
// }
// }
//
// if (empty($deliveryOptions)) {
// return $shippingCost;
// }
//
// $isPickup = $deliveryOptions['isPickup'] ?? false;
//
// if ($isPickup) {
// $myParcelCost += (float) CarrierConfigurationProvider::get(
// $carrierId,
// 'pricePickup'
// );
// } else {
// $deliveryType = $deliveryOptions['deliveryType'] ?? AbstractConsignment::DELIVERY_TYPE_STANDARD_NAME;
//
// if ($deliveryType !== AbstractConsignment::DELIVERY_TYPE_STANDARD_NAME) {
// $priceHourInterval = 'price' . ucfirst($deliveryType) . 'Delivery';
// $myParcelCost += (float) CarrierConfigurationProvider::get(
// $carrierId,
// $priceHourInterval
// );
// }
//
// if (! empty($deliveryOptions['shipmentOptions']['only_recipient'])) {
// $myParcelCost += (float) CarrierConfigurationProvider::get(
// $carrierId,
// 'priceOnlyRecipient'
// );
// }
//
// if (! empty($deliveryOptions['shipmentOptions']['signature'])) {
// $myParcelCost += (float) CarrierConfigurationProvider::get(
// $carrierId,
// 'priceSignature'
// );
// }
// }
//
// return $shippingCost + $myParcelCost;
}

/**
* @return bool
*/
Expand Down

0 comments on commit 2da524a

Please sign in to comment.