3

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.

<?php

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,
        $cartId,
        \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();
        $pid='';
        if (count($items) > 0){
            foreach ($items as $item)
            { 
                $pname= $item->getName();
                $pid=$item['product_id'];
                $carttotal=$item['grand_total'];
                $weight=$item['weight'];
                $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)
                {
                    $pikup=$qryresult['postcode'];
                }

                foreach($result as $object) {
                    $stat = "NO SERVICE AVAILABLE";
                }
                //print_r($object);
                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;
                    $this->_messageManager->addError(__("errorr"));
                    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 – Rama Chandran M Jul 6 '17 at 8:18
  • @ Rama Chandran Please look at my code now it display in cosole not in the site – amith lal Jul 6 '17 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 :) – Rama Chandran M Jul 6 '17 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 Jul 6 '17 at 8:40
  • i will check and tell – Rama Chandran M Jul 6 '17 at 8:47
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.

vendor/magento/module-ui/view/frontend/web/js/view/messages.js

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

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.