Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes needed to support timeframe selection and retrieval #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Api/Data/PostnlRequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ public function setLocationCode($locationCode);
public function setName($name);

public function setFrom($from);

public function setDeliveryDate($deliveryDate);

public function setTo($to);
}
10 changes: 10 additions & 0 deletions Request/PostnlRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,14 @@ public function setFrom($from): void
{
$this->setData('from', $from);
}

public function setDeliveryDate($deliveryDate)
{
$this->setData('date', $deliveryDate);
}

public function setTo($to)
{
$this->setData('to', $to);
}
}
34 changes: 30 additions & 4 deletions Service/DeliveryOptions/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,16 @@ private function saveDeliveryOption(PostnlRequestInterface $postnlRequest): bool
$params = $this->orderParams->get($this->addSessionDataToParams($postnlRequest->getParams()));
$postnlOrder = $this->utils->getPostNLOrderByQuoteId($params['quote_id']);

$this->savePostNLOrderData($params, $postnlOrder);

if ($type != 'pickup') {
$this->pickupAddress->remove();
if ($type == 'fallback') {
unset ($params['is_pakjegemak']);
}

$this->savePostNLOrderData($params, $postnlOrder);

if ($type == 'pickup') {
$this->pickupAddress->set($params['pg_address']);
} else if ($type == 'delivery') {
$this->pickupAddress->remove();
}

return true;
Expand All @@ -80,6 +82,14 @@ private function saveDeliveryOption(PostnlRequestInterface $postnlRequest): bool
private function savePostNLOrderData(array $params, PostnlOrder $postnlOrder): void
{
foreach ($params as $key => $value) {
if ($key === PostnlOrder::FIELD_AC_INFORMATION) {
$value = $this->convertParamValueToJson($value);
}

if (empty($value) && $value !== 0) {
continue;
}

$postnlOrder->setData($key, $value);
}

Expand Down Expand Up @@ -116,6 +126,22 @@ private function addSessionDataToParams(array $params) : array
$params['date'] = $this->utils->getDeliveryDay($params['address']);
}

if ($params['date'] === 'keep') {
unset($params['date']);
}

return $params;
}

public function convertParamValueToJson(mixed $value) : mixed
{
if (is_array($value) && empty($value)) {
$value = null;
}

if (!empty($value)) {
$value = json_encode($value);
}
return $value;
}
}
14 changes: 11 additions & 3 deletions Utils/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Magento\Quote\Model\Quote\Address;
use Vendic\PostnlApi\Api\Data\LocationInterface;
use Vendic\PostnlApi\Api\Data\PostnlRequestInterface;
use Vendic\PostnlApi\Api\TimeframesInterface;
use Vendic\PostnlApi\Api\Data\TimeframeInterface;
use Vendic\PostnlApi\Api\Data\PostnlRequestInterfaceFactory;

class RequestBuilder
Expand Down Expand Up @@ -112,11 +112,11 @@ public function buildForPickupSave(Address $billingAddress, LocationInterface $l
*/
public function buildForDeliverySave(
Address $shippingAddress,
?TimeframesInterface $timeframe = null
?TimeframeInterface $timeframe = null
): PostnlRequestInterface {
/** @var PostnlRequestInterface $request */
$request = $this->postnlRequestFactory->create();
$request->setType('fallback');
$request->setType($timeframe ? 'delivery' : 'fallback');

// Extract street and housenumber from street array
/** @var string|array $street */
Expand Down Expand Up @@ -146,6 +146,14 @@ public function buildForDeliverySave(
$request->setType('EPS');
}

// Delivery Timeframes
if ($timeframe) {
$request->setDeliveryDate($timeframe->getDate());
$request->setOption($timeframe->getOption());
$request->setFrom($timeframe->getFrom());
$request->setTo($timeframe->getTo());
}

return $request;
}

Expand Down