4

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 ???

1 Answer 1

6

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 Stackoverflow\Contact\Helper; use Magento\Customer\Api\Data\CustomerInterface; use Magento\Customer\Model\Session; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Newsletter\Model\Subscriber; /** * Contact base helper */ class Data extends AbstractHelper { /** * @var \Magento\Customer\Model\Session */ protected $customerSession; /** * @var \Magento\Newsletter\Model\Subscriber */ 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('Stackoverflow\Contact\Helper\Data')->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 Stackoverflow\Contact\Plugin\Controller\Index; use Magento\Contact\Controller\Index\Post as Subject; use Magento\Customer\Model\Session; use Magento\Newsletter\Model\SubscriberFactory; 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="Magento\Contact\Controller\Index\Post"> <plugin sortOrder="1" name="stackoverflowContactPost" type="Stackoverflow\Contact\Plugin\Controller\Index\Post"/> </type> 

11
  • I tried this, but after massage sent, email is is not added in newsletter subscriber and even customer not getting mail for subscribe Commented Jul 10, 2017 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. Commented Jul 10, 2017 at 5:30
  • what i have to add there, in controller file for subscribe? Commented Jul 10, 2017 at 5:35
  • @sam Please check my updated answer. Commented Jul 10, 2017 at 5:40
  • getting this error Fatal error: Call to undefined method Sfii\Bulkbuy\Plugin\Controller\Index\Post::getRequest() in /home/schoolin/public_html/demo/app/code/Sfii/Bulkbuy/Plugin/Controller/Index/Post.php on line 51 Commented Jul 10, 2017 at 9:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.