4

I am using a plugin feature in module to validate the zip code for delivery available. I need to prevent the next step if the zip code is not available for delivery by showing a message in checkout page. hear i have get error message in console but not showing in site

Hear is my code

/app/code/Ship/Details/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">
 <type name="Magento\Checkout\Model\ShippingInformationManagement">
 <plugin name="get_shipping_info" type="Sem\Shipment\Plugin\Checkout\Model\ShippingInformationManagement" sortOrder="1" disabled="false"/>
 </type>
</config>

plugin code

/app/code/Sem/Shipment/Plugin/Checkout/Model/ShippingInformationManagement.php

<?php
namespace Sem\Shipment\Plugin\Checkout\Model;
use Magento\Framework\Exception\StateException;
class ShippingInformationManagement
{
 protected $_messageManager;
 protected $jsonResultFactory;
 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
 ) {
 $this->_messageManager = $messageManager;
 $this->jsonResultFactory = $jsonResultFactory;
 }
 public function beforeSaveAddressInformation(
 \Magento\Checkout\Model\ShippingInformationManagement $subject,
 $cartId,
 \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
 )
 {
 $address = $addressInformation->getShippingAddress();
 $postcode = $address->getData('postcode');
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $result = $this->jsonResultFactory->create();
 $stat="no sevice";
 throw new StateException(__($stat)); 
}
}

In my console enter image description here

How can i resolve this issue now the message is not showing also i cant move to next step payment

asked Jun 21, 2017 at 6:13

2 Answers 2

0

Please use like this:

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
 ) {
 $this->_messageManager = $messageManager;
 $this->jsonResultFactory = $jsonResultFactory;
 parent::__construct($context);
 }
answered Jun 21, 2017 at 7:08
4
  • after change like this the error changed to" POST examole.com/rest/default/V1/… 500 Internal Server Error 2.88s" Commented Jun 21, 2017 at 7:21
  • let me check and update u Commented Jun 21, 2017 at 7:26
  • Any updation for this issue Commented Jun 22, 2017 at 10:27
  • when i use this parent::__construct($context); i'm getting the internal server it saying no access to parent class Commented Jun 23, 2017 at 6:27
0

You've injected ManagerInterface into the class, so you put this line before throw exception

$this->messageManager->addErrorMessage('no service');

And the result will be like this

enter image description here

answered Jul 16, 2018 at 8:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.