Question

I need to put my custom validation before place order.

magento\app\code\Custom\Module\etc\events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_place_before"> 
        <observer name="check_price" instance="Custom\Module\Observer\BeforePlaceOrderObserver" />
    </event>    
</config>

magento\app\code\Custom\Module\Observer\BeforePlaceOrderObserver.php

namespace Custom\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\ObjectManager;

class BeforePlaceOrderObserver implements ObserverInterface {

    public function execute(Observer $observer) {
        die("Error Message");
        exit;
    }
}

It still places order. Need to used $this->_messageManager->addError(__('Error'))

Was it helpful?

Solution

Finally It works :)

magento\app\code\Custom\Module\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Model\PaymentInformationManagement" type="Custom\Module\Model\PaymentInformationManagement" />
</config>

magento\app\code\Custom\Module\Model\PaymentInformationManagement.php

namespace Custom\Module\Model;

use Magento\Framework\Exception\CouldNotSaveException;

class PaymentInformationManagement extends \Magento\Checkout\Model\PaymentInformationManagement {

    public function savePaymentInformationAndPlaceOrder($cartId, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress = null) {
        if(<your condition){
                throw new CouldNotSaveException(__('Error'));
                return false;
        }else{
            $this->savePaymentInformation($cartId, $paymentMethod, $billingAddress);
            try {
                $orderId = $this->cartManagement->placeOrder($cartId);
            } catch (\Exception $e) {
                throw new CouldNotSaveException(
                __('An error occurred on the server. Please try to place the order again.'), $e
                );
            }
            return $orderId;
        }
    }
}

OTHER TIPS

Place order action using API area.

Move events.xml to webapi_rest folder. And modify your code:

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

    /**
     * @var \Magento\Framework\Message\ManagerInterface
     */
    protected $_messageManager;

    /**
     * OrderPlaceBefore constructor.
     * @param \Magento\Framework\Message\ManagerInterface $messageManager
     */
    public function __construct(
        \Magento\Framework\Message\ManagerInterface $messageManager
    ) {
        $this->_messageManager = $messageManager;
    }

    /**
     * Execute observer
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {

        $this->_messageManager->addError('Error Message');
        exit;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top