I want to add 3 new fields in news letter popup for that i have written custom module. i am overriding Subscriber model and New Action Controller. But not able to call subscribe method of subscriber model. and also getting error :
PHP Fatal error: Uncaught TypeError: Argument 1 passed to Navin\Subscribe\Controller\Subscribe\NewAction::__construct() must be an instance of Navin\Subscribe\Model\Subscriber, instance of Magento\Framework\App\Action\Context given, called in /var/www/html/jansport/var/generation/Navin/Subscribe/Controller/Subscribe/NewAction/Interceptor.php on line 14 and defined in /var/www/html/jansport/app/code/Navin/Subscribe/Controller/Subscribe/NewAction.php:33\nStack trace:\n#0 /var/www/html/jansport/var/generation/Navin/Subscribe/Controller/Subscribe/NewAction/Interceptor.php(14): Navin\Subscribe\Controller\Subscribe\NewAction->__construct(Object(Magento\Framework\App\Action\Context), Object(Magento\Newsletter\Model\SubscriberFactory), Object(Magento\Customer\Model\Session\Interceptor), Object(Magento\Store\Model\StoreManager), Object(Magento\Customer\Model\Url), Object(Magento\Customer\Model\AccountManagement\Interceptor))\n#1 /var/www/html/jansport/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(93): Navin\Subscribe in /var/www/html/jansport/app/code/Navin/Subscribe/Controller/Subscribe/NewAction.php on line 33
My code is: di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Newsletter\Controller\Subscriber\NewAction" type="Navin\Subscribe\Controller\Subscribe\NewAction" />
<preference for="Magento\Newsletter\Model\Subscriber" type="Navin\Subscribe\Model\Subscriber" />
</config>
NewAction.php Controller
namespace Navin\Subscribe\Controller\Subscribe; //use Navin\Subscribe\Model\SubscriberFactory; class NewAction extends \Magento\Newsletter\Controller\Subscriber\NewAction { /** * @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page */ protected $subscriberFactory; public function __construct( \Navin\Subscribe\Model\Subscriber $subscriberFactory ) { $this->subscriberFactory = $subscriberFactory; } public function execute() { //echo 'test come here';exit; $post = $this->getRequest()->getPostValue(); //var_dump($post);exit; if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) { $email = (string) $this->getRequest()->getPost('email'); $subscriber_name = (string) $this->getRequest()->getPost('subscriber_name'); $subscriber_date_of_birth = (string) $this->getRequest()->getPost('subscriber_dateofbirth'); $subscriber_country_code = (string) $this->getRequest()->getPost('subscriber_countrycode'); $requestCatalog = (string) $this->getRequest()->getPost('requestCatalog'); try { $this->validateEmailFormat($email); $this->validateGuestSubscription(); $this->validateEmailAvailable($email); $status = $this->subscriberFactory->subscribe($email, $subscriber_name, $subscriber_date_of_birth, $subscriber_country_code); if ($status == \Magento\Newsletter\Model\Subscriber::STATUS_NOT_ACTIVE) { $this->messageManager->addSuccess(__('The confirmation request has been sent.')); } else { $this->messageManager->addSuccess(__('Thank you for your subscription.')); } } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->messageManager->addException( $e, __('There was a problem with the subscription: %1', $e->getMessage()) ); } catch (\Exception $e) { $this->messageManager->addException($e, __('Something went wrong with the subscription.')); } } $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl()); } }
Subscriber.php Model
namespace Navin\Subscribe\Model; class Subscriber extends Magento\Newsletter\Model\Subscriber { /** * Initialize resource model * * @return void */ public function subscribe($email, $subscriber_name = '', $subscriber_date_of_birth = '', $subscriber_country_code = '') { $this->loadByEmail($email); if (!$this->getId()) { $this->setSubscriberConfirmCode($this->randomSequence()); } $isConfirmNeed = $this->_scopeConfig->getValue( self::XML_PATH_CONFIRMATION_FLAG, \Magento\Store\Model\ScopeInterface::SCOPE_STORE ) == 1 ? true : false; $isOwnSubscribes = false; $isSubscribeOwnEmail = $this->_customerSession->isLoggedIn() && $this->_customerSession->getCustomerDataObject()->getEmail() == $email; if (!$this->getId() || $this->getStatus() == self::STATUS_UNSUBSCRIBED || $this->getStatus() == self::STATUS_NOT_ACTIVE ) { if ($isConfirmNeed === true) { // if user subscribes own login email - confirmation is not needed $isOwnSubscribes = $isSubscribeOwnEmail; if ($isOwnSubscribes == true) { $this->setStatus(self::STATUS_SUBSCRIBED); } else { $this->setStatus(self::STATUS_NOT_ACTIVE); } } else { $this->setStatus(self::STATUS_SUBSCRIBED); } $this->setSubscriberEmail($email); $this->setSubscriberName($subscriber_name); //subscriber_name $this->setSubscriberDateofbirth($subscriber_date_of_birth); //date of birth $this->setSubscriberCountrycode($subscriber_country_code); //country code } if ($isSubscribeOwnEmail) { try { $customer = $this->customerRepository->getById($this->_customerSession->getCustomerId()); $this->setStoreId($customer->getStoreId()); $this->setCustomerId($customer->getId()); } catch (NoSuchEntityException $e) { $this->setStoreId($this->_storeManager->getStore()->getId()); $this->setCustomerId(0); } } else { $this->setStoreId($this->_storeManager->getStore()->getId()); $this->setCustomerId(0); } $this->setStatusChanged(true); try { $this->save(); if ($isConfirmNeed === true && $isOwnSubscribes === false ) { $this->sendConfirmationRequestEmail(); } else { $this->sendConfirmationSuccessEmail(); } return $this->getStatus(); } catch (\Exception $e) { throw new \Exception($e->getMessage()); } } }
pub/static
files expext .htaccess andvar/generation
files and the deploy static content. See the result again.