0

I have create admin sales grid. All is working fine. And i have added one link in order view page. This link click redirect custom admin form. And Finally i want to save custom admin form. And redirect order view page backurl

View edit link in order view section :-

enter image description here

And then click edit link go to custom admin form.And custom admin form save after redirect order view page.

Order view page URL :- http://127.0.0.1/mag233/admin/sales/order/view/order_id/2/

Custom admin form URL :- http://127.0.0.1/mag233/admin/editfrom/index/index/order_id/2/

And then custom admin form save after come back order view page.

Here is my save controller code :-

<?php

namespace LR\Adminsalesgrid\Controller\Adminhtml\Index;

use Magento\Backend\App\Action;
use LR\Adminsalesgrid\Model\Lr;
use \Magento\Framework\Controller\ResultFactory;

class Save extends \Magento\Backend\App\Action
{
    protected $Custommodel;

    public function __construct(
        Action\Context $context,
        Lr $Custommodel
    ) {
        parent::__construct($context);
        $this->Custommodel = $Custommodel;
    }

    public function execute()
    {
        $data = $this->getRequest()->getPostValue();

        if ($data) {
            $id = $this->getRequest()->getParam('id');

            if ($id) {
                $this->Custommodel->load($id);
            }

            $this->Custommodel->setData($data);

            try {
                $this->Custommodel->save();
                $this->messageManager->addSuccess(__('The data has been saved.'));
                $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
                $resultRedirect->setUrl($this->_redirect->getRefererUrl());

                return $resultRedirect;
            }catch (\Exception $e) {
                $this->messageManager->addException($e, __('Something went wrong while saving the data.'));
            }

            return $resultRedirect->setPath('*/*/edit', ['id' => $this->getRequest()->getParam('id')]);
        }
        return $resultRedirect->setPath('*/*/');
    }
}

Please advice me what is actually problem.

THANKS.

0

2 Answers 2

1

Try code

<?php

namespace LR\Adminsalesgrid\Controller\Adminhtml\Index;

use Magento\Backend\App\Action;
use LR\Adminsalesgrid\Model\Lr;
use \Magento\Framework\Controller\ResultFactory;

class Save extends \Magento\Backend\App\Action
{
    protected $Custommodel;

    public function __construct(
        Action\Context $context,
        Lr $Custommodel
    ) {
        parent::__construct($context);
        $this->Custommodel = $Custommodel;
    }

    public function execute()
    {
        $data = $this->getRequest()->getPostValue();
        if ($data) {
            $id = $this->getRequest()->getParam('id');
            if ($id) {
                $this->Custommodel->load($id);
            }
            $this->Custommodel->setData($data);
            try {
                $this->Custommodel->save();
                $this->messageManager->addSuccess(__('The data has been saved.'));
                //$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
                $order_id = 2; // add your order id here
                $order_url = $this->getUrl("sales/order/view/order_id/".$order_id);
                $resultRedirect->setUrl($order_url);
                return $resultRedirect;
            }catch (\Exception $e) {
                $this->messageManager->addException($e, __('Something went wrong while saving the data.'));
            }
            return $resultRedirect->setPath('*/*/edit', ['id' => $this->getRequest()->getParam('id')]);
        }
        return $resultRedirect->setPath('*/*/');
    }
}

I Hope This Helps You

3
  • please check and update me on above answer.
    – Msquare
    Commented Sep 12, 2020 at 5:31
  • Hello noob here, Is that applicable on Magento 2.4 as well? Commented Sep 5, 2023 at 11:00
  • 1
    Yes @DimitriosDesyllas
    – Msquare
    Commented Sep 5, 2023 at 11:49
1

$this->_redirect->getRefererUrl() this means refer to the previous url which is the custom edit page url. So, It will not redirect to the order view page.

You have to send the order_id to save action function and create the order view url to redirect. getRegereeUrl() will not redirect to order view page.

2
  • Hy, I want to custom form save after back order view page. Thanks. Commented Aug 30, 2020 at 7:09
  • hello @Ritesh Santra how to create Please share with me code Commented Aug 30, 2020 at 16:18

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.