Abstraction Layer
To simplify your workflow, we provide an abstraction layer that handles most of the heavy lifting for you. Therefore, it is always recommended to extend this abstraction layer whenever you need a custom place order service. In this section, we try to explain in detail what each method is used for, along with examples on how you can tailor it to your specific needs and requirements.
API Methods
placeOrder
- Returns: int
Expects to handle the order and return the final reserved order ID when the canPlaceOrder()
function returns true
.
public function placeOrder(\Magento\Quote\Model\Quote $quote): int
{
return (int) $this->cartManagement->placeOrder($quote->getId(), $quote->getPayment());
}
canPlaceOrder
- Returns: boolean
Returns whether the order can be processed by the placeOrder method or should remain as a quote. This scenario may arise when an external payment gateway creates the order via the Magento API using the quote ID after payment has been made.
handleException
- Returns: void
Manages all exceptions that may occur during the order process. By default, any exceptions will be rethrown to be handled by the place order service processor. This processor logs the exception as an error and dispatches the exception message via the Main component for display on the frontend using an error message evaluation result.
Exception localization is automatically handled when the thrown exception is an instance of \Magento\Framework\Exception\LocalizedException
.
public function handleException(\Exception $exception, \Magewirephp\Magewire\Component $component, \Magento\Quote\Model\Quote $quote): void
{
throw new \Magento\Framework\Exception\LocalizedException(__($exception->getMessage()));
}
canRedirect
- Returns: boolean
Returns whether a redirect should occur after the order process is completed.
getRedirectUrl
- Returns: string
Returns the redirect URL that the customer should be directed to after the order process is complete, along with whether
the service is permitted to redirect based on canRedirect()
. This URL can either be a route or a full URL, such as to
an external payment gateway. The frontend will handle the redirection automatically.
evaluateCompletion
Available since Hyvä Checkout 1.1.13
- Returns: \Hyva\Checkout\Model\Magewire\Component\Evaluation\EvaluationResult
Returns an evaluation result as a set of instructions for the frontend to know what to do next. By default this is
a success result where the place order service processor will
automatically inject an addition redirect result accordingly
if canRedirect
was set to true
and no Redirect result was found.
public function evaluateCompletion(\Hyva\Checkout\Model\Magewire\Component\EvaluationResultFactory $resultFactory, ?int $orderId = null): \Hyva\Checkout\Model\Magewire\Component\Evaluation\EvaluationResult
{
$batch = $resultFactory->createBatch([$resultFactory->createSuccess()]);
if ($orderId === null) {
$batch->push($resultFactory->createErrorMessage('No order ID known...'));
}
return $batch;
}
More details about evaluation results can be found here.
getData
Available since Hyvä Checkout 1.1.13
- Returns: \Hyva\Checkout\Model\Magewire\Payment\AbstractOrderData
Returns the data passed to the Main
component from the browser session storage. This data can be set using
hyvaCheckout.storage.setValue()
. This data is exclusively provided to the place order service for handling
and is not stored elsewhere.