0

I'm trying to find the best practice in order to do a cart validation before an order is placed. if the cart meets my requirements the order should proceed. If not it should show a custom error message and prevent the order from being created.

Any good tips on how to achieve this?

I created a preference for Magento\Checkout\Model\PaymentInformationManagement and do the validation on the savePaymentInformationAndPlaceOrder method but I'm not feeling confortable with this solution and was hoping to do this via a plugin or an observer :)

Thanks in advance !

2
  • do you want this to be actioned on the cart page or the checkout Sep 9 '20 at 19:13
  • I wish this to be activated on the final step of checkout, meaning this should trigger when the customer presses the "Place order" button. Thanks in advance !
    – Joao71
    Sep 11 '20 at 13:08
0

So, here is how to do it.

/vendor/module/etc/events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">   
     <event name="controller_action_predispatch_checkout_index_index">
        <observer name="checkcout_page" instance="vendor\mdodule\Observer\CheckShoppingCartObserver" />
    </event>
</config>

/vendor/module/Observer/CheckShoppingCartObserver.php

<?php

namespace vendor\module\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\Response\RedirectInterface;
use Magento\Framework\Message\ManagerInterface as MessageManager;

class CheckShoppingCartObserver implements ObserverInterface
{  
    protected $redirect;
    protected $checkoutSession;
    protected $productRepository;  
    protected $categoryRepository;  
    protected $customerRepositoryInterface;
    protected $cart;
    public function __construct(
        RedirectInterface                                 $_redirect,
        \Magento\Checkout\Model\Session                   $_checkoutSession,
        \Magento\Catalog\Model\ProductRepository          $_productRepository,
        \Magento\Catalog\Api\CategoryRepositoryInterface  $_categoryRepository,
        \Magento\Customer\Api\CustomerRepositoryInterface $_customerRepositoryInterface,
        \Magento\Checkout\Model\Cart $cart

    )
    {
        $this->redirect                    = $_redirect;
        $this->checkoutSession             = $_checkoutSession;
        $this->productRepository           = $_productRepository;
        $this->categoryRepository          = $_categoryRepository;
        $this->customerRepositoryInterface = $_customerRepositoryInterface;
        $this->cart = $cart;
    }

    public function execute(EventObserver $observer)
    {
         $quote = $this->checkoutSession->getQuote();
         //In my case I redirect the customer to the cart page if the condition is not met       

         If( condition not met)
         {
           $controller = $observer->getControllerAction();
           $this->redirect->redirect($controller->getResponse(), 'checkout/cart');
         }

    }
}
?>

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.