2

I had a custom Module with custom form, which was working fine until recently. Most likely it is throwing error after upgrade from 2.2.2 to 2.2.6.

and the line representing the error is like:

$templateOptions = array('area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->storeManager->getStore()->getId());

and the error shown is like this:

Notice: Undefined property: MyCompany\Mymodule\Controller\Index\Form\Interceptor::$storeManager in /home/www/public_html/app/code/MyCompany/Mymodule/Controller/Index/Form.php on line 113

is it related to upgrade or could it be something else.

The whole code of the class looks like this:

namespace MyCompany\Mymodule\Controller\Index;
use Magento\Framework\Controller\ResultFactory;

class Form extends \Magento\Framework\App\Action\Action
{
/**
 * Contact action
 *
 * @return void
 */
/**
 * @var \Magento\Framework\Mail\Template\TransportBuilder
 */

 /**
* @var Google reCaptcha Options
*/
private static $_siteVerifyUrl = "https://www.google.com/recaptcha/api/siteverify?";
private $_secret;
private static $_version = "php_7.0";
/**
* Save Form Data
*
* @return array
*/
protected $_transportBuilder;
protected $_storeManager;


public function __construct(
    \Magento\Framework\App\Action\Context $context,

   // \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
    \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
    \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
     \Magento\Store\Model\StoreManagerInterface $storeManager
) {
//  $this->fileUploaderFactory = $fileUploaderFactory;

    $this->_transportBuilder = $transportBuilder;
    $this->inlineTranslation = $inlineTranslation;
    $this->_storeManager = $storeManager;
    $this->scopeConfig = $scopeConfig;
    parent::__construct($context);

}

public function execute()
{

    $post = (array) $this->getRequest()->getPost();

    if (!empty($post)) {
        // Retrieve your form data
        $name   = $post['name'];
        $email  = $post['email'];
        $mobile  = $post['mobile'];
        $sku  = $post['sku'];
        $width  = $post['width'];
        $height  = $post['height'];
        $shipto  = $post['ship_to'];
        $remark = $post['remark'];
        $captcha = $this->getRequest()->getParam('g-recaptcha-response');

        $secret = "6LcjmUsUAAAAAM3ZNm02YjdW7tHqJetUqS2Q4J8f"; //Replace with your secret key
        $response = null;
        $path = self::$_siteVerifyUrl;
        $dataC = array (
        'secret' => $secret,
        'remoteip' => $_SERVER["REMOTE_ADDR"],
        'v' => self::$_version,
        'response' => $captcha
        );
        $req = "";
        foreach ($dataC as $key => $value) {
             $req .= $key . '=' . urlencode(stripslashes($value)) . '&';
        }
        // Cut the last '&'
        $req = substr($req, 0, strlen($req)-1);
        $response = file_get_contents($path . $req);
       //var_dump($response);exit;
        $answers = json_decode($response, true);
        //var_dump($answers);exit;
        if(trim($answers ['success']) == true) {


        $MyCompany = $this->_objectManager->create('MyCompany\Mymodule\Model\MyCompany');

        $MyCompany->setName($name);
        $MyCompany->setMobile($mobile);
        $MyCompany->setEmail($email);
        $MyCompany->setSku($sku);
        $MyCompany->setWidth($width);
        $MyCompany->setHeight($height);
        $MyCompany->setShip_to($shipto);
        $MyCompany->setRemarks($remark);
        $MyCompany->save();

        //$this->getResponse()->setBody('Your Request is Submitted Successfully !');

        // Doing-something with...

        // Display the succes form validation message
        $this->messageManager->addSuccessMessage('Your Request is Submitted Successfully !');
        }else{
     $this->messageManager->addError(__('Captcha Validation Failed. Please retry')
                             );

}


         $templateOptions = array('area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->_storeManager->getStore()->getId());
      //  var_dump($templateOptions);exit;

        $templateVars = array(
               'store' => $this->_storeManager->getStore(),
                'customer_name' => $name,
                'mobile' => $mobile,
                'email' => $email,
                'width' => $width,
                'height' => $height,
                'shipto' => $shipto,
                'remark'   => $remark
            );
        $from = array('email' => "[email protected]", 'name' => 'Indian Art Zone');
        $this->inlineTranslation->suspend();
        $to = array($email,'[email protected]');
        $transport = $this->_transportBuilder->setTemplateIdentifier('MyCompany_email_template')
            ->setTemplateOptions($templateOptions)
            ->setTemplateVars($templateVars)
            ->setFrom($from)
            ->addTo($to)
            ->getTransport();
        $transport->sendMessage();
        $this->inlineTranslation->resume();

        // Redirect to your form page (or anywhere you want...)
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $resultRedirect->setUrl('/MyCompany/index/form');

        return $resultRedirect;
    }
    // Render the page
    $this->_view->loadLayout();
    $this->_view->renderLayout();
}

}

3
  • 1
    Show me please class MyCompany/Mymodule/Controller/Index/Form.php Commented Jul 6, 2019 at 21:54
  • please defined store manager in controller. Commented Jul 7, 2019 at 2:25
  • did you find your solution?
    – Mohit Rane
    Commented Jul 8, 2019 at 6:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.