Skip to content

Commit

Permalink
Merge pull request #86 from anoziere/feat/event_set_pickup
Browse files Browse the repository at this point in the history
add event to get information from pickup location choose
  • Loading branch information
anoziere authored May 7, 2024
2 parents 610fe74 + 1c9afe1 commit 6313cb1
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>2.2.20</version>
<version>2.2.21</version>
<authors>
<author>
<name>Vincent Lopes-Vicente</name>
Expand Down
25 changes: 19 additions & 6 deletions Controller/Front/CheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Front\Front;
use OpenApi\Annotations as OA;
use OpenApi\Events\PickupLocationEvent;
use OpenApi\Model\Api\Checkout;
use OpenApi\Model\Api\ModelFactory;
use OpenApi\OpenApi;
Expand Down Expand Up @@ -86,15 +87,30 @@ public function setCheckout(
/** @var Checkout $checkout */
$checkout = $modelFactory->buildModel('Checkout', $decodedContent);

if (isset($decodedContent['pickupAddress'])) {
$pickupAddress = $decodedContent['pickupAddress'];
if(is_string($pickupAddress)) {
$pickupAddress = json_decode($pickupAddress, true, 512, JSON_THROW_ON_ERROR);
}
$pickupLocationEvent = new PickupLocationEvent();
$pickupLocationEvent
->setFromPayload($pickupAddress)
->setCart($cart);
$dispatcher->dispatch(
$pickupLocationEvent,
PickupLocationEvent::MODULE_DELIVERY_SET_PICKUP_LOCATION,
);
}


if (isset($decodedContent['needValidate']) && true === $decodedContent['needValidate']) {
$checkout->checkIsValid();
}


$order = $this->getOrder($request);
$orderEvent = new OrderEvent($order);


$this->setOrderDeliveryPart(
$request,
$session,
Expand All @@ -115,10 +131,8 @@ public function setCheckout(
// Save payment module option choices in session (for the next step)
$session->set(self::PAYMENT_MODULE_OPTION_CHOICES_SESSION_KEY, $decodedContent['paymentOptionChoices']);
}

$responseCheckout = $checkout
->createFromOrder($orderEvent->getOrder());

return OpenApiService::jsonResponse($responseCheckout);
}

Expand Down Expand Up @@ -199,15 +213,14 @@ protected function setOrderDeliveryPart(

$postage = null;
if ($deliveryAddress && $deliveryModule) {

$moduleInstance = $deliveryModule->getDeliveryModuleInstance($this->container);

$deliveryPostageEvent = new DeliveryPostageEvent($moduleInstance, $cart, $deliveryAddress);

$dispatcher->dispatch(
$deliveryPostageEvent,
TheliaEvents::MODULE_DELIVERY_GET_POSTAGE
);

if (!$deliveryPostageEvent->isValidModule()) {
throw new DeliveryException(
Translator::getInstance()->trans('The delivery module is not valid.', [], Front::MESSAGE_DOMAIN)
Expand All @@ -216,7 +229,7 @@ protected function setOrderDeliveryPart(

$postage = $deliveryPostageEvent->getPostage();
}

$orderEvent->setDeliveryAddress($deliveryAddress !== null ? $deliveryAddress->getId() : $securityContext->getCustomerUser()?->getDefaultAddress()?->getId());
$orderEvent->setDeliveryModule($deliveryModule?->getId());

Expand Down
158 changes: 158 additions & 0 deletions Events/PickupLocationEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

namespace OpenApi\Events;

use Thelia\Core\Event\ActionEvent;
use Thelia\Model\Cart;

class PickupLocationEvent extends ActionEvent
{
public const MODULE_DELIVERY_SET_PICKUP_LOCATION = 'thelia.module.delivery.set.pickup_location';

private ?string $id = null;

private ?int $default = null;

private ?string $label = null;

private ?string $title = null;

private ?string $address = null;

private ?string $zipCode = null;

private ?string $city = null;

private ?string $countryCode = null;

private ?string $type = null;

private ?Cart $cart = null;

public function getId(): ?string
{
return $this->id;
}

public function setId(?string $id): PickupLocationEvent
{
$this->id = $id;
return $this;
}

public function getDefault(): ?int
{
return $this->default;
}

public function setDefault(?int $default): PickupLocationEvent
{
$this->default = $default;
return $this;
}

public function getLabel(): ?string
{
return $this->label;
}

public function setLabel(?string $label): PickupLocationEvent
{
$this->label = $label;
return $this;
}

public function getTitle(): ?string
{
return $this->title;
}

public function setTitle(?string $title): PickupLocationEvent
{
$this->title = $title;
return $this;
}

public function getAddress(): ?string
{
return $this->address;
}

public function setAddress(?string $address): PickupLocationEvent
{
$this->address = $address;
return $this;
}

public function getZipCode(): ?string
{
return $this->zipCode;
}

public function setZipCode(?string $zipCode): PickupLocationEvent
{
$this->zipCode = $zipCode;
return $this;
}

public function getCity(): ?string
{
return $this->city;
}

public function setCity(?string $city): PickupLocationEvent
{
$this->city = $city;
return $this;
}

public function getCountryCode(): ?string
{
return $this->countryCode;
}

public function setCountryCode(?string $countryCode): PickupLocationEvent
{
$this->countryCode = $countryCode;
return $this;
}

public function getType(): ?string
{
return $this->type;
}

public function setType(?string $type): PickupLocationEvent
{
$this->type = $type;
return $this;
}

public function getCart(): ?Cart
{
return $this->cart;
}

public function setCart(?Cart $cart): PickupLocationEvent
{
$this->cart = $cart;
return $this;
}

public function setFromPayload(array $payload): PickupLocationEvent
{
$this
->setId($payload['id'] ?? null)
->setDefault($payload['default'] ?? null)
->setLabel($payload['label'] ?? null)
->setTitle($payload['title'] ?? null)
->setAddress($payload['address1'] ?? null)
->setZipCode($payload['zipCode'] ?? null)
->setCity($payload['city'] ?? null)
->setCountryCode($payload['countryCode'] ?? null)
->setType($payload['type'] ?? null)
;

return $this;
}
}

0 comments on commit 6313cb1

Please sign in to comment.