Skip to content

Commit 20908f7

Browse files
Merge pull request #117 from Paazl/release/1.17.4
Release/1.17.4
2 parents 2bd5ef9 + 6147d58 commit 20908f7

File tree

7 files changed

+243
-82
lines changed

7 files changed

+243
-82
lines changed

Model/Api/Builder/Order.php

+76-37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright © 2019 Paazl. All rights reserved.
3+
* Copyright © Paazl. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
66

@@ -10,71 +10,59 @@
1010
use Magento\Framework\Exception\LocalizedException;
1111
use Magento\Framework\Exception\NoSuchEntityException;
1212
use Magento\Framework\Exception\NotFoundException;
13+
use Magento\Framework\Serialize\SerializerInterface;
1314
use Magento\GroupedProduct\Model\Product\Type\Grouped;
1415
use Magento\Sales\Api\Data\OrderInterface;
1516
use Magento\Sales\Model\Order\Address;
1617
use Magento\Sales\Model\Order\Item;
1718
use Paazl\CheckoutWidget\Api\OrderReferenceRepositoryInterface;
19+
use Magento\Quote\Api\CartRepositoryInterface;
20+
use Paazl\CheckoutWidget\Model\Api\ApiException;
1821
use Paazl\CheckoutWidget\Model\Api\Builder\Reference as Reference;
1922
use Paazl\CheckoutWidget\Model\Api\Field\DeliveryType;
23+
use Paazl\CheckoutWidget\Model\Api\PaazlApiFactory;
2024
use Paazl\CheckoutWidget\Model\Config;
2125
use Paazl\CheckoutWidget\Model\ExtInfoHandler;
2226
use Paazl\CheckoutWidget\Model\Handler\Item as ItemHandler;
27+
use Paazl\CheckoutWidget\Model\Order\WidgetConfigProvider;
2328
use Paazl\CheckoutWidget\Model\System\Config\Source\DimensionsMetric;
2429

2530
/**
2631
* Class Order
27-
*
28-
* @package Paazl\CheckoutWidget\Model\Api\Builder
2932
*/
3033
class Order
3134
{
32-
/**
33-
* @var ExtInfoHandler
34-
*/
35-
private $extInfoHandler;
36-
37-
/**
38-
* @var Reference
39-
*/
40-
private $referenceBuilder;
41-
42-
/**
43-
* @var Config
44-
*/
45-
private $config;
46-
47-
/**
48-
* @var OrderReferenceRepositoryInterface
49-
*/
50-
private $orderReferenceRepository;
5135

52-
/**
53-
* @var ItemHandler
54-
*/
55-
private $itemHandler;
36+
private ExtInfoHandler $extInfoHandler;
37+
private Reference $referenceBuilder;
38+
private Config $config;
39+
private OrderReferenceRepositoryInterface $orderReferenceRepository;
40+
private CartRepositoryInterface $quoteRepository;
41+
private ItemHandler $itemHandler;
42+
private WidgetConfigProvider $widgetConfigProvider;
43+
private PaazlApiFactory $paazlApiFactory;
44+
private SerializerInterface $serializer;
5645

57-
/**
58-
* Order constructor.
59-
*
60-
* @param ExtInfoHandler $extInfoHandler
61-
* @param Reference $referenceBuilder
62-
* @param Config $config
63-
* @param OrderReferenceRepositoryInterface $orderReferenceRepository
64-
* @param ItemHandler $itemHandler
65-
*/
6646
public function __construct(
6747
ExtInfoHandler $extInfoHandler,
6848
Reference $referenceBuilder,
6949
Config $config,
7050
OrderReferenceRepositoryInterface $orderReferenceRepository,
71-
ItemHandler $itemHandler
51+
CartRepositoryInterface $quoteRepository,
52+
ItemHandler $itemHandler,
53+
WidgetConfigProvider $widgetConfigProvider,
54+
PaazlApiFactory $paazlApiFactory,
55+
SerializerInterface $serializer
7256
) {
7357
$this->extInfoHandler = $extInfoHandler;
7458
$this->referenceBuilder = $referenceBuilder;
7559
$this->config = $config;
7660
$this->orderReferenceRepository = $orderReferenceRepository;
61+
$this->quoteRepository = $quoteRepository;
7762
$this->itemHandler = $itemHandler;
63+
$this->widgetConfigProvider = $widgetConfigProvider;
64+
$this->paazlApiFactory = $paazlApiFactory;
65+
$this->serializer = $serializer;
7866
}
7967

8068
/**
@@ -141,7 +129,7 @@ public function getCreateOrderData(OrderInterface $order)
141129
'reference' => $this->referenceBuilder->getOrderReference($order),
142130
'invoiceNumber' => $this->referenceBuilder->getOrderReference($order),
143131
'shipping' => [
144-
'option' => $extInformation->getIdenfifier()
132+
'option' => $this->getShippingOption($extInformation->getIdenfifier(), $order)
145133
],
146134
];
147135

@@ -449,4 +437,55 @@ private function hasBundleParent($item): bool
449437
}
450438
return false;
451439
}
440+
441+
/**
442+
* @param $identifier
443+
* @param OrderInterface $order
444+
* @return mixed|string
445+
* @throws LocalizedException
446+
* @throws ApiException
447+
*/
448+
private function getShippingOption($identifier, OrderInterface $order)
449+
{
450+
if ($identifier !== null) {
451+
return $identifier;
452+
}
453+
454+
try {
455+
$quote = $this->quoteRepository->get((int)$order->getQuoteId());
456+
} catch (NoSuchEntityException $e) {
457+
return '';
458+
}
459+
460+
if ($quote == null) {
461+
return '';
462+
}
463+
464+
$extInformation = $this->extInfoHandler->getInfoFromQuote($quote);
465+
if (($extInformation !== null) && ($identifier = $extInformation->getIdenfifier())) {
466+
return $identifier;
467+
}
468+
469+
//get identifier from api using shipping description
470+
$shippingDescription = $order->getShippingDescription();
471+
$carrierTitle = $this->config->getCarrierTitle() . ' - ';
472+
$shippingDescription = str_replace($carrierTitle, '', $shippingDescription);
473+
474+
$orderData = $this->widgetConfigProvider
475+
->setOrder($order)
476+
->getConfig();
477+
$shippingOptions = $this->serializer->unserialize(
478+
$this->paazlApiFactory->create($order->getStoreId())
479+
->getShippingOptions($orderData)
480+
);
481+
$shippingOptions = $shippingOptions['shippingOptions'] ?? [];
482+
foreach ($shippingOptions as $shippingOption) {
483+
if (isset($shippingOption['name'], $shippingOption['identifier'])) {
484+
if ($shippingOption['name'] == $shippingDescription) {
485+
return $shippingOption['identifier'];
486+
}
487+
}
488+
}
489+
return '';
490+
}
452491
}

Model/Checkout/WidgetConfigProvider.php

+47-20
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Paazl\CheckoutWidget\Model\System\Config\Source\CalculateVolume;
2020
use Paazl\CheckoutWidget\Model\System\Config\Source\DimensionsMetric;
2121
use Paazl\CheckoutWidget\Model\TokenRetriever;
22+
use Magento\Catalog\Api\Data\ProductInterface;
2223

2324
/**
2425
* Class WidgetConfigProvider
@@ -73,6 +74,8 @@ class WidgetConfigProvider implements ConfigProviderInterface
7374
*/
7475
private $quote;
7576

77+
private $metricFactor = null;
78+
7679
/**
7780
* Widget constructor.
7881
*
@@ -136,25 +139,9 @@ public function getConfig()
136139
"price" => $this->itemHandler->getPriceValue($item)
137140
];
138141
if ($useDimensions) {
139-
switch ($this->scopeConfig->getDimensionsMetric()) {
140-
case DimensionsMetric::METRIC_MM:
141-
$k = 0.1;
142-
break;
143-
case DimensionsMetric::METRIC_CM:
144-
$k = 1;
145-
break;
146-
case DimensionsMetric::METRIC_M:
147-
$k = 100;
148-
break;
149-
default:
150-
$k = 1;
151-
}
152-
$goodsItem["length"] =
153-
(float)str_replace(',', '.', $product->getData($lengthAttribute)) * $k;
154-
$goodsItem["width"] =
155-
(float)str_replace(',', '.', $product->getData($widthAttribute)) * $k;
156-
$goodsItem["height"] =
157-
(float)str_replace(',', '.', $product->getData($heightAttribute)) * $k;
142+
$goodsItem["length"] = $this->getDimensionValue($product, $lengthAttribute);
143+
$goodsItem["width"] = $this->getDimensionValue($product, $widthAttribute);
144+
$goodsItem["height"] = $this->getDimensionValue($product, $heightAttribute);
158145
}
159146
if ($this->scopeConfig->addVolume()) {
160147
$goodsItem["volume"] = $this->getProductVolume($product);
@@ -570,7 +557,7 @@ private function calculateVolumeByDimensions($product): float
570557
default:
571558
$k = 0.000001;
572559
}
573-
560+
574561
$widthAttribute = $this->scopeConfig->getProductAttributeWidth();
575562
$heightAttribute = $this->scopeConfig->getProductAttributeHeight();
576563
$lengthAttribute = $this->scopeConfig->getProductAttributeLength();
@@ -593,4 +580,44 @@ private function reformatVolumeData($value): float
593580

594581
return (float)str_replace(',', '.', $value);
595582
}
583+
584+
/**
585+
* Calculate dimension value based on attribute and metric.
586+
*
587+
* @param ProductInterface $product
588+
* @param string $attribute
589+
* @return float
590+
*/
591+
private function getDimensionValue(ProductInterface $product, string $attribute): float
592+
{
593+
if (!$attribute) {
594+
return 0.0;
595+
}
596+
$value = $product->getData($attribute);
597+
$k = $this->getMetricFactor();
598+
return $value !== null ? (float)str_replace(',', '.', $value) * $k : 0.0;
599+
}
600+
601+
/**
602+
* @return float|int
603+
*/
604+
private function getMetricFactor()
605+
{
606+
if (!$this->metricFactor) {
607+
switch ($this->scopeConfig->getDimensionsMetric()) {
608+
case DimensionsMetric::METRIC_MM:
609+
$this->metricFactor = 0.1;
610+
break;
611+
case DimensionsMetric::METRIC_CM:
612+
$this->metricFactor = 1;
613+
break;
614+
case DimensionsMetric::METRIC_M:
615+
$this->metricFactor = 100;
616+
break;
617+
default:
618+
$this->metricFactor = 1;
619+
}
620+
}
621+
return $this->metricFactor;
622+
}
596623
}

Model/Order/WidgetConfigProvider.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ public function getConfig()
118118
'sortingModel' => [
119119
'orderBy' => 'PRICE',
120120
'sortOrder' => 'ASC'
121-
]
121+
],
122+
'locale' => $this->getLocale()
122123
];
123124
$this->generalHelper->addTolog('Request to change from admin', $config);
124125

@@ -229,4 +230,22 @@ public function getOrder()
229230
{
230231
return $this->order;
231232
}
233+
234+
/**
235+
* @return array|mixed|string|string[]
236+
*/
237+
private function getLocale()
238+
{
239+
$store = $this->getOrder()->getStore();
240+
$locale = $this->scopeConfig->getValue(
241+
'general/locale/code',
242+
$store
243+
);
244+
245+
if ($locale == 'en_US') {
246+
return 'en-GB';
247+
}
248+
249+
return str_replace('_', '-', $locale);
250+
}
232251
}

Plugin/Sales/OrderRepositoryPlugin.php

+48-20
Original file line numberDiff line numberDiff line change
@@ -92,45 +92,73 @@ public function afterSave(
9292
OrderRepository $subject,
9393
OrderInterface $order
9494
) {
95-
$shippingMethod = $order->getShippingMethod(true);
96-
97-
if (!$shippingMethod) {
98-
return $order;
99-
}
100-
101-
$carrierCode = $shippingMethod->getData('carrier_code');
102-
103-
if ($carrierCode !== Paazlshipping::CODE) {
95+
if (!$this->isPaazlShipping($order)) {
10496
return $order;
10597
}
10698

10799
try {
100+
// Check if an order reference already exists
108101
$this->orderReferenceRepository->getByOrderId($order->getId());
109102
return $order;
110103
} catch (NoSuchEntityException $e) {
111-
// Reference not found
112-
/** @var OrderReference $orderReference */
113-
$orderReference = $this->orderReferenceFactory->create(['data' => [
114-
OrderReferenceInterface::ORDER_ID => $order->getId(),
115-
]]);
104+
// No reference found, proceed to create one
116105
}
117106

118-
try {
119-
$quote = $this->cartRepository->get($order->getQuoteId());
107+
$orderReference = $this->createOrderReference($order);
120108

121-
$quoteReference = $this->quoteReferenceRepository->getByQuoteId($quote->getId());
122-
} catch (NoSuchEntityException $e) {
109+
if ($orderReference === null) {
123110
return $order;
124111
}
112+
$this->saveOrderReference($orderReference);
113+
return $order;
114+
}
115+
116+
/**
117+
* Check if the order uses Paazl shipping.
118+
*
119+
* @param OrderInterface $order
120+
* @return bool
121+
*/
122+
private function isPaazlShipping(OrderInterface $order): bool
123+
{
124+
$shippingMethod = $order->getShippingMethod(true);
125+
return $shippingMethod && $shippingMethod->getData('carrier_code') === Paazlshipping::CODE;
126+
}
125127

128+
/**
129+
* Create an order reference from the quote reference.
130+
*
131+
* @param OrderInterface $order
132+
* @return OrderReferenceInterface|null
133+
*/
134+
private function createOrderReference(OrderInterface $order): ?OrderReferenceInterface
135+
{
136+
try {
137+
$quoteReference = $this->quoteReferenceRepository->getByQuoteId($order->getQuoteId());
138+
} catch (NoSuchEntityException $e) {
139+
return null;
140+
}
141+
$orderReference = $this->orderReferenceFactory->create([
142+
'data' => [
143+
OrderReferenceInterface::ORDER_ID => $order->getId(),
144+
],
145+
]);
126146
$orderReference->setExtShippingInfo($quoteReference->getExtShippingInfo());
147+
return $orderReference;
148+
}
127149

150+
/**
151+
* Save the order reference and log any exceptions.
152+
*
153+
* @param OrderReferenceInterface $orderReference
154+
* @return void
155+
*/
156+
private function saveOrderReference(OrderReferenceInterface $orderReference): void
157+
{
128158
try {
129159
$this->orderReferenceRepository->save($orderReference);
130160
} catch (\Exception $e) {
131161
$this->generalHelper->addTolog('exception', $e->getMessage());
132162
}
133-
134-
return $order;
135163
}
136164
}

0 commit comments

Comments
 (0)