I have installed a custom module related newsletter, I want my module to be compatible in 2.3 and 2.4. I'm overriding the class Magento\Newsletter\Controller\Subscriber\NewAction
MyVendor/MyModule/etc/di.xml
<preference for="Magento\Newsletter\Controller\Subscriber\NewAction" type="MyVendor\MyModule\Controller\Subscriber\NewAction" />
MyVendor/MyModule/Controller/Subscriber/NewAction.php
<?php
namespace MyVendor\MyModule\Controller\Subscriber;
use Magento\Customer\Api\AccountManagementInterface as CustomerAccountManagement;
use Magento\Customer\Model\Session;
use Magento\Customer\Model\Url as CustomerUrl;
use Magento\Framework\App\Action\Context;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Newsletter\Model\SubscriberFactory;
use Magento\Framework\App\ProductMetadataInterface;
use Magento\Framework\App\ObjectManager;
class NewAction extends \Magento\Newsletter\Controller\Subscriber\NewAction
{
/**
* @var \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
*/
protected $resultJsonFactory;
/**
* @var ProductMetadataInterface $productMetadata
*/
protected $productMetadata;
/**
* Initialize dependencies.
*
* @param Context $context
* @param SubscriberFactory $subscriberFactory
* @param Session $customerSession
* @param StoreManagerInterface $storeManager
* @param CustomerUrl $customerUrl
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
* @param \Magento\Newsletter\Model\Subscriber $subscriber
* @param CustomerAccountManagement $customerAccountManagement
* @param ProductMetadataInterface $productMetadata
*/
public function __construct(
Context $context,
SubscriberFactory $subscriberFactory,
Session $customerSession,
StoreManagerInterface $storeManager,
CustomerUrl $customerUrl,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
\Magento\Newsletter\Model\Subscriber $subscriber,
CustomerAccountManagement $customerAccountManagement,
ProductMetadataInterface $productMetadata
)
{
$this->resultJsonFactory = $resultJsonFactory;
$this->_subscriber= $subscriber;
$this->productMetadata = $productMetadata;
if ($this->productMetadata->getVersion() >= "2.4.0") {
$subscriptionManager = ObjectManager::getInstance()->get(\Magento\Newsletter\Model\SubscriptionManagerInterface::class);
parent::__construct(
$context,
$subscriberFactory,
$customerSession,
$storeManager,
$customerUrl,
$customerAccountManagement,
$subscriptionManager
);
}else{
parent::__construct(
$context,
$subscriberFactory,
$customerSession,
$storeManager,
$customerUrl,
$customerAccountManagement
);
}
}
/**
* New subscription action
*
* @throws \Magento\Framework\Exception\LocalizedException
* @return void
*/
public function execute()
{
$ajax=$this->getRequest()->getPost('isajax');
if(isset($ajax) && $ajax == 1){
$result = $this->resultJsonFactory->create();
if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')){
$email = (string)$this->getRequest()->getPost('email');
try {
$this->validateEmailFormat($email);
$this->validateGuestSubscription();
$this->validateEmailAvailable($email);
$emailExist = $this->_subscriber->loadByEmail($email);
if ($emailExist->isSubscribed()) {
return $result->setData(['message' => 'This email address is already exist.']);
}
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$session = $objectManager->create('\Magento\Newsletter\Model\Session');
$session->setisNewsAjax($this->getRequest()->getPost('isajax'));
$session->setNewsPromocode($this->getRequest()->getPost('promo'));
$session->setCodeDesc($this->getRequest()->getPost('codedesc'));
$status = $this->_subscriberFactory->create()->subscribe($email);
if ($status == \Magento\Newsletter\Model\Subscriber::STATUS_NOT_ACTIVE) {
return $result->setData(['message' => '2']);
} else {
return $result->setData(['message' => '1']);
}
} catch (\Magento\Framework\Exception\LocalizedException $e) {
return $result->setData(['message' => 'There was a problem with the subscription']);
} catch (\Exception $e) {
return $result->setData(['message' => 'Sorry !!! due to some problems you are not able subscribe right now.']);
}
}
}
else{
parent::execute();
}
}
}
I have tried with the below code in construct but with that default Magento newsletter is not working in 2.4 and 2.3 and showing blank page on newsletter submit as shown in this screenshot https://nimb.ws/hFuldp
if ($this->productMetadata->getVersion() >= "2.4.0") {
$subscriptionManager = ObjectManager::getInstance()->get(\Magento\Newsletter\Model\SubscriptionManagerInterface::class);
parent::__construct(
$context,
$subscriberFactory,
$customerSession,
$storeManager,
$customerUrl,
$customerAccountManagement,
$subscriptionManager
);
}else{
parent::__construct(
$context,
$subscriberFactory,
$customerSession,
$storeManager,
$customerUrl,
$customerAccountManagement
);
}
Please let me know if anyone has a solution.