0

I'm trying to redirect to my shipping information page to cart page based on a condition how can i do this my plugin file is in this path

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

    <?php

namespace Sem\Shipment\Plugin\Checkout\Model;
use Magento\Framework\Exception\StateException;
use Magento\Checkout\Model\Session;
use Magento\Framework\View\Element\Messages;

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

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

    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();
    $resulta = [];
    $pid='';
    if (count($items) > 0){
        foreach ($items as $item)
            $resulta= $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'];


          //Some code using Api here


       $result = json_decode($respon); 
       foreach($result as $object) {
       $stat = $object->status;
      if($stat=="NO SERVICE AVAILABLE")
      {
         //Here i need to redirect to the cart page from checkout By displaying some message
        throw new StateException(__('Shipping is not available on the provided zipcode.'));
     }
    }
    }   
    }

}
}

also i need to print the success and error message in the front end enter image description here

  • Your first foreach missing '{'. Lots of error, could you fix itself? Or use phpstorm IDE it may help. – Sohel Rana Jun 27 '17 at 11:45
1

There are no this type of implementation that you redirect only using plugin. Because in js part check following logic.

If through any exception from backend(plugin/php class whatever) It check if response status 401 is redirect to login page otherwise it's print message in same page. Check Here.

So you need to implement custom solution for this.

So you need to overwrite following JS


Magento/Checkout/view/frontend/web/js/model/shipping-save-processor/default.js

Check following line you need to add your customization here.

OR

Overwrite following JS and customize your own logic

[Update]

Following sample code display same page error message.


namespace Sem\Shipment\Plugin\Checkout\Model;

use Magento\Framework\Exception\StateException;
use Magento\Checkout\Model\Session;
use Magento\Framework\View\Element\Messages;

class ShippingInformationManagement
{
    protected $messageManager;
    protected $jsonResultFactory;
    protected $checkout;
    protected $resource;

    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\App\ResourceConnection $resource
    ) {
        $this->messageManager = $messageManager;
        $this->jsonResultFactory = $jsonResultFactory;
        $this->session = $session;
        $this->resource = $resource;
    }

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

    ) {
        $connection = $this->resource->getConnection();
        $items = $this->session->getQuote()->getAllVisibleItems();
        $address = $addressInformation->getShippingAddress();
        $postcode = $address->getData('postcode');
        $resulta = [];
        $pid='';
        if (count($items) > 0){
            throw new StateException(__('Shipping is not available on the provided zipcode.'));
        }
    }
}
|improve this answer|||||
  • @ Sohel Rana but hear i'm write this code on plugin if you need i will post the full code – amith lal Jun 27 '17 at 10:59
  • You cant redirect only using plugin. You must need to customize js too. – Sohel Rana Jun 27 '17 at 11:03
  • @ Sohel Rana i have updated my full code this one i have used to check the delivery availability using zip code before saving the shipping address i don't know I'm on the correct direction if you have any sujjection please help me – amith lal Jun 27 '17 at 11:08
  • Check updated answer. It may help. You need now js overwrite. Plugin looks good. – Sohel Rana Jun 27 '17 at 11:10
  • Ok I will try this before that i have tried to disply an error message on screen using state exception but i'm getting "400 bad request in console " why this? why can't i print the message on screen – amith lal Jun 27 '17 at 11:14

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.