5

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'))

7
  • Did you try plugin for before method ? Apr 17, 2017 at 8:56
  • Nope I only tried observer. If we can achieve using Plugin then can go ahead with that also
    – Jackson
    Apr 17, 2017 at 8:57
  • Okay. I think you should try using plugin. Apr 17, 2017 at 8:58
  • Which file & function need to use?
    – Jackson
    Apr 17, 2017 at 8:59
  • You can refer example here blog.chapagain.com.np/… Apr 17, 2017 at 9:01

2 Answers 2

4

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;
        }
    }
}
1
  • not get success. its not working Nov 6, 2017 at 5:54
1

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;
    }
}
1

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.