0

This question already has an answer here:

I want to override the controller of vendor in my module

the original file path is Magento/Checkout/Controller/Account/Create to my module path can any one help me.

My Controller code Silk/Helloworld/Controller/Account/Create.php

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Silk\Helloworld\Controller\Account;

use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\NoSuchEntityException;

class Create extends \Magento\Checkout\Controller\Account\Create
{
    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $checkoutSession;

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;

    /**
     * @var \Magento\Sales\Api\OrderCustomerManagementInterface
     */
    protected $orderCustomerService;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Checkout\Model\Session $checkoutSession
     * @param \Magento\Customer\Model\Session $customerSession
     * @param \Magento\Sales\Api\OrderCustomerManagementInterface $orderCustomerService
     * @codeCoverageIgnore
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Sales\Api\OrderCustomerManagementInterface $orderCustomerService
    ) {
        $this->customerSession = $customerSession;
        $this->orderCustomerService = $orderCustomerService;
        parent::__construct($context, $checkoutSession);
    }

    /**
     * Execute request
     *
     * @throws AlreadyExistsException
     * @throws NoSuchEntityException
     * @throws \Exception
     * @return \Magento\Framework\Controller\Result\Json
     */
    public function execute()
    {   
        /** @var \Magento\Framework\Controller\Result\Json $resultJson */
        $resultJson = $this->_objectManager->get(\Magento\Framework\Controller\Result\JsonFactory::class)->create();

        if ($this->customerSession->isLoggedIn()) {
            return $resultJson->setData(
                [
                    'errors' => true,
                    'message' => __('Customer is already registered')
                ]
            );
        }
        $orderId = $this->checkoutSession->getLastOrderId();
        if (!$orderId) {
            return $resultJson->setData(
                [
                    'errors' => true,
                    'message' => __('Your session has expired')
                ]
            );
        }
        try {
            $this->orderCustomerService->create($orderId);
            return $resultJson->setData(
                [
                    'errors' => false,
                    'message' => __('A letter with further instructions will be sent to your email.')
                ]
            );
        } catch (\Exception $e) {
            $this->messageManager->addException($e, $e->getMessage());
            throw $e;
        }
    }
}

thanks in advance

marked as duplicate by Piyush, Manoj Deswal, Siarhey Uchukhlebau, Jai, Manashvi Birla Nov 30 '17 at 6:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

app/code/Vendorname/ModuleName/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Controller\Account\Create" type="<Vendorname>\<ModuleName>\Controller\Account\Create" />
</config>

Vendorname\\Controller\Account\Create.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace <Vendorname>\<ModuleName>\Controller\Account;

use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\NoSuchEntityException;

class Create extends \Magento\Checkout\Controller\Account
{
    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $checkoutSession;

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;

    /**
     * @var \Magento\Sales\Api\OrderCustomerManagementInterface
     */
    protected $orderCustomerService;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Checkout\Model\Session $checkoutSession
     * @param \Magento\Customer\Model\Session $customerSession
     * @param \Magento\Sales\Api\OrderCustomerManagementInterface $orderCustomerService
     * @codeCoverageIgnore
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Sales\Api\OrderCustomerManagementInterface $orderCustomerService
    ) {
        $this->checkoutSession = $checkoutSession;
        $this->customerSession = $customerSession;
        $this->orderCustomerService = $orderCustomerService;
        parent::__construct($context, $checkoutSession, $customerSession, $orderCustomerService);
    }

    /**
     * Execute request
     *
     * @throws AlreadyExistsException
     * @throws NoSuchEntityException
     * @throws \Exception
     * @return \Magento\Framework\Controller\Result\Json
     */
    public function execute()
    {
        /*Custom Code Here */

        /** @var \Magento\Framework\Controller\Result\Json $resultJson */
        $resultJson = $this->_objectManager->get(\Magento\Framework\Controller\Result\JsonFactory::class)->create();

        if ($this->customerSession->isLoggedIn()) {
            return $resultJson->setData(
                [
                    'errors' => true,
                    'message' => __('Customer is already registered')
                ]
            );
        }
        $orderId = $this->checkoutSession->getLastOrderId();
        if (!$orderId) {
            return $resultJson->setData(
                [
                    'errors' => true,
                    'message' => __('Your session has expired')
                ]
            );
        }
        try {
            $this->orderCustomerService->create($orderId);
            return $resultJson->setData(
                [
                    'errors' => false,
                    'message' => __('A letter with further instructions will be sent to your email.')
                ]
            );
        } catch (\Exception $e) {
            $this->messageManager->addException($e, $e->getMessage());
            throw $e;
        }
    }
}
  • But it is giving me the error, it is unable to override vendor file @Ronak Chauhan – Learing_Coder Nov 30 '17 at 5:28
  • What is the error you are getting? – Ronak Chauhan Nov 30 '17 at 5:29
  • Error getting in the report @Ronka Chauhan – Learing_Coder Nov 30 '17 at 5:29
  • post error here – Ronak Chauhan Nov 30 '17 at 5:30
  • a:4:{i:0;s:370:"Recoverable Error: Argument 2 passed to Magento\Checkout\Controller\Account\Create::__construct() must be an instance of Magento\Checkout\Model\Session, none given, called in /var/www/default217/app/code/Silk/Helloworld/Controller/Account/Create.php on line 44 and defined in /var/www/default217/vendor/magento/module-checkout/Controller/Account/Create.php on line 37";i:1;s:6129:"#0 /var/www/default217/vendor/magento/module-checkout/Controller/Account/Create.php(37): Magento\Framework\App\ErrorHandler->handler(4096, 'Argument 2 pass...', '/var/www/defaul...', 37) @Ronak Cahuhan – Learing_Coder Nov 30 '17 at 5:47
0

Your di.xml would read as follows:

    <?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <preference for="Magento\Checkout\Controller\Account\Create" type="Companyname\Modulename\Controller\Account\Create" />

</config>

You would override the controller in the file at:

Companyname\Modulename\Controller\Account\Create

I wrote a module overwriting the newsletter subscriber controller to add a form key which you can download and view at http://www.ecommercegorilla.com/magento-2-fake-subscriber-spam-sign-ups-newsletter-security/

You will be able to see how to inject into a controller with di.xml and override a frontend template with a layout update.

Not the answer you're looking for? Browse other questions tagged or ask your own question.