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
2 Answers 2
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);
}
-
after change like this the error changed to" POST examole.com/rest/default/V1/… 500 Internal Server Error 2.88s"amith lal– amith lal2017年06月21日 07:21:49 +00:00Commented Jun 21, 2017 at 7:21
-
let me check and update uNagaraju Kasa– Nagaraju Kasa2017年06月21日 07:26:07 +00:00Commented Jun 21, 2017 at 7:26
-
Any updation for this issueamith lal– amith lal2017年06月22日 10:27:36 +00:00Commented 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 classamith lal– amith lal2017年06月23日 06:27:12 +00:00Commented Jun 23, 2017 at 6:27
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