6

I'm developing Magento 2 extension. I've added controller_action_predispatch_checkout event observer and trying to add a error message via ManagerInterface.

events.xml:

<event name="controller_action_predispatch_checkout">
    <observer name="controller_action_predispatch_checkout" instance="<namespace>\<module>\Observer\CheckoutPredispatchObserver" />
</event>

CheckoutPredispatchObserver.php:

class CheckoutPredispatchObserver implements \Magento\Framework\Event\ObserverInterface {

private $messageManager;

public function __construct(\Magento\Framework\Message\ManagerInterface $messageManager) {
    $this->messageManager = $messageManager;        
}

public function execute(\Magento\Framework\Event\Observer $observer) {
    $this->messageManager->addErrorMessage("Custom error message");
}

}

However, my error message is not displayed in checkout page:

Checkout page

Instead, I can see my error message twice in checkout success page: Checkout success page

How can I make my error message appear in Checkout page but not in further pages?

  • Did you figure this one out? – harri Oct 2 '18 at 12:24
0

Try this event

checkout_onepage_controller_success_action

$this->_eventManager->dispatch(
    'checkout_onepage_controller_success_action',
    ['order_ids' => [$session->getLastOrderId()]]
);

These are all checkout events

1) checkout_directpost_placeOrder

$this->eventManager->dispatch(
    'checkout_directpost_placeOrder',
    [
        'result' => $result,
        'action' => $this
    ]);

2) checkout_cart_add_product_complete

$this->_eventManager->dispatch(
    'checkout_cart_add_product_complete',
    ['product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse()]);

3) checkout_cart_update_item_complete

$this->_eventManager->dispatch(
    'checkout_cart_update_item_complete',
    ['item' => $item, 'request' => $this->getRequest(), 'response' => $this->getResponse()]);

4) checkout_controller_onepage_saveOrder

$this->_eventManager->dispatch(
    'checkout_controller_onepage_saveOrder',
    [
'result' => $result,
'action' => $this
    ]);

5) checkout_onepage_controller_success_action

$this->_eventManager->dispatch(
    'checkout_onepage_controller_success_action',
    ['order_ids' => [$session->getLastOrderId()]]);

6) checkout_allow_guest

$this->_eventManager->dispatch(
    'checkout_allow_guest',
    ['quote' => $quote, 'store' => $store, 'result' => $result]);

7) checkout_cart_product_add_after

$this->_eventManager->dispatch(
    'checkout_cart_product_add_after',
    ['quote_item' => $result, 'product' => $product]);

8) checkout_cart_update_items_before

$this->_eventManager->dispatch(
    'checkout_cart_update_items_before',
    ['cart' => $this, 'info' => $infoDataObject]);

9) checkout_cart_update_items_after

$this->_eventManager->dispatch(
    'checkout_cart_update_items_after',
    ['cart' => $this, 'info' => $infoDataObject]);

10) checkout_cart_save_before

$this->_eventManager->dispatch('checkout_cart_save_before', ['cart' => $this]);

11) checkout_cart_save_after

$this->_eventManager->dispatch('checkout_cart_save_after', ['cart' => $this]);

12) checkout_cart_product_update_after

$this->_eventManager->dispatch(
'checkout_cart_product_update_after',
['quote_item' => $result, 'product' => $product]);

13) custom_quote_process

$this->_eventManager->dispatch('custom_quote_process', ['checkout_session' => $this]);

14) checkout_quote_init

$this->_eventManager->dispatch('checkout_quote_init', ['quote' => $quote]);

15) load_customer_quote_before

$this->_eventManager->dispatch('load_customer_quote_before', ['checkout_session' => $this]);

16) checkout_quote_destroy

$this->_eventManager->dispatch('checkout_quote_destroy', ['quote' => $this->getQuote()]);

17) restore_quote

$this->_eventManager->dispatch('restore_quote', ['order' => $order, 'quote' => $quote]);

18) checkout_type_onepage_save_order_after

$this->_eventManager->dispatch(
'checkout_type_onepage_save_order_after',
['order' => $order, 'quote' => $this->getQuote()]);

19) checkout_submit_all_after

$this->_eventManager->dispatch(
    'checkout_submit_all_after',
    [
'order' => $order,
'quote' => $this->getQuote()
    ]);

20) checkout_multishipping_refund_all

$this->_eventManager->dispatch('checkout_multishipping_refund_all', ['orders' => $orders]);
| improve this answer | |
  • The event you suggested (checkout_onepage_controller_success_action) fired before rendering success page. But I need to show my message in Checkout page (/magento/checkout), not in (/checkout/onepage/success). – Rustem Zinnatullin May 18 '17 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.