I am using Magento 2.1.3 and I need to display an error message in the shipping information page. Currently I'm using this code:

throw new StateException(__('Shipping is not available on the provided zipcode'));

but the issue is that the error message is just showing 2 or 3 seconds. I need to show this message up to refresh the page.


namespace Sem\Shipment\Plugin\Checkout\Model;
use Magento\Framework\Exception\StateException;
use Magento\Checkout\Model\Session;
use Magento\Framework\View\Element\Messages;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\UrlInterface $urlInterface;

class ShippingInformationManagement

    protected $_messageManager;
    protected $jsonResultFactory;
    protected $_checkout; 

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $jsonResultFactory,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
        \Magento\Checkout\Model\Session $session,
        \Magento\Framework\UrlInterface $urlInterface

    ) {
        $this->_messageManager = $messageManager;
        $this->jsonResultFactory = $jsonResultFactory;
        $this->_session = $session;
        $this->_urlInterface = $urlInterface;

    public function beforeSaveAddressInformation(
        \Magento\Checkout\Model\ShippingInformationManagement $subject,
        \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation

        $objectManager =   \Magento\Framework\App\ObjectManager::getInstance();
        $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
        $connection = $resource->getConnection();
        $items = $this->_session->getQuote()->getAllVisibleItems();
        $address = $addressInformation->getShippingAddress();
        $postcode = $address->getData('postcode');
        $result = $this->jsonResultFactory->create();
        if (count($items) > 0){
            foreach ($items as $item)
                $pname= $item->getName();
                $sql ="SELECT ca.postcode FROM
                       mgcb_marketplace_product mp
                       JOIN mgcb_marketplace_userdata mu ON mp.seller_id = mu.seller_id
                       JOIN mgcb_customer_entity ce ON mu.seller_id = ce.entity_id
                       JOIN mgcb_customer_address_entity ca ON ce.entity_id = ca.parent_id
                       WHERE(mp.mageproduct_id = '$pid')";
                $qryresult = $connection->fetchAll($sql);
                foreach ($qryresult as $qryresult)

                foreach($result as $object) {
                    $stat = "NO SERVICE AVAILABLE";
                if($stat=="NO SERVICE AVAILABLE")

                    $msg = "Shipping is not available on the provided zipcode For the following product: <em>" . $item->getName() . "</em>
                            Currently Added Product : " . $linkUrl;
                    throw new StateException(__($msg));

  • $this->messageManager->addError( __('Shipping is not available on the provided zipcode.') ); This injects the \Magento\Framework\Message\ManagerInterface object in its __construct function using the \Magento\Framework\App\Action\Context $context object Commented Jul 6, 2017 at 8:18
  • @ Rama Chandran Please look at my code now it display in cosole not in the site
    – amith lal
    Commented Jul 6, 2017 at 8:31
  • I understand. you are call from Ajax right? so return error message to ajax and dont set any duration for display error message :) Commented Jul 6, 2017 at 8:38
  • @ Rama Chandran I'm using the plugin feature of magento 2 here i use the "beforeSaveAddressInformation" method not ajax call it just an event
    – amith lal
    Commented Jul 6, 2017 at 8:40
  • i will check and tell Commented Jul 6, 2017 at 8:47

2 Answers 2


I know it's three months down the line, but am sure this will help someone.

You can do this by adjusting the timeout period on the file.


It is around lines 70/71. you can set the time out to be a figure of your choice (in milliseconds)


As Moses has detailed above, the timeout is set in:


This can simply be applied as a Mixin inside your theme. Mixin's are a way of changing/extending functions within JS files without over-riding the entire file. If you aren't family with mixins, I'd suggest checking out Magento's documentation: https://devdocs.magento.com/guides/v2.4/javascript-dev-guide/javascript/js_mixins.html

Create the file:


], function (ko, $, Component, globalMessages) {
    'use strict';
    var mixin = {
         * @param {Boolean} isHidden
        onHiddenChange: function (isHidden) {
            // Hide message block if needed
            if (isHidden) {
                setTimeout(function () {
                    $(this.selector).hide('blind', {}, this.hideSpeed);
                }.bind(this), 30000);
    return function (target) {
        return target.extend(mixin);

Create the file:


var config = {
    config: {
        mixins: {
            'Magento_Ui/js/view/messages': {
                'Magento_Ui/js/view/messages-mixin': true

Modify the vendor/theme name, and timeout accordingly.

If the above doesn't work, it's likely that the theme, or a 3rd party module is already doing something with Message timeouts. Best thing to do in this case is to search for keywords related to the functionality, e.g. globalMessages or DOM element '.page.messages' . I found that a popular 3rd party extension (UB Theme Mega Nav) was modifying the behaviour of messages in a PHTML file within a script tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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