cancel
Showing results for 
Search instead for 
Did you mean: 

Not able to override Mogento_Sales admin controller via routes

Not able to override Mogento_Sales admin controller via routes

Path: Vendor/CustomModule/etc/adminhtml/routes.xml
 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="custom" frontName="custom">
            <module name="Vendor_CustomModule" />
            <module name="Vendor_CustomModule" before="Magento_Sales" />
        </route>
    </router>
</config>



Path: Vendor/CustomModule/Controller/Adminhtml/Order/Create/Save.php
<?php

namespace Vendor\CustomModule\Controller\Adminhtml\Order\Create;

use Magento\Framework\Exception\PaymentException;

class Save extends \Magento\Sales\Controller\Adminhtml\Order\Create\Save
{
    /**
     * Saving quote and create order
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        $path = 'sales/*/';
        $pathParams = [];

        // Do something Here.
        
        try {
            // check if the creation of a new customer is allowed
            if (!$this->_authorization->isAllowed('Magento_Customer::manage')
                && !$this->_getSession()->getCustomerId()
                && !$this->_getSession()->getQuote()->getCustomerIsGuest()
            ) {
                return $this->resultForwardFactory->create()->forward('denied');
            }
            $this->_getOrderCreateModel()->getQuote()->setCustomerId($this->_getSession()->getCustomerId());
            $this->_processActionData('save');
            $paymentData = $this->getRequest()->getPost('payment');
            if ($paymentData) {
                $paymentData['checks'] = [
                    \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_INTERNAL,
                    \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_COUNTRY,
                    \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_CURRENCY,
                    \Magento\Payment\Model\Method\AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
                    \Magento\Payment\Model\Method\AbstractMethod::CHECK_ZERO_TOTAL,
                ];
                $this->_getOrderCreateModel()->setPaymentData($paymentData);
                $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($paymentData);
            }

            $order = $this->_getOrderCreateModel()
                ->setIsValidate(true)
                ->importPostData($this->getRequest()->getPost('order'))
                ->createOrder();

            $this->_getSession()->clearStorage();
            $this->messageManager->addSuccessMessage(__('You created the order.'));
            if ($this->_authorization->isAllowed('Magento_Sales::actions_view')) {
                $pathParams = ['order_id' => $order->getId()];
                $path = 'sales/order/view';
            } else {
                $path = 'sales/order/index';
            }
        } catch (PaymentException $e) {
            $this->_getOrderCreateModel()->saveQuote();
            $message = $e->getMessage();
            if (!empty($message)) {
                $this->messageManager->addErrorMessage($message);
            }
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            // customer can be created before place order flow is completed and should be stored in current session
            $this->_getSession()->setCustomerId((int)$this->_getSession()->getQuote()->getCustomerId());
            $message = $e->getMessage();
            if (!empty($message)) {
                $this->messageManager->addErrorMessage($message);
            }
        } catch (\Exception $e) {
            $this->messageManager->addExceptionMessage($e, __('Order saving error: %1', $e->getMessage()));
        }

        return $this->resultRedirectFactory->create()->setPath($path, $pathParams);
    }
}
1 REPLY 1

Re: Not able to override Mogento_Sales admin controller via routes

Hello @devsamratb2d35 

 

We recommended using the plugin instead of preference for this functionality.

Create one file in your any custom module

Vendor/Namespace/etc/di.xml

<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\Index"> <plugin name="vendor_namespace_plugin_list" type="Vendor\Namespace\Plugin\ControllerOrderIndexPlugin"/> </type>

 

Then create one file for plugin

Vendor/Namespace/Plugin/ControllerOrderIndexPlugin.php

<?php

namespace Vendor\Namespace\Plugin;
use Magento\Framework\Message\ManagerInterface;
use Magento\Sales\Controller\Adminhtml\Order\Index;
/**

 * Class ControllerOrderIndexPlugin

 */
class ControllerOrderIndexPlugin
{
    /**

     * @var ManagerInterface

     */

    private $messageManager;
    /**
     * ControllerOrderIndexPlugin constructor.
     *
     * @param ManagerInterface $messageManager
     */
    public function __construct(ManagerInterface $messageManager)

    {

        $this->messageManager = $messageManager;

    }
    /**
     * @param Index $subject
     */
    public function beforeExecute(

        Index $subject

    ) {
        $this->messageManager->addSuccessMessage(__('Message from new admin controller.'));
    }
}

Hope it helps !

If you find our reply helpful, please give us kudos.

 

A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.

 

WebDesk Solution Support Team

Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789

 

Thank You,


WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789


Location: 150 King St. W. Toronto, ON M5H 1J9