Skip to content

Commit

Permalink
fix: prevent camel cased options from breaking order detail page (#838)
Browse files Browse the repository at this point in the history
  • Loading branch information
joerivanveen committed Apr 22, 2024
1 parent c560351 commit bc8cff4
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 113 deletions.
130 changes: 73 additions & 57 deletions Block/Sales/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,82 +22,98 @@
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\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter;
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
*
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Exception
*/
public function getCheckoutOptionsHtml()
public function getCheckoutOptionsHtml(): string
{
$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;

$date = new DateTime($data['date'] ?? '');
$dateTime = $date->format('d-m-Y H:i');

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');
$data = $order->getData(CheckoutHelper::FIELD_DELIVERY_OPTIONS) !== null ? json_decode($order->getData(CheckoutHelper::FIELD_DELIVERY_OPTIONS), true) : null;

if (! is_array($data)) {
return '';
}

$deliveryOptions = DeliveryOptionsAdapterFactory::create((array) $data);
$returnString = '';

try {
if ($deliveryOptions->isPickup()) {
$returnString = htmlentities($this->getCheckoutOptionsPickupHtml($deliveryOptions));
}
} else {
if (is_array($data) && key_exists('date', $data)) {
if (key_exists('packageType', $data)) {
$html .= __($data['packageType'] . ' ');
}

$html .= __('Deliver:') . ' ' . $dateTime;

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 ($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();

echo __("{$deliveryOptions->getCarrier()} location:"), ' ';

if ('pickup' !== $deliveryOptions->getDeliveryType()) {
echo __($deliveryOptions->getDeliveryType()), ', ';
}

if (is_array($data) && key_exists('browser', $data)) {
$html = ' <span title="' . $data['browser'] . '">' . $html . '</span>';
$pickupLocation = $deliveryOptions->getPickupLocation();

if (null !== $pickupLocation) {
echo $pickupLocation->getLocationName(), ', ';
echo $pickupLocation->getCity(), ' (', $pickupLocation->getPostalCode(), ')';
}

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:'), ' ', $date->format('d-m-Y H:i');

$shipmentOptions = $deliveryOptions->getShipmentOptions();

if (null !== $shipmentOptions) {
if ($shipmentOptions->hasSignature()) {
echo ', ', __('Signature on receipt');
}
if ($shipmentOptions->hasOnlyRecipient()) {
echo ', ', __('Home address only');
}
}

return $html !== false ? '<br>' . $html : '';
return ob_get_clean();
}
}
38 changes: 0 additions & 38 deletions Helper/Order.php

This file was deleted.

40 changes: 25 additions & 15 deletions Model/Source/DefaultOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];
}
}

/**
Expand All @@ -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]);
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"type": "magento2-module",
"require": {
"php": "~5.6.5 || ^7.0 || ^8.0",
"myparcelnl/sdk": "~v7.11.0",
"myparcelnl/sdk": "~v7.13.1",
"magento/framework": ">=101.0.8 <102 || >=102.0.1"
},
"require-dev": {
Expand Down
5 changes: 3 additions & 2 deletions i18n/nl_NL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ status_99,Inactief - Onbekende status
myparcelnl_magento_postnl_settings/delivery_title, postnl bezorging
myparcelnl_magento_postnl_settings/pickup_title, postnl ophaallocatie
myparcelnl_magento_postnl_settings/delivery/signature_title, postnl bezorging met handtekening
myparcelnl_magento_dpd_settings/delivery_title, DPD bezorging
myparcelnl_magento_dpd_settings/pickup_title, DPD ophaallocatie
myparcelnl_magento_postnl_settings/pickup, PostNL ophaallocatie
myparcelnl_magento_postnl_settings/delivery, PostNL bezorging
myparcelnl_magento_postnl_settings/delivery/signature, PostNL bezorging met handtekening voor ontvangst
Expand All @@ -53,7 +51,10 @@ myparcelnl_magento_dhlparcelconnect_settings/delivery, DHL Parcel Connect bezorg
myparcelnl_magento_dhleuroplus_settings/delivery, DHL Europlus bezorging
myparcelnl_magento_dhlparcelconnect_settings/delivery, DHL Parcel Connect bezorging
myparcelnl_magento_ups_settings/delivery, UPS bezorging
myparcelnl_magento_dpd_settings/delivery_title, DPD bezorging
myparcelnl_magento_dpd_settings/pickup_title, DPD ophaallocatie
myparcelnl_magento_dpd_settings/delivery, DPD bezorging
myparcelnl_magento_dpd_settings/pickup, DPD ophaallocatie
myparcelnl_magento_error_no_shipments_to_process, Geen MyParcel zendingen om te verwerken.
package_small, Klein pakket
show_total_price, Toon totaalprijs
Expand Down

0 comments on commit bc8cff4

Please sign in to comment.