I have created some coupons. when a coupon applied it dispatch an event salesrule_validator_process.

In the observer I will check some condition. based on condition the coupon will apply to the cart. But here how can I show a custom validation message if coupon not allowed?


public function execute(EventObserver $observer)
    //return $this;
    $rule           =   $observer->getEvent()->getRule()->getData();
    $couponCode    =   $rule['coupon_code'];
    $customerId     =   $this->_customerSession->getCustomer()->getData('entity_id');

    $userId     =   '';
    $validity   =   -1; 

    $userId    =   $this->_rule->customerIdToUserId($customerId);
    $validity  =   $this->_rule->checkPromoRule($userId,$couponCode);

    if ($validity == 0) {
        $this->_logger->debug("Coupon Not Valid");
        $this->_logger->critical("Couponcode Not Allowed");
        return $this;

    return $this;

enter image description here

  • You can use \Magento\Framework\Message\ManagerInterface class to show success or failure messages in observer. Mar 5 '18 at 6:37
  • $this->_messageManager->addError(__("Error")); this is not showing any messages. I used namespace , extended constructor and compiled. but its not showing any mesagges.
    – Midlaj
    Mar 5 '18 at 7:05

Your Answer

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

Browse other questions tagged or ask your own question.