Skip to content

Commit 5a72e42

Browse files
committed
geocode addresses & fixes
1 parent 79c9ac2 commit 5a72e42

File tree

6 files changed

+80
-6
lines changed

6 files changed

+80
-6
lines changed

app/base/commerce/ShipmentTrackers/DHLTracker.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ class DHLTracker extends AbstractOAuth2ApiClient implements ShipmentTrackerInter
2525
{
2626
private const CARRIER_CODE = 'dhl';
2727

28-
public function __construct(ContainerInterface $container)
29-
{
28+
public function __construct(
29+
protected ContainerInterface $container
30+
) {
3031
parent::__construct(
3132
$container,
3233
App::getInstance()->getSiteData()->getConfigValue('commerce/shipment_trackers/dhl/client_id'),

app/base/commerce/ShipmentTrackers/GLSTracker.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use App\Base\Models\OrderShipment;
1818
use App\Base\Abstracts\ContainerAwareObject;
1919
use Degami\Basics\Exceptions\BasicException;
20+
use Psr\Container\ContainerInterface;
2021

2122
class GLSTracker extends ContainerAwareObject implements ShipmentTrackerInterface
2223
{
@@ -28,7 +29,7 @@ class GLSTracker extends ContainerAwareObject implements ShipmentTrackerInterfac
2829

2930

3031
public function __construct(
31-
protected $container,
32+
protected ContainerInterface $container,
3233
) {
3334
parent::__construct($container);
3435

app/base/controllers/Frontend/Users/Addresses.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,19 @@ public function formSubmitted(FAPI\Form $form, array &$form_state): mixed
226226
->setPostcode($values['postcode'])
227227
->setCountryCode($values['country_code'])
228228
->setPhone($values['phone'])
229-
->setEmail($values['email'])
230-
->persist();
229+
->setEmail($values['email']);
230+
231+
if (!$address->getLongitude() && !$address->getLatitude()) {
232+
try {
233+
$geocoder = App::getInstance()->getGeocoder();
234+
$coords = $geocoder->geocode($address->getFullAddress());
235+
if ($coords) {
236+
$address->setLatitude($coords['lat'])->setLongitude($coords['lon']);
237+
}
238+
} catch (\Exception $e) {}
239+
}
240+
241+
$address->persist();
231242

232243
$this->addInfoFlashMessage($this->getUtils()->translate("Address saved"));
233244
break;

app/base/models/Order.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,15 @@ public function setBillingAddress(OrderAddress|Address $address): self
200200
{
201201
if (!$address instanceof OrderAddress) {
202202
$address = OrderAddress::createFromAddress($address);
203+
if (!$address->getLongitude() && !$address->getLatitude()) {
204+
try {
205+
$geocoder = App::getInstance()->getGeocoder();
206+
$coords = $geocoder->geocode($address->getFullAddress());
207+
if ($coords) {
208+
$address->setLatitude($coords['lat'])->setLongitude($coords['lon']);
209+
}
210+
} catch (\Exception $e) {}
211+
}
203212
}
204213

205214
$this->billingAddress = $address->setType('billing')->setOrder($this);
@@ -216,6 +225,15 @@ public function setShippingAddress(OrderAddress|Address $address): self
216225
{
217226
if (!$address instanceof OrderAddress) {
218227
$address = OrderAddress::createFromAddress($address);
228+
if (!$address->getLongitude() && !$address->getLatitude()) {
229+
try {
230+
$geocoder = App::getInstance()->getGeocoder();
231+
$coords = $geocoder->geocode($address->getFullAddress());
232+
if ($coords) {
233+
$address->setLatitude($coords['lat'])->setLongitude($coords['lon']);
234+
}
235+
} catch (\Exception $e) {}
236+
}
219237
}
220238

221239
$this->shippingAddress = $address->setType('shipping')->setOrder($this);

app/base/models/OrderAddress.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ public static function createFromAddress(Address $address): self
105105
->setPostcode($address->getPostcode())
106106
->setCountryCode($address->getCountryCode())
107107
->setPhone($address->getPhone())
108-
->setEmail($address->getEmail());
108+
->setEmail($address->getEmail())
109+
->setLatitude($address->getLatitude())
110+
->setLongitude($address->getLongitude());
109111
}
110112

111113
/**

app/base/traits/WithLatLngTrait.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace App\Base\Traits;
1515

1616
use App\Base\Abstracts\Models\BaseCollection;
17+
use App\Base\Abstracts\Models\BaseModel;
1718
use App\Base\Abstracts\Models\ModelWithLocationCollection;
1819
use App\Base\GraphQl\GraphQLExport;
1920

@@ -42,6 +43,10 @@ trait WithLatLngTrait
4243
#[GraphQLExport]
4344
public function getLatitude(): float
4445
{
46+
if ($this instanceof BaseModel && !$this->latitude) {
47+
$this->latitude = floatval($this->getData('latitude') ?? 0.0);
48+
}
49+
4550
return $this->latitude;
4651
}
4752

@@ -53,6 +58,10 @@ public function getLatitude(): float
5358
#[GraphQLExport]
5459
public function getLongitude(): float
5560
{
61+
if ($this instanceof BaseModel && !$this->longitude) {
62+
$this->longitude = floatval($this->getData('longitude') ?? 0.0);
63+
}
64+
5665
return $this->longitude;
5766
}
5867

@@ -88,4 +97,36 @@ public function nearBy(float $radius) : ModelWithLocationCollection|BaseCollecti
8897

8998
return $collection;
9099
}
100+
101+
/**
102+
* sets latitude
103+
*
104+
* @param float $latitude
105+
* @return self
106+
*/
107+
public function setLatitude(float $latitude): self
108+
{
109+
$this->latitude = $latitude;
110+
if ($this instanceof BaseModel) {
111+
$this->setData(['latitude' => $latitude]);
112+
}
113+
114+
return $this;
115+
}
116+
117+
/**
118+
* sets longitude
119+
*
120+
* @param float $longitude
121+
* @return self
122+
*/
123+
public function setLongitude(float $longitude): self
124+
{
125+
$this->longitude = $longitude;
126+
if ($this instanceof BaseModel) {
127+
$this->setData(['longitude' => $longitude]);
128+
}
129+
130+
return $this;
131+
}
91132
}

0 commit comments

Comments
 (0)