1

I am trying to override CreatePost.php in local but its not working properly. Below i am pasting the code please check once.

I am trying to override the code in getSuccessMessage function

C:\xampp\htdocs\magedemo\app\code\Vendor\Navdata\Controller\Account\CreatePost.php

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

    use Magento\Customer\Model\Account\Redirect as AccountRedirect;
    use Magento\Customer\Api\Data\AddressInterface;
    use Magento\Framework\Api\DataObjectHelper;
    use Magento\Framework\App\Action\Context;
    use Magento\Customer\Model\Session;
    use Magento\Framework\App\Config\ScopeConfigInterface;
    use Magento\Framework\App\ObjectManager;
    use Magento\Framework\Exception\LocalizedException;
    use Magento\Store\Model\StoreManagerInterface;
    use Magento\Customer\Api\AccountManagementInterface;
    use Magento\Customer\Helper\Address;
    use Magento\Framework\UrlFactory;
    use Magento\Customer\Model\Metadata\FormFactory;
    use Magento\Newsletter\Model\SubscriberFactory;
    use Magento\Customer\Api\Data\RegionInterfaceFactory;
    use Magento\Customer\Api\Data\AddressInterfaceFactory;
    use Magento\Customer\Api\Data\CustomerInterfaceFactory;
    use Magento\Customer\Model\Url as CustomerUrl;
    use Magento\Customer\Model\Registration;
    use Magento\Framework\Escaper;
    use Magento\Customer\Model\CustomerExtractor;
    use Magento\Framework\Exception\StateException;
    use Magento\Framework\Exception\InputException;
    use Magento\Framework\Data\Form\FormKey\Validator;

    /**
     * @SuppressWarnings(PHPMD.TooManyFields)
     * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
     */
    class CreatePost extends \Magento\Customer\Controller\Account\CreatPost
    {

        protected $accountManagement;

        protected $addressHelper;

        protected $formFactory;

        protected $subscriberFactory;

        protected $regionDataFactory;

        protected $addressDataFactory;

        protected $registration;

        protected $customerDataFactory;

        protected $customerUrl;

        protected $escaper;

        protected $customerExtractor;

        protected $urlModel;

        protected $dataObjectHelper;

        protected $session;

        private $accountRedirect;

        private $cookieMetadataFactory;

        private $cookieMetadataManager;

        private $formKeyValidator;

        public function __construct(
            Context $context,
            Session $customerSession,
            ScopeConfigInterface $scopeConfig,
            StoreManagerInterface $storeManager,
            AccountManagementInterface $accountManagement,
            Address $addressHelper,
            UrlFactory $urlFactory,
            FormFactory $formFactory,
            SubscriberFactory $subscriberFactory,
            RegionInterfaceFactory $regionDataFactory,
            AddressInterfaceFactory $addressDataFactory,
            CustomerInterfaceFactory $customerDataFactory,
            CustomerUrl $customerUrl,
            Registration $registration,
            Escaper $escaper,
            CustomerExtractor $customerExtractor,
            DataObjectHelper $dataObjectHelper,
            AccountRedirect $accountRedirect,
            Validator $formKeyValidator = null
        ) {
            $this->session = $customerSession;
            $this->scopeConfig = $scopeConfig;
            $this->storeManager = $storeManager;
            $this->accountManagement = $accountManagement;
            $this->addressHelper = $addressHelper;
            $this->formFactory = $formFactory;
            $this->subscriberFactory = $subscriberFactory;
            $this->regionDataFactory = $regionDataFactory;
            $this->addressDataFactory = $addressDataFactory;
            $this->customerDataFactory = $customerDataFactory;
            $this->customerUrl = $customerUrl;
            $this->registration = $registration;
            $this->escaper = $escaper;
            $this->customerExtractor = $customerExtractor;
            $this->urlModel = $urlFactory->create();
            $this->dataObjectHelper = $dataObjectHelper;
            $this->accountRedirect = $accountRedirect;
            $this->formKeyValidator = $formKeyValidator ?: ObjectManager::getInstance()->get(Validator::class);
            parent::__construct($customerSession,$scopeConfig,$storeManager,$accountManagement,$addressHelper,$formFactory,$subscriberFactory,$regionDataFactory,$addressDataFactory,$customerDataFactory,$customerUrl,$registration,$escaper,$customerExtractor,$dataObjectHelper,$accountRedirect,$context);
        }

        /**
         * Retrieve cookie manager
         *
         * @deprecated 100.1.0
         * @return \Magento\Framework\Stdlib\Cookie\PhpCookieManager
         */
        private function getCookieManager()
        {
            if (!$this->cookieMetadataManager) {
                $this->cookieMetadataManager = ObjectManager::getInstance()->get(
                    \Magento\Framework\Stdlib\Cookie\PhpCookieManager::class
                );
            }
            return $this->cookieMetadataManager;
        }

        /**
         * Retrieve cookie metadata factory
         *
         * @deprecated 100.1.0
         * @return \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
         */
        private function getCookieMetadataFactory()
        {
            if (!$this->cookieMetadataFactory) {
                $this->cookieMetadataFactory = ObjectManager::getInstance()->get(
                    \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
                );
            }
            return $this->cookieMetadataFactory;
        }

        /**
         * Add address to customer during create account
         *
         * @return AddressInterface|null
         */
        protected function extractAddress()
        {
            if (!$this->getRequest()->getPost('create_address')) {
                return null;
            }

            $addressForm = $this->formFactory->create('customer_address', 'customer_register_address');
            $allowedAttributes = $addressForm->getAllowedAttributes();

            $addressData = [];

            $regionDataObject = $this->regionDataFactory->create();
            foreach ($allowedAttributes as $attribute) {
                $attributeCode = $attribute->getAttributeCode();
                $value = $this->getRequest()->getParam($attributeCode);
                if ($value === null) {
                    continue;
                }
                switch ($attributeCode) {
                    case 'region_id':
                        $regionDataObject->setRegionId($value);
                        break;
                    case 'region':
                        $regionDataObject->setRegion($value);
                        break;
                    default:
                        $addressData[$attributeCode] = $value;
                }
            }
            $addressDataObject = $this->addressDataFactory->create();
            $this->dataObjectHelper->populateWithArray(
                $addressDataObject,
                $addressData,
                \Magento\Customer\Api\Data\AddressInterface::class
            );
            $addressDataObject->setRegion($regionDataObject);

            $addressDataObject->setIsDefaultBilling(
                $this->getRequest()->getParam('default_billing', false)
            )->setIsDefaultShipping(
                $this->getRequest()->getParam('default_shipping', false)
            );
            return $addressDataObject;
        }

        /**
         * Create customer account action
         *
         * @return void
         * @SuppressWarnings(PHPMD.CyclomaticComplexity)
         * @SuppressWarnings(PHPMD.NPathComplexity)
         */
        public function execute()
        {
            /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
            $resultRedirect = $this->resultRedirectFactory->create();
            if ($this->session->isLoggedIn() || !$this->registration->isAllowed()) {
                $resultRedirect->setPath('*/*/');
                return $resultRedirect;
            }

            if (!$this->getRequest()->isPost() || !$this->formKeyValidator->validate($this->getRequest())) {
                $url = $this->urlModel->getUrl('*/*/create', ['_secure' => true]);
                $resultRedirect->setUrl($this->_redirect->error($url));
                return $resultRedirect;
            }

            $this->session->regenerateId();

            try {
                $address = $this->extractAddress();
                $addresses = $address === null ? [] : [$address];

                $customer = $this->customerExtractor->extract('customer_account_create', $this->_request);
                $customer->setAddresses($addresses);

                $password = $this->getRequest()->getParam('password');
                $confirmation = $this->getRequest()->getParam('password_confirmation');
                $redirectUrl = $this->session->getBeforeAuthUrl();

                $this->checkPasswordConfirmation($password, $confirmation);

                $customer = $this->accountManagement
                    ->createAccount($customer, $password, $redirectUrl);

                if ($this->getRequest()->getParam('is_subscribed', false)) {
                    $this->subscriberFactory->create()->subscribeCustomerById($customer->getId());
                }

                $this->_eventManager->dispatch(
                    'customer_register_success',
                    ['account_controller' => $this, 'customer' => $customer]
                );

                $confirmationStatus = $this->accountManagement->getConfirmationStatus($customer->getId());
                if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) {
                    $email = $this->customerUrl->getEmailConfirmationUrl($customer->getEmail());
                    // @codingStandardsIgnoreStart
                    $this->messageManager->addSuccess(
                        __(
                            'You must confirm your account. Please check your email for the confirmation link or <a href="%1">click here</a> for a new link.',
                            $email
                        )
                    );
                    // @codingStandardsIgnoreEnd
                    $url = $this->urlModel->getUrl('*/*/index', ['_secure' => true]);
                    $resultRedirect->setUrl($this->_redirect->success($url));
                } else {
                    $this->session->setCustomerDataAsLoggedIn($customer);
                    $this->messageManager->addSuccess($this->getSuccessMessage());
                    $requestedRedirect = $this->accountRedirect->getRedirectCookie();
                    if (!$this->scopeConfig->getValue('customer/startup/redirect_dashboard') && $requestedRedirect) {
                        $resultRedirect->setUrl($this->_redirect->success($requestedRedirect));
                        $this->accountRedirect->clearRedirectCookie();
                        return $resultRedirect;
                    }
                    $resultRedirect = $this->accountRedirect->getRedirect();
                }
                if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
                    $metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
                    $metadata->setPath('/');
                    $this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
                }

                return $resultRedirect;
            } catch (StateException $e) {
                $url = $this->urlModel->getUrl('customer/account/forgotpassword');
                // @codingStandardsIgnoreStart
                $message = __(
                    'There is already an account with this email address. If you are sure that it is your email address, <a href="%1">click here</a> to get your password and access your account.',
                    $url
                );
                // @codingStandardsIgnoreEnd
                $this->messageManager->addError($message);
            } catch (InputException $e) {
                $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
                foreach ($e->getErrors() as $error) {
                    $this->messageManager->addError($this->escaper->escapeHtml($error->getMessage()));
                }
            } catch (LocalizedException $e) {
                $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
            } catch (\Exception $e) {
                $this->messageManager->addException($e, __('We can\'t save the customer.'));
            }

            $this->session->setCustomerFormData($this->getRequest()->getPostValue());
            $defaultUrl = $this->urlModel->getUrl('*/*/create', ['_secure' => true]);
            $resultRedirect->setUrl($this->_redirect->error($defaultUrl));
            return $resultRedirect;
        }

        /**
         * Make sure that password and password confirmation matched
         *
         * @param string $password
         * @param string $confirmation
         * @return void
         * @throws InputException
         */
        protected function checkPasswordConfirmation($password, $confirmation)
        {
            if ($password != $confirmation) {
                throw new InputException(__('Please make sure your passwords match.'));
            }
        }

        /**
         * Retrieve success message
         *
         * @return string
         */
        protected function getSuccessMessage()
        {
            if ($this->addressHelper->isVatValidationEnabled()) {
                if ($this->addressHelper->getTaxCalculationAddressType() == Address::TYPE_SHIPPING) {
                    // @codingStandardsIgnoreStart
                    $message = __(
                        'If you are a registered VAT customer, please <a href="%1">click here</a> to enter your shipping address for proper VAT calculation.',
                        $this->urlModel->getUrl('customer/address/edit')
                    );
                    // @codingStandardsIgnoreEnd
                } else {
                    // @codingStandardsIgnoreStart
                    $message = __(
                        'If you are a registered VAT customer, please <a href="%1">click here</a> to enter your billing address for proper VAT calculation.',
                        $this->urlModel->getUrl('customer/address/edit')
                    );
                    // @codingStandardsIgnoreEnd
                }
            } else {
             //   $message = __('Thank you for registering with %1.', $this->storeManager->getStore()->getFrontendName());
                $message = __('Thank you for registering %1.');
            }
            return $message;
        }
    }

etc/di.xml

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">


     <preference for="Magento\Customer\Controller\Account\CreatPost" type="Vendor\Navdata\Controller\Account\CreatPost" />
    </config> 
  • Basically you want to change success message TEXT only ? – anonymous Dec 18 '18 at 6:31
  • yes i want to change text only from above way but i think we dont want to change this text directley from i18n/en_US.csv – Hitesh Balpande Dec 18 '18 at 6:33
  • Why not using translation ? Is there any reason ?! – anonymous Dec 18 '18 at 6:34
  • is it work using translation?but yesterday i tried with in custom theme Magento_Customer/i18n/en_US.csv – Hitesh Balpande Dec 18 '18 at 6:36
  • Yes Definitely It will work. – anonymous Dec 18 '18 at 6:38
2

If you only need to translate success message Than There no need to override whole controller.

You can create CSV file and add your desired translation.

app/design/frontend/Vendor/ThemeName/i18n/en_US.csv

Than add as below.

"Thank you for registering %1.","Your custom text"

Deploy static content and clear cache.

  • is setup:di:compile required – Hitesh Balpande Dec 18 '18 at 7:32
  • i tried the path app/design/frontend/Vendor/ThemeName/i18n/en_US.csv but even it is not worked with language code. – Hitesh Balpande Dec 18 '18 at 7:43

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.