0

How to override Magento\Sales\Controller\Adminhtml\Order\AddressSave contoller?

di.xml

<preference for="Magento\Sales\Controller\Adminhtml\Order\AddressSave" type="Xxx\Order\Controller\Adminhtml\Order\Address" /> 

Contoller

<?php

namespace Xxx\Order\Controller\Adminhtml\Order;

use Magento\Sales\Controller\Adminhtml\Order\AddressSave;


class Address extends AddressSave
{
    public function execute()
    {        
        echo "hi";
        exit;}}

I am trying to do this but it's not working.

Any help will be appreciated.

Update

<?php

namespace Xxx\Yyy\Plugin;

use  Xxx\Zzz\Model\Data;
class AddressSave
{

    /**
     * @var \Magento\Framework\Controller\Result\RedirectFactory
     */
    protected $resultRedirectFactory;
    protected $_pincodeModel;

    /**
     * @var \Magento\Framework\Message\ManagerInterface
     */
    protected $messageManager;

    public function __construct(
    \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory, \Magento\Framework\Message\ManagerInterface $messageManager,Data $pincodeModel    )
    {
        $this->resultRedirectFactory = $resultRedirectFactory;
        $this->messageManager = $messageManager;
        $this->_pincodeModel = $pincodeModel;
    }

    public function aroundExecute(
    \Magento\Sales\Controller\Adminhtml\Order\AddressSave $subject, \Closure $proceed
    )
    {
        $resultRedirect = $this->resultRedirectFactory->create();

        $codAvailability = $this->_pincodeModel->isDeliveryAvailable($postCode) ? "yes" : "no";
        if ($codAvailability == 'yes') {// Write your own logic here
            $result = $proceed();
            return $result;
        } else {
            $this->messageManager->addError('We can\'t update the order address right now.');
            return $resultRedirect->setPath('sales/*/');
        }
    }

}

di.xml

 <type name="Magento\Sales\Controller\Adminhtml\Order\AddressSave">
        <plugin name="admin_order_address_save" type="Xxx\Yyy\Plugin\AddressSave" />
    </type>


}else {
        $action = $this->_request->getActionName();
        $this->messageManager->addError(__('Delivery is not available at ' . $postCode));
        $this->_actionFlag->set($action, \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
        $redirectionUrl = $this->_url->getUrl('adminhtml/sales_order/addressSave', array('address_id' => $addressId));
        $this->_responseFactory->create()->setRedirect($redirectionUrl)->sendResponse();
        return $this;
    }
3
  • Is your module activated and installed properly ? Feb 15 '18 at 4:34
  • What do you exactly want to do? I recommend you to use plugin instead of overriding the whole controller method. You can use before, after, or around plugin as per your need. Feb 15 '18 at 4:34
  • I want add additional condition for before address save...so i can use beforePlugin
    – Mahi M
    Feb 15 '18 at 4:42
2

You can use an around plugin.

Create following files.

app/code/Vendor/Module/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\Sales\Controller\Adminhtml\Order\AddressSave">
        <plugin name="admin_order_address_save" type="Vendor\Module\Plugin\AddressSave" />
    </type>

</config>

app/code/Vendor/Module/Plugin/AddressSave.php

<?php

namespace Vendor\Module\Plugin;

class AddressSave
{
    /**
     * @var \Magento\Framework\Controller\Result\RedirectFactory
     */
    protected $resultRedirectFactory;

    /**
     * @var \Magento\Framework\Message\ManagerInterface
     */
    protected $messageManager;

    public function __construct(
        \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
        \Magento\Framework\Message\ManagerInterface $messageManager
    ) {
        $this->resultRedirectFactory = $resultRedirectFactory;
        $this->messageManager = $messageManager;
    }

    public function aroundExecute(
        \Magento\Sales\Controller\Adminhtml\Order\AddressSave $subject,
        \Closure $proceed
    ) {
        $resultRedirect = $this->resultRedirectFactory->create();

        $condition = 1; // Write your own logic here

        if ($condition == 1) {
            $result = $proceed();
            return $result;
        } else {
            $this->messageManager->addError('We can\'t update the order address right now.');
            return $resultRedirect->setPath('sales/*/');
        }
    }

}
5
  • If you still face any problem, you can ask me. Feb 15 '18 at 6:31
  • no change ....after this @Dinesh
    – Mahi M
    Feb 15 '18 at 6:37
  • I have checked the code. It's working fine. It might be possible there is some mistake in your code. Please update your current code in the question. Feb 15 '18 at 6:40
  • Update my post..
    – Mahi M
    Feb 15 '18 at 6:49
  • What error are you getting? Feb 15 '18 at 6:54
0

app/code/Xxx/Sales/etc/di.xml

<preference for="Magento\Sales\Controller\Adminhtml\Order\AddressSave" type="Xxx\Sales\Controller\Adminhtml\Order\AddressSave" />

app/code/Xxx/Sales/Controller/Adminhtml/Order/AddressSave.php

<?php

namespace Xxx\Sales\Controller\Adminhtml\Order;

class AddressSave extends \Magento\Sales\Controller\Adminhtml\Order
{
    public function execute()
    {        
        echo "hi";
        exit;
    }
}
1
  • its not working
    – Mahi M
    Feb 15 '18 at 5:01

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.