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>