-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from Dinwy/feature/restoreQuote
Restore Quote when clicking browser back button
- Loading branch information
Showing
12 changed files
with
186 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
name: lint | ||
|
||
on: | ||
- push | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
php-cs-fixer: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Magento Events and Observers | ||
|
||
## Introduction to Magento Events | ||
|
||
Magento utilizes an event-driven architecture to allow for extending and customizing the behavior of the core system and third-party components without modifying the original source code. This architecture revolves around the concept of events and observers: | ||
|
||
- Events: These are specific points in the Magento codebase where Magento announces that something has occurred, such as saving an item, loading a page, or completing a transaction. | ||
- Observers: These are custom modules or scripts that 'listen' for specific events and execute code in response to them. This allows developers to hook into the application lifecycle to add or modify functionality dynamically. | ||
|
||
## How Events Work in Magento | ||
|
||
When an event is triggered in Magento, any observers that are configured to listen to that event are executed. This mechanism is managed by Magento's event dispatch system, which handles the mapping of events to their respective observers. The process can be summarized as follows: | ||
|
||
- Event Triggering: An event is triggered by calling a method like $this->_eventManager->dispatch('event_name', ['data' => $data]) from within Magento's core code or a custom module. | ||
- Observer Execution: Once an event is dispatched, Magento checks for observers registered for that event and executes their execute method, passing an Observer object that contains any relevant data about the event. | ||
|
||
## Configuration of Observers | ||
|
||
Observers are configured in a module's events.xml file, which is located in the module's etc directory. The file specifies which events an observer is listening to and the class that contains the logic to be executed when the event is triggered. | ||
|
||
Example of an events.xml configuration: | ||
|
||
```xml | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> | ||
<event name="controller_action_postdispatch_checkout_index_index"> | ||
<observer name="restore_quote_from_session" instance="Komoju\Payments\Observer\RestoreQuoteFromSession" /> | ||
</event> | ||
</config> | ||
``` | ||
|
||
## Restore Quote Session Observer | ||
### What is the Restore Quote Session? | ||
|
||
The `RestoreQuoteFromSession` observer is designed to enhance the user experience during the checkout process. It specifically addresses scenarios where a customer may not complete their payment, leaving their order in a pending_payment state. | ||
|
||
### Why This Functionality Is Important | ||
|
||
Restoring a quote to the session in cases where the payment was not completed is crucial for the following reasons: | ||
|
||
- User Convenience: It provides a seamless experience for users who may have navigated away from the checkout page or experienced an interruption during the payment process. | ||
- Increased Conversion Rates: By allowing users to easily return to their last state in the checkout process, it potentially increases the likelihood of completing the sale. | ||
- Reduced Frustration: Minimizes customer frustration by eliminating the need to re-add products to the cart and re-enter information. | ||
|
||
```php | ||
public function execute(Observer $observer) | ||
{ | ||
$quote = $this->checkoutSession->getQuote(); | ||
|
||
if ($quote) { | ||
$order = $this->checkoutSession->getLastRealOrder(); | ||
|
||
if ($order) { | ||
$orderStatus = $order->getStatus(); | ||
|
||
if ($orderStatus == ORDER::STATE_PENDING_PAYMENT) { | ||
$this->checkoutSession->restoreQuote(); | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
Explanation: | ||
- State Check and Action: Only if the order's status matches Order::STATE_PENDING_PAYMENT then perform the action to restore the quote. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/app/code/Komoju/Payments/Observer/RestoreQuoteFromSession.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Komoju\Payments\Observer; | ||
|
||
use Magento\Checkout\Model\Session as CheckoutSession; | ||
use Magento\Framework\App\ObjectManager; | ||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Session\SessionManager; | ||
use Magento\Sales\Model\Order; | ||
use Magento\Quote\Api\CartRepositoryInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Observer for restoring a customer's quote during the checkout, | ||
* if the last order state is pending payment. | ||
* | ||
* Configured to listen to `controller_action_postdispatch_checkout_index_index` event. | ||
* It verifies if the last real order within the session is still pending payment. | ||
* If so, it restores the quote to the session. | ||
* This allows customers to resume their cart and complete the transaction without re-entering info. | ||
* For more information, please check the events_and_observers.md file in docs folder. | ||
*/ | ||
class RestoreQuoteFromSession implements ObserverInterface | ||
{ | ||
protected CheckoutSession $checkoutSession; | ||
protected SessionManager $sessionManager; | ||
protected CartRepositoryInterface $quoteRepository; | ||
private LoggerInterface $logger; | ||
|
||
public function __construct( | ||
CheckoutSession $checkoutSession, | ||
SessionManager $sessionManager, | ||
CartRepositoryInterface $quoteRepository, | ||
LoggerInterface $logger = null | ||
) { | ||
$this->checkoutSession = $checkoutSession; | ||
$this->sessionManager = $sessionManager; | ||
$this->quoteRepository = $quoteRepository; | ||
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class); | ||
} | ||
|
||
public function execute(Observer $observer) | ||
{ | ||
$quote = $this->checkoutSession->getQuote(); | ||
|
||
if ($quote) { | ||
$order = $this->checkoutSession->getLastRealOrder(); | ||
|
||
if ($order && $order->getStatus() == Order::STATE_PENDING_PAYMENT) { | ||
$this->checkoutSession->restoreQuote(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters