Newsletter Checkbox on contact us page in magento 2Fatal error: Uncaught TypeErrormagento 2 breadcrumbs on...

Is Draco portrayed in the canon books as good-looking?

Can chords be played on the flute?

Why do members of Congress in committee hearings ask witnesses the same question multiple times?

Why do neural networks need so many training examples to perform?

Using AWS Fargate as web server

How to avoid being sexist when trying to employ someone to function in a very sexist environment?

What do the pedals on grand pianos do?

What is the class of function that when called repeatedly, has the same effect as calling once?

What's the rationale behind the objections to these measures against human trafficking?

Why is my solution for the partial pressures of two different gases incorrect?

Which aircraft had such a luxurious-looking navigator's station?

awk unexpectedly removes dot from string

Is my plan for fixing my water heater leak bad?

If I delete my router's history can my ISP still provide it to my parents?

Quenching swords in dragon blood; why?

How to define a macro with multiple optional parameters?

Does "sickness" have the same meaning as "vomitus"?

Where is this triangular-shaped space station from?

How to properly claim credit for peer review?

Could quantum mechanics be necessary to analyze some biology scenarios?

What can I substitute for soda pop in a sweet pork recipe?

How do we edit a novel that's written by several people?

Can the Assuming function be used with ContourPlot or DensityPlot?

How to satisfy a player character's curiosity about another player character?



Newsletter Checkbox on contact us page in magento 2


Fatal error: Uncaught TypeErrormagento 2 breadcrumbs on contact pageSign Up For Newsletter checkbox in guest checkout?magento 2 captcha not rendering if I override layout xmlInserting Newsletter into CMS page. Magento 2Product page Review Section from tabs to Bottom of pageContact Us PageMagento 2 - Add newsletter checkbox on checkout order summary.In Contact form I have a checkbox for subscription to newsletterAdd to cart button freezes and nothing is put in basketMagento 2 Newsletter Subscribe checkbox after email in thirdparty module













3















How to add newsletter checkbox in contact us form in magento 2.1.6 same like customer create account page.



i tried to use same block



 <div class="field choice newsletter">
<input type="checkbox" name="is_subscribed" title="<?php /* @escapeNotVerified */ echo __('Sign Up for Newsletter') ?>" value="1" id="is_subscribed"<?php if ($block->getFormData()->getIsSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox">
<label for="is_subscribed" class="label"><span><?php /* @escapeNotVerified */ echo __('Sign Up for Newsletter') ?></span></label>
</div>


but its give error:




Fatal error: Call to a member function getIsSubscribed() on null in /home/schoolin/public_html/demo/app/design/frontend/Sm/furnicom/Magento_Contact/templates/form.phtml on line 72




How to fix this ???










share|improve this question





























    3















    How to add newsletter checkbox in contact us form in magento 2.1.6 same like customer create account page.



    i tried to use same block



     <div class="field choice newsletter">
    <input type="checkbox" name="is_subscribed" title="<?php /* @escapeNotVerified */ echo __('Sign Up for Newsletter') ?>" value="1" id="is_subscribed"<?php if ($block->getFormData()->getIsSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox">
    <label for="is_subscribed" class="label"><span><?php /* @escapeNotVerified */ echo __('Sign Up for Newsletter') ?></span></label>
    </div>


    but its give error:




    Fatal error: Call to a member function getIsSubscribed() on null in /home/schoolin/public_html/demo/app/design/frontend/Sm/furnicom/Magento_Contact/templates/form.phtml on line 72




    How to fix this ???










    share|improve this question



























      3












      3








      3


      2






      How to add newsletter checkbox in contact us form in magento 2.1.6 same like customer create account page.



      i tried to use same block



       <div class="field choice newsletter">
      <input type="checkbox" name="is_subscribed" title="<?php /* @escapeNotVerified */ echo __('Sign Up for Newsletter') ?>" value="1" id="is_subscribed"<?php if ($block->getFormData()->getIsSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox">
      <label for="is_subscribed" class="label"><span><?php /* @escapeNotVerified */ echo __('Sign Up for Newsletter') ?></span></label>
      </div>


      but its give error:




      Fatal error: Call to a member function getIsSubscribed() on null in /home/schoolin/public_html/demo/app/design/frontend/Sm/furnicom/Magento_Contact/templates/form.phtml on line 72




      How to fix this ???










      share|improve this question
















      How to add newsletter checkbox in contact us form in magento 2.1.6 same like customer create account page.



      i tried to use same block



       <div class="field choice newsletter">
      <input type="checkbox" name="is_subscribed" title="<?php /* @escapeNotVerified */ echo __('Sign Up for Newsletter') ?>" value="1" id="is_subscribed"<?php if ($block->getFormData()->getIsSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox">
      <label for="is_subscribed" class="label"><span><?php /* @escapeNotVerified */ echo __('Sign Up for Newsletter') ?></span></label>
      </div>


      but its give error:




      Fatal error: Call to a member function getIsSubscribed() on null in /home/schoolin/public_html/demo/app/design/frontend/Sm/furnicom/Magento_Contact/templates/form.phtml on line 72




      How to fix this ???







      magento2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 3 mins ago









      Manashvi Birla

      6,33741841




      6,33741841










      asked Jul 10 '17 at 4:09









      samsam

      135214




      135214






















          1 Answer
          1






          active

          oldest

          votes


















          5














          You will need to create a new module with a new helper for it to check if customer is subscribed in this situation.




          app/code/Stackoverflow/Contact/Helper/Data.php




          <?php
          /**
          * Copyright © 2017 Toan Nguyen. All rights reserved.
          * See COPYING.txt for license details.
          */

          namespace StackoverflowContactHelper;

          use MagentoCustomerApiDataCustomerInterface;
          use MagentoCustomerModelSession;
          use MagentoFrameworkAppHelperAbstractHelper;
          use MagentoFrameworkAppHelperContext;
          use MagentoNewsletterModelSubscriber;

          /**
          * Contact base helper
          */
          class Data extends AbstractHelper
          {
          /**
          * @var MagentoCustomerModelSession
          */
          protected $customerSession;

          /**
          * @var MagentoNewsletterModelSubscriber
          */
          protected $subscriber;

          /**
          * @param Context $context
          * @param Session $customerSession
          * @param Subscriber $subscriber
          */
          public function __construct(
          Context $context,
          Session $customerSession,
          Subscriber $subscriber
          ) {
          $this->customerSession = $customerSession;
          $this->subscriber = $subscriber;
          parent::__construct($context);
          }

          /**
          * Check if customer subsribed
          *
          * @return bool
          */
          public function isSubscribed()
          {
          if (!$this->customerSession->isLoggedIn()) {
          return false;
          }

          /**
          * @var CustomerInterface $customer
          */
          $customer = $this->customerSession->getCustomer();
          $checkSubscriber = $this->subscriber->loadByEmail($customer->getEmail());

          return $checkSubscriber->isSubscribed();
          }
          }



          app/design/frontend/YOURVENDOR/YOURTHEME/Magento_Contact/templates/form.phtml




              <div class="field choice newsletter">
          <input type="checkbox" name="is_subscribed" title="<?php /* @escapeNotVerified */ echo __('Sign Up for Newsletter') ?>" value="1" id="is_subscribed"<?php if ($this->helper('StackoverflowContactHelperData')->isSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox">
          <label for="is_subscribed" class="label"><span><?php /* @escapeNotVerified */ echo __('Sign Up for Newsletter') ?></span></label>
          </div>



          app/code/Stackoverflow/Contact/Plugin/Controller/Index/Post.php




          <?php
          /**
          * Copyright © 2017 Toan Nguyen. All rights reserved.
          * See COPYING.txt for license details.
          */

          namespace StackoverflowContactPluginControllerIndex;

          use MagentoContactControllerIndexPost as Subject;
          use MagentoCustomerModelSession;
          use MagentoNewsletterModelSubscriberFactory;

          class Post
          {
          /**
          * @var SubscriberFactory
          */
          protected $subscriberFactory;

          /**
          * @var Session
          */
          protected $customerSession;

          /**
          * Post constructor.
          *
          * @param SubscriberFactory $subscriberFactory
          * @param Session $customerSession
          */
          public function __construct(
          SubscriberFactory $subscriberFactory,
          Session $customerSession
          ) {
          $this->subscriberFactory = $subscriberFactory;
          $this->customerSession = $customerSession;
          }


          /**
          * Override execute() to subscribe customer before send email
          *
          * @param Subject $subject
          * @param Closure $proceed
          *
          * @return mixed
          */
          public function aroundExecute(Subject $subject, Closure $proceed)
          {
          if ($subject->getRequest()->isPost()) {
          $post = $subject->getRequest()->getPostValue();
          if ((boolean)$subject->getRequest()->getParam('is_subscribed') === true) {
          $this->subscriberFactory->create()->subscribe($post['email']);
          }
          }
          return $proceed();
          }
          }



          app/code/Stackoverflow/Contact/etc/frontend/di.xml









          <type name="MagentoContactControllerIndexPost">
          <plugin sortOrder="1" name="stackoverflowContactPost"
          type="StackoverflowContactPluginControllerIndexPost"/>
          </type>







          share|improve this answer


























          • I tried this, but after massage sent, email is is not added in newsletter subscriber and even customer not getting mail for subscribe

            – sam
            Jul 10 '17 at 4:40











          • @sam I misunderstood your question, I thought you want to get the status is_subscribed in contact form. You will also need to change the contact form post controller to subscribe the customer manually.

            – Toan Nguyen
            Jul 10 '17 at 5:30













          • what i have to add there, in controller file for subscribe?

            – sam
            Jul 10 '17 at 5:35











          • @sam Please check my updated answer.

            – Toan Nguyen
            Jul 10 '17 at 5:40











          • getting this error Fatal error: Call to undefined method SfiiBulkbuyPluginControllerIndexPost::getRequest() in /home/schoolin/public_html/demo/app/code/Sfii/Bulkbuy/Plugin/Controller/Index/Post.php on line 51

            – sam
            Jul 10 '17 at 9:41











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "479"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f182810%2fnewsletter-checkbox-on-contact-us-page-in-magento-2%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          You will need to create a new module with a new helper for it to check if customer is subscribed in this situation.




          app/code/Stackoverflow/Contact/Helper/Data.php




          <?php
          /**
          * Copyright © 2017 Toan Nguyen. All rights reserved.
          * See COPYING.txt for license details.
          */

          namespace StackoverflowContactHelper;

          use MagentoCustomerApiDataCustomerInterface;
          use MagentoCustomerModelSession;
          use MagentoFrameworkAppHelperAbstractHelper;
          use MagentoFrameworkAppHelperContext;
          use MagentoNewsletterModelSubscriber;

          /**
          * Contact base helper
          */
          class Data extends AbstractHelper
          {
          /**
          * @var MagentoCustomerModelSession
          */
          protected $customerSession;

          /**
          * @var MagentoNewsletterModelSubscriber
          */
          protected $subscriber;

          /**
          * @param Context $context
          * @param Session $customerSession
          * @param Subscriber $subscriber
          */
          public function __construct(
          Context $context,
          Session $customerSession,
          Subscriber $subscriber
          ) {
          $this->customerSession = $customerSession;
          $this->subscriber = $subscriber;
          parent::__construct($context);
          }

          /**
          * Check if customer subsribed
          *
          * @return bool
          */
          public function isSubscribed()
          {
          if (!$this->customerSession->isLoggedIn()) {
          return false;
          }

          /**
          * @var CustomerInterface $customer
          */
          $customer = $this->customerSession->getCustomer();
          $checkSubscriber = $this->subscriber->loadByEmail($customer->getEmail());

          return $checkSubscriber->isSubscribed();
          }
          }



          app/design/frontend/YOURVENDOR/YOURTHEME/Magento_Contact/templates/form.phtml




              <div class="field choice newsletter">
          <input type="checkbox" name="is_subscribed" title="<?php /* @escapeNotVerified */ echo __('Sign Up for Newsletter') ?>" value="1" id="is_subscribed"<?php if ($this->helper('StackoverflowContactHelperData')->isSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox">
          <label for="is_subscribed" class="label"><span><?php /* @escapeNotVerified */ echo __('Sign Up for Newsletter') ?></span></label>
          </div>



          app/code/Stackoverflow/Contact/Plugin/Controller/Index/Post.php




          <?php
          /**
          * Copyright © 2017 Toan Nguyen. All rights reserved.
          * See COPYING.txt for license details.
          */

          namespace StackoverflowContactPluginControllerIndex;

          use MagentoContactControllerIndexPost as Subject;
          use MagentoCustomerModelSession;
          use MagentoNewsletterModelSubscriberFactory;

          class Post
          {
          /**
          * @var SubscriberFactory
          */
          protected $subscriberFactory;

          /**
          * @var Session
          */
          protected $customerSession;

          /**
          * Post constructor.
          *
          * @param SubscriberFactory $subscriberFactory
          * @param Session $customerSession
          */
          public function __construct(
          SubscriberFactory $subscriberFactory,
          Session $customerSession
          ) {
          $this->subscriberFactory = $subscriberFactory;
          $this->customerSession = $customerSession;
          }


          /**
          * Override execute() to subscribe customer before send email
          *
          * @param Subject $subject
          * @param Closure $proceed
          *
          * @return mixed
          */
          public function aroundExecute(Subject $subject, Closure $proceed)
          {
          if ($subject->getRequest()->isPost()) {
          $post = $subject->getRequest()->getPostValue();
          if ((boolean)$subject->getRequest()->getParam('is_subscribed') === true) {
          $this->subscriberFactory->create()->subscribe($post['email']);
          }
          }
          return $proceed();
          }
          }



          app/code/Stackoverflow/Contact/etc/frontend/di.xml









          <type name="MagentoContactControllerIndexPost">
          <plugin sortOrder="1" name="stackoverflowContactPost"
          type="StackoverflowContactPluginControllerIndexPost"/>
          </type>







          share|improve this answer


























          • I tried this, but after massage sent, email is is not added in newsletter subscriber and even customer not getting mail for subscribe

            – sam
            Jul 10 '17 at 4:40











          • @sam I misunderstood your question, I thought you want to get the status is_subscribed in contact form. You will also need to change the contact form post controller to subscribe the customer manually.

            – Toan Nguyen
            Jul 10 '17 at 5:30













          • what i have to add there, in controller file for subscribe?

            – sam
            Jul 10 '17 at 5:35











          • @sam Please check my updated answer.

            – Toan Nguyen
            Jul 10 '17 at 5:40











          • getting this error Fatal error: Call to undefined method SfiiBulkbuyPluginControllerIndexPost::getRequest() in /home/schoolin/public_html/demo/app/code/Sfii/Bulkbuy/Plugin/Controller/Index/Post.php on line 51

            – sam
            Jul 10 '17 at 9:41
















          5














          You will need to create a new module with a new helper for it to check if customer is subscribed in this situation.




          app/code/Stackoverflow/Contact/Helper/Data.php




          <?php
          /**
          * Copyright © 2017 Toan Nguyen. All rights reserved.
          * See COPYING.txt for license details.
          */

          namespace StackoverflowContactHelper;

          use MagentoCustomerApiDataCustomerInterface;
          use MagentoCustomerModelSession;
          use MagentoFrameworkAppHelperAbstractHelper;
          use MagentoFrameworkAppHelperContext;
          use MagentoNewsletterModelSubscriber;

          /**
          * Contact base helper
          */
          class Data extends AbstractHelper
          {
          /**
          * @var MagentoCustomerModelSession
          */
          protected $customerSession;

          /**
          * @var MagentoNewsletterModelSubscriber
          */
          protected $subscriber;

          /**
          * @param Context $context
          * @param Session $customerSession
          * @param Subscriber $subscriber
          */
          public function __construct(
          Context $context,
          Session $customerSession,
          Subscriber $subscriber
          ) {
          $this->customerSession = $customerSession;
          $this->subscriber = $subscriber;
          parent::__construct($context);
          }

          /**
          * Check if customer subsribed
          *
          * @return bool
          */
          public function isSubscribed()
          {
          if (!$this->customerSession->isLoggedIn()) {
          return false;
          }

          /**
          * @var CustomerInterface $customer
          */
          $customer = $this->customerSession->getCustomer();
          $checkSubscriber = $this->subscriber->loadByEmail($customer->getEmail());

          return $checkSubscriber->isSubscribed();
          }
          }



          app/design/frontend/YOURVENDOR/YOURTHEME/Magento_Contact/templates/form.phtml




              <div class="field choice newsletter">
          <input type="checkbox" name="is_subscribed" title="<?php /* @escapeNotVerified */ echo __('Sign Up for Newsletter') ?>" value="1" id="is_subscribed"<?php if ($this->helper('StackoverflowContactHelperData')->isSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox">
          <label for="is_subscribed" class="label"><span><?php /* @escapeNotVerified */ echo __('Sign Up for Newsletter') ?></span></label>
          </div>



          app/code/Stackoverflow/Contact/Plugin/Controller/Index/Post.php




          <?php
          /**
          * Copyright © 2017 Toan Nguyen. All rights reserved.
          * See COPYING.txt for license details.
          */

          namespace StackoverflowContactPluginControllerIndex;

          use MagentoContactControllerIndexPost as Subject;
          use MagentoCustomerModelSession;
          use MagentoNewsletterModelSubscriberFactory;

          class Post
          {
          /**
          * @var SubscriberFactory
          */
          protected $subscriberFactory;

          /**
          * @var Session
          */
          protected $customerSession;

          /**
          * Post constructor.
          *
          * @param SubscriberFactory $subscriberFactory
          * @param Session $customerSession
          */
          public function __construct(
          SubscriberFactory $subscriberFactory,
          Session $customerSession
          ) {
          $this->subscriberFactory = $subscriberFactory;
          $this->customerSession = $customerSession;
          }


          /**
          * Override execute() to subscribe customer before send email
          *
          * @param Subject $subject
          * @param Closure $proceed
          *
          * @return mixed
          */
          public function aroundExecute(Subject $subject, Closure $proceed)
          {
          if ($subject->getRequest()->isPost()) {
          $post = $subject->getRequest()->getPostValue();
          if ((boolean)$subject->getRequest()->getParam('is_subscribed') === true) {
          $this->subscriberFactory->create()->subscribe($post['email']);
          }
          }
          return $proceed();
          }
          }



          app/code/Stackoverflow/Contact/etc/frontend/di.xml









          <type name="MagentoContactControllerIndexPost">
          <plugin sortOrder="1" name="stackoverflowContactPost"
          type="StackoverflowContactPluginControllerIndexPost"/>
          </type>







          share|improve this answer


























          • I tried this, but after massage sent, email is is not added in newsletter subscriber and even customer not getting mail for subscribe

            – sam
            Jul 10 '17 at 4:40











          • @sam I misunderstood your question, I thought you want to get the status is_subscribed in contact form. You will also need to change the contact form post controller to subscribe the customer manually.

            – Toan Nguyen
            Jul 10 '17 at 5:30













          • what i have to add there, in controller file for subscribe?

            – sam
            Jul 10 '17 at 5:35











          • @sam Please check my updated answer.

            – Toan Nguyen
            Jul 10 '17 at 5:40











          • getting this error Fatal error: Call to undefined method SfiiBulkbuyPluginControllerIndexPost::getRequest() in /home/schoolin/public_html/demo/app/code/Sfii/Bulkbuy/Plugin/Controller/Index/Post.php on line 51

            – sam
            Jul 10 '17 at 9:41














          5












          5








          5







          You will need to create a new module with a new helper for it to check if customer is subscribed in this situation.




          app/code/Stackoverflow/Contact/Helper/Data.php




          <?php
          /**
          * Copyright © 2017 Toan Nguyen. All rights reserved.
          * See COPYING.txt for license details.
          */

          namespace StackoverflowContactHelper;

          use MagentoCustomerApiDataCustomerInterface;
          use MagentoCustomerModelSession;
          use MagentoFrameworkAppHelperAbstractHelper;
          use MagentoFrameworkAppHelperContext;
          use MagentoNewsletterModelSubscriber;

          /**
          * Contact base helper
          */
          class Data extends AbstractHelper
          {
          /**
          * @var MagentoCustomerModelSession
          */
          protected $customerSession;

          /**
          * @var MagentoNewsletterModelSubscriber
          */
          protected $subscriber;

          /**
          * @param Context $context
          * @param Session $customerSession
          * @param Subscriber $subscriber
          */
          public function __construct(
          Context $context,
          Session $customerSession,
          Subscriber $subscriber
          ) {
          $this->customerSession = $customerSession;
          $this->subscriber = $subscriber;
          parent::__construct($context);
          }

          /**
          * Check if customer subsribed
          *
          * @return bool
          */
          public function isSubscribed()
          {
          if (!$this->customerSession->isLoggedIn()) {
          return false;
          }

          /**
          * @var CustomerInterface $customer
          */
          $customer = $this->customerSession->getCustomer();
          $checkSubscriber = $this->subscriber->loadByEmail($customer->getEmail());

          return $checkSubscriber->isSubscribed();
          }
          }



          app/design/frontend/YOURVENDOR/YOURTHEME/Magento_Contact/templates/form.phtml




              <div class="field choice newsletter">
          <input type="checkbox" name="is_subscribed" title="<?php /* @escapeNotVerified */ echo __('Sign Up for Newsletter') ?>" value="1" id="is_subscribed"<?php if ($this->helper('StackoverflowContactHelperData')->isSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox">
          <label for="is_subscribed" class="label"><span><?php /* @escapeNotVerified */ echo __('Sign Up for Newsletter') ?></span></label>
          </div>



          app/code/Stackoverflow/Contact/Plugin/Controller/Index/Post.php




          <?php
          /**
          * Copyright © 2017 Toan Nguyen. All rights reserved.
          * See COPYING.txt for license details.
          */

          namespace StackoverflowContactPluginControllerIndex;

          use MagentoContactControllerIndexPost as Subject;
          use MagentoCustomerModelSession;
          use MagentoNewsletterModelSubscriberFactory;

          class Post
          {
          /**
          * @var SubscriberFactory
          */
          protected $subscriberFactory;

          /**
          * @var Session
          */
          protected $customerSession;

          /**
          * Post constructor.
          *
          * @param SubscriberFactory $subscriberFactory
          * @param Session $customerSession
          */
          public function __construct(
          SubscriberFactory $subscriberFactory,
          Session $customerSession
          ) {
          $this->subscriberFactory = $subscriberFactory;
          $this->customerSession = $customerSession;
          }


          /**
          * Override execute() to subscribe customer before send email
          *
          * @param Subject $subject
          * @param Closure $proceed
          *
          * @return mixed
          */
          public function aroundExecute(Subject $subject, Closure $proceed)
          {
          if ($subject->getRequest()->isPost()) {
          $post = $subject->getRequest()->getPostValue();
          if ((boolean)$subject->getRequest()->getParam('is_subscribed') === true) {
          $this->subscriberFactory->create()->subscribe($post['email']);
          }
          }
          return $proceed();
          }
          }



          app/code/Stackoverflow/Contact/etc/frontend/di.xml









          <type name="MagentoContactControllerIndexPost">
          <plugin sortOrder="1" name="stackoverflowContactPost"
          type="StackoverflowContactPluginControllerIndexPost"/>
          </type>







          share|improve this answer















          You will need to create a new module with a new helper for it to check if customer is subscribed in this situation.




          app/code/Stackoverflow/Contact/Helper/Data.php




          <?php
          /**
          * Copyright © 2017 Toan Nguyen. All rights reserved.
          * See COPYING.txt for license details.
          */

          namespace StackoverflowContactHelper;

          use MagentoCustomerApiDataCustomerInterface;
          use MagentoCustomerModelSession;
          use MagentoFrameworkAppHelperAbstractHelper;
          use MagentoFrameworkAppHelperContext;
          use MagentoNewsletterModelSubscriber;

          /**
          * Contact base helper
          */
          class Data extends AbstractHelper
          {
          /**
          * @var MagentoCustomerModelSession
          */
          protected $customerSession;

          /**
          * @var MagentoNewsletterModelSubscriber
          */
          protected $subscriber;

          /**
          * @param Context $context
          * @param Session $customerSession
          * @param Subscriber $subscriber
          */
          public function __construct(
          Context $context,
          Session $customerSession,
          Subscriber $subscriber
          ) {
          $this->customerSession = $customerSession;
          $this->subscriber = $subscriber;
          parent::__construct($context);
          }

          /**
          * Check if customer subsribed
          *
          * @return bool
          */
          public function isSubscribed()
          {
          if (!$this->customerSession->isLoggedIn()) {
          return false;
          }

          /**
          * @var CustomerInterface $customer
          */
          $customer = $this->customerSession->getCustomer();
          $checkSubscriber = $this->subscriber->loadByEmail($customer->getEmail());

          return $checkSubscriber->isSubscribed();
          }
          }



          app/design/frontend/YOURVENDOR/YOURTHEME/Magento_Contact/templates/form.phtml




              <div class="field choice newsletter">
          <input type="checkbox" name="is_subscribed" title="<?php /* @escapeNotVerified */ echo __('Sign Up for Newsletter') ?>" value="1" id="is_subscribed"<?php if ($this->helper('StackoverflowContactHelperData')->isSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox">
          <label for="is_subscribed" class="label"><span><?php /* @escapeNotVerified */ echo __('Sign Up for Newsletter') ?></span></label>
          </div>



          app/code/Stackoverflow/Contact/Plugin/Controller/Index/Post.php




          <?php
          /**
          * Copyright © 2017 Toan Nguyen. All rights reserved.
          * See COPYING.txt for license details.
          */

          namespace StackoverflowContactPluginControllerIndex;

          use MagentoContactControllerIndexPost as Subject;
          use MagentoCustomerModelSession;
          use MagentoNewsletterModelSubscriberFactory;

          class Post
          {
          /**
          * @var SubscriberFactory
          */
          protected $subscriberFactory;

          /**
          * @var Session
          */
          protected $customerSession;

          /**
          * Post constructor.
          *
          * @param SubscriberFactory $subscriberFactory
          * @param Session $customerSession
          */
          public function __construct(
          SubscriberFactory $subscriberFactory,
          Session $customerSession
          ) {
          $this->subscriberFactory = $subscriberFactory;
          $this->customerSession = $customerSession;
          }


          /**
          * Override execute() to subscribe customer before send email
          *
          * @param Subject $subject
          * @param Closure $proceed
          *
          * @return mixed
          */
          public function aroundExecute(Subject $subject, Closure $proceed)
          {
          if ($subject->getRequest()->isPost()) {
          $post = $subject->getRequest()->getPostValue();
          if ((boolean)$subject->getRequest()->getParam('is_subscribed') === true) {
          $this->subscriberFactory->create()->subscribe($post['email']);
          }
          }
          return $proceed();
          }
          }



          app/code/Stackoverflow/Contact/etc/frontend/di.xml









          <type name="MagentoContactControllerIndexPost">
          <plugin sortOrder="1" name="stackoverflowContactPost"
          type="StackoverflowContactPluginControllerIndexPost"/>
          </type>








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 10 '17 at 11:54

























          answered Jul 10 '17 at 4:28









          Toan NguyenToan Nguyen

          1,9871137




          1,9871137













          • I tried this, but after massage sent, email is is not added in newsletter subscriber and even customer not getting mail for subscribe

            – sam
            Jul 10 '17 at 4:40











          • @sam I misunderstood your question, I thought you want to get the status is_subscribed in contact form. You will also need to change the contact form post controller to subscribe the customer manually.

            – Toan Nguyen
            Jul 10 '17 at 5:30













          • what i have to add there, in controller file for subscribe?

            – sam
            Jul 10 '17 at 5:35











          • @sam Please check my updated answer.

            – Toan Nguyen
            Jul 10 '17 at 5:40











          • getting this error Fatal error: Call to undefined method SfiiBulkbuyPluginControllerIndexPost::getRequest() in /home/schoolin/public_html/demo/app/code/Sfii/Bulkbuy/Plugin/Controller/Index/Post.php on line 51

            – sam
            Jul 10 '17 at 9:41



















          • I tried this, but after massage sent, email is is not added in newsletter subscriber and even customer not getting mail for subscribe

            – sam
            Jul 10 '17 at 4:40











          • @sam I misunderstood your question, I thought you want to get the status is_subscribed in contact form. You will also need to change the contact form post controller to subscribe the customer manually.

            – Toan Nguyen
            Jul 10 '17 at 5:30













          • what i have to add there, in controller file for subscribe?

            – sam
            Jul 10 '17 at 5:35











          • @sam Please check my updated answer.

            – Toan Nguyen
            Jul 10 '17 at 5:40











          • getting this error Fatal error: Call to undefined method SfiiBulkbuyPluginControllerIndexPost::getRequest() in /home/schoolin/public_html/demo/app/code/Sfii/Bulkbuy/Plugin/Controller/Index/Post.php on line 51

            – sam
            Jul 10 '17 at 9:41

















          I tried this, but after massage sent, email is is not added in newsletter subscriber and even customer not getting mail for subscribe

          – sam
          Jul 10 '17 at 4:40





          I tried this, but after massage sent, email is is not added in newsletter subscriber and even customer not getting mail for subscribe

          – sam
          Jul 10 '17 at 4:40













          @sam I misunderstood your question, I thought you want to get the status is_subscribed in contact form. You will also need to change the contact form post controller to subscribe the customer manually.

          – Toan Nguyen
          Jul 10 '17 at 5:30







          @sam I misunderstood your question, I thought you want to get the status is_subscribed in contact form. You will also need to change the contact form post controller to subscribe the customer manually.

          – Toan Nguyen
          Jul 10 '17 at 5:30















          what i have to add there, in controller file for subscribe?

          – sam
          Jul 10 '17 at 5:35





          what i have to add there, in controller file for subscribe?

          – sam
          Jul 10 '17 at 5:35













          @sam Please check my updated answer.

          – Toan Nguyen
          Jul 10 '17 at 5:40





          @sam Please check my updated answer.

          – Toan Nguyen
          Jul 10 '17 at 5:40













          getting this error Fatal error: Call to undefined method SfiiBulkbuyPluginControllerIndexPost::getRequest() in /home/schoolin/public_html/demo/app/code/Sfii/Bulkbuy/Plugin/Controller/Index/Post.php on line 51

          – sam
          Jul 10 '17 at 9:41





          getting this error Fatal error: Call to undefined method SfiiBulkbuyPluginControllerIndexPost::getRequest() in /home/schoolin/public_html/demo/app/code/Sfii/Bulkbuy/Plugin/Controller/Index/Post.php on line 51

          – sam
          Jul 10 '17 at 9:41


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f182810%2fnewsletter-checkbox-on-contact-us-page-in-magento-2%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          “%fieldName is a required field.”, in Magento2 REST API Call for GET Method Type The Next...

          How to change City field to a dropdown in Checkout step Magento 2Magento 2 : How to change UI field(s)...

          夢乃愛華...