Tuesday, April 26, 2022

redirect custom path with params magento

 Code

<?php
namespace Icube\Customer\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\LocalizedException;

class ValidateTelephone implements ObserverInterface
{
    protected $_request;
    protected $_customerManagement;
    protected $_messageManager;
    protected $_responseFactory;
    protected $_url;
    protected $_resulFactory;

    public function __construct(
        \Magento\Framework\App\RequestInterface $request,
        \Icube\Customer\Model\CustomerManagement $customerManagement,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\Controller\ResultFactory $resulFactory
    ) {
        $this->_request = $request;
        $this->_customerManagement = $customerManagement;
        $this->_messageManager = $messageManager;
        $this->_responseFactory = $responseFactory;
        $this->_url = $url;
        $this->_resultFactory = $resulFactory;
    }

    /**
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $phone = $this->_request->getParam('telephone');
        if ($this->_customerManagement->isTelephoneAvailable($phone) === false) {
            $this->_messageManager->addError("Your phone number is already registered, please change your phone number.");
            $customerUrl = $this->_url->getUrl('customer/account/create');
            $resultRedirect = $this->_resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setPath($customerUrl, ['params' => $this->_request->getParams()]);
            // or
            //throw new LocalizedException(
            //    __("Your phone number is already registered, please change your phone number.")
            //);
        }
        //  $tmp = "hi" ;
        //  die(json_encode(is_object($tmp) ? get_class_methods($tmp) : $tmp));

    }

}

References

Sunday, April 24, 2022

No Administrators role was found, data fixture needs to be run

 Error Details

In AdminAccount.php line 262:

  No Administrators role was found, data fixture needs to be run


admin:user:create [--admin-user ADMIN-USER] [--admin-password ADMIN-PASSWORD] [--admin-email ADMIN-EMAIL] [--admin-firstname ADMIN-FIRSTNAME] [--admin-lastname ADMIN-LASTNAME] [--magento-init-params MAGENTO-INIT-PARAMS]


Solution

> INSERT INTO authorization_role (role_id, parent_id, tree_level, sort_order, role_type, user_id, user_type, role_name) VALUES (1, 0, 1, 1, 'G', 0, '2', 'Administrators');

References

Wednesday, April 20, 2022

cannot login "An unspecified error occurred"

 Fix

for fix this problem, change the code to be able to print the error

Can you check logs for specific error of Magento? var/log folder or any reports in var/report folder.

Apart from that you can just check by printing error message.

vendor/magento/module-customer/Controller/Account/LoginPost.php and put these lines

change this code

$this->messageManager->addError(
                        __('An unspecified error occurred. Please contact us for assistance.')
                    );

to

$message = $e->getMessage();
$this->messageManager->addError($message);

References

https://community.magento.com/t5/Magento-2-x-Technical-Issues/magento-2-2-0-cannot-login-quot-An-unspecified-error-occurred/td-p/447788

The account sign-in was incorrect or your account is disabled temporarily.

 Error

The account sign-in was incorrect or your account is disabled temporarily. Please wait and try again later

Solution

1. Create new account by command line

php bin/magento admin:user:create --admin-user='magento243' --admin-password='magento243' --admin-email='magento243@gmail.com' --admin-firstname='Firstname' --admin-lastname='Lastname'
2. Now login with the new created username and then change the password of previous user which was causing issue in login

cannot login "An unspecified error occurred"

Fix the Problem

for fix the problem change the code

Can you check logs for specific error of Magento? var/log folder or any reports in var/report folder.

Apart from that you can just check by printing error message.

vendor/magento/module-customer/Controller/Account/LoginPost.php and put these lines

ubah kode ini


$this->messageManager->addError( __('An unspecified error occurred. Please contact us for assistance.') );

menjadi

$message = $e->getMessage(); $this->messageManager->addError($message);


References

https://community.magento.com/t5/Magento-2-x-Technical-Issues/magento-2-2-0-cannot-login-quot-An-unspecified-error-occurred/td-p/447788