From 86a3910c677efcd07c3eb2ab646c3191adf90345 Mon Sep 17 00:00:00 2001 From: Joeri van Veen Date: Tue, 16 Apr 2024 14:52:08 +0200 Subject: [PATCH] fix: prevent camel cased options from breaking order detail page --- Block/Sales/View.php | 93 +++++++++++++++------------------ Helper/Order.php | 38 -------------- Model/Source/DefaultOptions.php | 40 ++++++++------ 3 files changed, 66 insertions(+), 105 deletions(-) delete mode 100755 Helper/Order.php diff --git a/Block/Sales/View.php b/Block/Sales/View.php index 7b5b896f..4b7d577a 100755 --- a/Block/Sales/View.php +++ b/Block/Sales/View.php @@ -22,30 +22,10 @@ use Magento\Framework\App\ObjectManager; use Magento\Sales\Block\Adminhtml\Order\AbstractOrder; use MyParcelNL\Magento\Helper\Checkout as CheckoutHelper; -use MyParcelNL\Magento\Model\Quote\Checkout; +use MyParcelNL\Sdk\src\Factory\DeliveryOptionsAdapterFactory; class View extends AbstractOrder { - /** - * @var \Magento\Framework\ObjectManagerInterface - */ - private $objectManager; - - /** - * @var \MyParcelNL\Magento\Helper\Order - */ - private $helper; - - /** - * Constructor - */ - public function _construct() - { - $this->objectManager = ObjectManager::getInstance(); - $this->helper = $this->objectManager->get('\MyParcelNL\Magento\Helper\Order'); - parent::_construct(); - } - /** * Collect options selected at checkout and calculate type consignment * @@ -55,49 +35,58 @@ public function _construct() */ public function getCheckoutOptionsHtml() { - $html = false; $order = $this->getOrder(); /** @var object $data Data from checkout */ - $data = $order->getData(CheckoutHelper::FIELD_DELIVERY_OPTIONS) !== null ? json_decode($order->getData(CheckoutHelper::FIELD_DELIVERY_OPTIONS), true) : false; + $data = $order->getData(CheckoutHelper::FIELD_DELIVERY_OPTIONS) !== null ? json_decode($order->getData(CheckoutHelper::FIELD_DELIVERY_OPTIONS), true) : null; - $date = new DateTime($data['date'] ?? ''); - $dateTime = $date->format('d-m-Y H:i'); + if (! is_array($data)) { + return ''; + } - if ($this->helper->isPickupLocation($data)) { - if (is_array($data) && key_exists('pickupLocation', $data)) { - $html .= __($data['carrier'] . ' location:') . ' ' . $dateTime; - if ($data['deliveryType'] != 'pickup') { - $html .= ', ' . __($data['deliveryType']); - } - $html .= ', ' . $data['pickupLocation']['location_name'] . ', ' . $data['pickupLocation']['city'] . ' (' . $data['pickupLocation']['postal_code'] . ')'; - } else { - /** Old data from orders before version 1.6.0 */ - $html .= __('MyParcel options data not found'); - } - } else { - if (is_array($data) && key_exists('date', $data)) { - if (key_exists('packageType', $data)) { - $html .= __($data['packageType'] . ' '); + $date = new DateTime($data['date'] ?? ''); + $dateTime = $date->format('d-m-Y H:i'); + $deliveryOptions = DeliveryOptionsAdapterFactory::create((array) $data); + + ob_start(); + + if ($deliveryOptions->isPickup()) { + try { + echo __("{$data['carrier']} location:"), ' ', $dateTime; + + if ($data['deliveryType'] !== 'pickup') { + echo ', ', __($data['deliveryType']); } - $html .= __('Deliver:') . ' ' . $dateTime; + $pickupLocation = $deliveryOptions->getPickupLocation(); - if (key_exists('shipmentOptions', $data)) { - if (key_exists('signature', $data['shipmentOptions']) && $data['shipmentOptions']['signature']) { - $html .= ', ' . __('Signature on receipt'); - } - if (key_exists('only_recipient', $data['shipmentOptions']) && $data['shipmentOptions']['only_recipient']) { - $html .= ', ' . __('Home address only'); - } + if (null !== $pickupLocation) { + echo ', ', $pickupLocation->getLocationName(), ', ', $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']), ' '; + } + + echo __('Deliver:'), ' ', $dateTime; + + $shipmentOptions = $deliveryOptions->getShipmentOptions(); - if (is_array($data) && key_exists('browser', $data)) { - $html = ' ' . $html . ''; + if (null !== $shipmentOptions) { + if ($shipmentOptions->hasSignature()) { + echo ', ', __('Signature on receipt'); + } + if ($shipmentOptions->hasOnlyRecipient()) { + echo ', ', __('Home address only'); + } + } } - return $html !== false ? '
' . $html : ''; + return htmlentities(ob_get_clean()); } } diff --git a/Helper/Order.php b/Helper/Order.php deleted file mode 100755 index fd681958..00000000 --- a/Helper/Order.php +++ /dev/null @@ -1,38 +0,0 @@ - - * @copyright 2010-2016 MyParcel - * @license http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US CC BY-NC-ND 3.0 NL - * @link https://github.com/myparcelnl/magento - * @since File available since Release v0.1.0 - */ - -namespace MyParcelNL\Magento\Helper; - -use Magento\Framework\App\Helper\AbstractHelper; - -class Order extends AbstractHelper -{ - /** - * Checks if the given shipping method is a pickup location - * - * @param $myparcelDeliveryOptions - * - * @return bool - */ - public function isPickupLocation($myparcelDeliveryOptions) - { - if (is_array($myparcelDeliveryOptions) && key_exists('isPickup', $myparcelDeliveryOptions) && $myparcelDeliveryOptions['isPickup']) { - return true; - } - - return false; - } -} diff --git a/Model/Source/DefaultOptions.php b/Model/Source/DefaultOptions.php index 3f013386..a00af564 100755 --- a/Model/Source/DefaultOptions.php +++ b/Model/Source/DefaultOptions.php @@ -13,11 +13,12 @@ namespace MyParcelNL\Magento\Model\Source; +use BadMethodCallException; use Magento\Sales\Model\Order; use MyParcelNL\Magento\Helper\Checkout; use MyParcelNL\Magento\Helper\Data; -use MyParcelNL\Magento\Model\Sales\Package; use MyParcelNL\Magento\Model\Sales\Repository\PackageRepository; +use MyParcelNL\Sdk\src\Factory\DeliveryOptionsAdapterFactory; use MyParcelNL\Sdk\src\Model\Carrier\AbstractCarrier; use MyParcelNL\Sdk\src\Model\Carrier\CarrierFactory; use MyParcelNL\Sdk\src\Model\Carrier\CarrierPostNL; @@ -62,9 +63,13 @@ public function __construct(Order $order, Data $helper) { self::$helper = $helper; self::$order = $order; - $options = self::$order->getData(Checkout::FIELD_DELIVERY_OPTIONS) ?? ''; - - self::$chosenOptions = json_decode($options, true) ?? []; + try { + self::$chosenOptions = DeliveryOptionsAdapterFactory::create( + (array) json_decode($order->getData(Checkout::FIELD_DELIVERY_OPTIONS), true) + )->toArray(); + } catch (BadMethodCallException $e) { + self::$chosenOptions = []; + } } /** @@ -86,15 +91,18 @@ public function hasDefault(string $option, string $carrier): bool return true; } - $total = self::$order->getGrandTotal(); - $settings = self::$helper->getStandardConfig($carrier, 'default_options'); + $total = self::$order->getGrandTotal(); + $settings = self::$helper->getStandardConfig($carrier, 'default_options'); + $activeKey = "{$option}_active"; - if (! isset($settings[$option . '_active'])) { + if (! isset($settings[$activeKey])) { return false; } - return '1' === $settings[$option . '_active'] - && (! ($settings[$option . '_from_price'] ?? false) || $total > (int) $settings[$option . '_from_price']); + $priceKey = "{$option}_from_price"; + + return '1' === $settings[$activeKey] + && (! ($settings[$priceKey] ?? false) || $total > (int) $settings[$priceKey]); } /** @@ -124,17 +132,19 @@ public function hasDefaultLargeFormat(string $carrier, string $option): bool $price = self::$order->getGrandTotal(); $weight = self::$helper->convertToGrams(self::$order->getWeight()); - $settings = self::$helper->getStandardConfig($carrier, 'default_options'); - if (isset($settings[$option . '_active']) && - 'weight' === $settings[$option . '_active'] && + $settings = self::$helper->getStandardConfig($carrier, 'default_options'); + $activeKey = "{$option}_active"; + + if (isset($settings[$activeKey]) && + 'weight' === $settings[$activeKey] && $weight >= PackageRepository::DEFAULT_LARGE_FORMAT_WEIGHT ) { return true; } - if (isset($settings[$option . '_active']) && - 'price' === $settings[$option . '_active'] && - $price >= $settings[$option . '_from_price'] + if (isset($settings[$activeKey]) && + 'price' === $settings[$activeKey] && + $price >= $settings["{$option}_from_price"] ) { return true; }