From 16ff9e2ce1d2731cfd5bff2f9da9b8341ec9a106 Mon Sep 17 00:00:00 2001 From: Joeri van Veen Date: Mon, 22 Apr 2024 10:39:55 +0200 Subject: [PATCH] fix: implement feedback --- Block/Sales/View.php | 87 +++++++++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 30 deletions(-) diff --git a/Block/Sales/View.php b/Block/Sales/View.php index 909dd21f..a0a523c3 100755 --- a/Block/Sales/View.php +++ b/Block/Sales/View.php @@ -22,6 +22,7 @@ use Magento\Framework\App\ObjectManager; use Magento\Sales\Block\Adminhtml\Order\AbstractOrder; use MyParcelNL\Magento\Helper\Checkout as CheckoutHelper; +use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter; use MyParcelNL\Sdk\src\Factory\DeliveryOptionsAdapterFactory; class View extends AbstractOrder @@ -44,49 +45,75 @@ public function getCheckoutOptionsHtml(): string return ''; } - $date = new DateTime($data['date'] ?? ''); - $dateTime = $date->format('d-m-Y H:i'); $deliveryOptions = DeliveryOptionsAdapterFactory::create((array) $data); + $returnString = ''; + try { + if ($deliveryOptions->isPickup()) { + $returnString = htmlentities($this->getCheckoutOptionsPickupHtml($deliveryOptions)); + } + + if ($deliveryOptions->getDate()) { + $returnString = htmlentities($this->getCheckoutOptionsDeliveryHtml($deliveryOptions)); + } + } catch (\Throwable $e) { + ObjectManager::getInstance()->get(CheckoutHelper::class)->log($e->getMessage()); + $returnString = __('MyParcel options data not found'); + } + + return $returnString; + } + + /** + * @param AbstractDeliveryOptionsAdapter $deliveryOptions + * @return string + */ + private function getCheckoutOptionsPickupHtml(AbstractDeliveryOptionsAdapter $deliveryOptions): string { ob_start(); - if ($deliveryOptions->isPickup()) { - try { - echo __("{$data['carrier']} location:"), ' ', $dateTime; + echo __("{$deliveryOptions->getCarrier()} location:"), ' '; - if ($data['deliveryType'] !== 'pickup') { - echo ', ', __($data['deliveryType']); - } + if ('pickup' !== $deliveryOptions->getDeliveryType()) { + echo __($deliveryOptions->getDeliveryType()), ', '; + } - $pickupLocation = $deliveryOptions->getPickupLocation(); + $pickupLocation = $deliveryOptions->getPickupLocation(); - if (null !== $pickupLocation) { - echo ', ', $pickupLocation->getLocationName(), ', ', $pickupLocation->getCity(), ' (', $pickupLocation->getPostalCode(), ')'; - } + if (null !== $pickupLocation) { + echo $pickupLocation->getLocationName(), ', '; + echo $pickupLocation->getCity(), ' (', $pickupLocation->getPostalCode(), ')'; + } - } catch (\Throwable $e) { - ObjectManager::getInstance()->get(CheckoutHelper::class)->log($e->getMessage()); - echo __('MyParcel options data not found'); - } - } elseif (array_key_exists('date', $data)) { - if (array_key_exists('packageType', $data)) { - echo __($data['packageType']), ' '; - } + return ob_get_clean(); + } + + /** + * @param AbstractDeliveryOptionsAdapter $deliveryOptions + * @return string + * @throws \Exception + */ + private function getCheckoutOptionsDeliveryHtml(AbstractDeliveryOptionsAdapter $deliveryOptions): string { + ob_start(); + + if ($deliveryOptions->getPackageType()) { + echo __($deliveryOptions->getPackageType()), ' '; + } + + $date = new DateTime($deliveryOptions->getDate() ?? ''); - echo __('Deliver:'), ' ', $dateTime; + echo __('Deliver:'), ' ', $date->format('d-m-Y H:i'); - $shipmentOptions = $deliveryOptions->getShipmentOptions(); + $shipmentOptions = $deliveryOptions->getShipmentOptions(); - if (null !== $shipmentOptions) { - if ($shipmentOptions->hasSignature()) { - echo ', ', __('Signature on receipt'); - } - if ($shipmentOptions->hasOnlyRecipient()) { - echo ', ', __('Home address only'); - } + if (null !== $shipmentOptions) { + if ($shipmentOptions->hasSignature()) { + echo ', ', __('Signature on receipt'); + } + if ($shipmentOptions->hasOnlyRecipient()) { + echo ', ', __('Home address only'); } } - return htmlentities(ob_get_clean()); + return ob_get_clean(); } }