0

I am upgrading Magento to version 2.4.2. After upgraded, when I ran di compile command,

I got the below error.

Errors during compilation:
    WeltPixel\ThankYouPage\Controller\Newsletter\Subscribe
        Incompatible argument type: Required type: \Magento\Newsletter\Model\SubscriptionManagerInterface. Actual type: array; File: 
/var/www/html/module/app/code/WeltPixel/ThankYouPage/Controller/Newsletter/Subscribe.php

I checked the Subscribe.php file but I didn't get anything.

Subscribe.php

<?php
namespace WeltPixel\ThankYouPage\Controller\Newsletter;

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\Controller\Result\JsonFactory;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Subscribe extends \Magento\Newsletter\Controller\Subscriber\NewAction
{
    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $checkoutSession;

    /**
     * @var JsonFactory
     */
    protected $resultJsonFactory;

    /**
     * Initialize dependencies.
     *
     * @param Context $context
     * @param SubscriberFactory $subscriberFactory
     * @param Session $customerSession
     * @param StoreManagerInterface $storeManager
     * @param CustomerUrl $customerUrl
     * @param CustomerAccountManagement $customerAccountManagement
     * @param \Magento\Checkout\Model\Session $checkoutSession
     * @param JsonFactory $resultJsonFactory
     * @param array $data
     */
    public function __construct(
        Context $context,
        SubscriberFactory $subscriberFactory,
        Session $customerSession,
        StoreManagerInterface $storeManager,
        CustomerUrl $customerUrl,
        CustomerAccountManagement $customerAccountManagement,
        \Magento\Checkout\Model\Session $checkoutSession,
        JsonFactory $resultJsonFactory,
        array $data = []
    ) {
         parent::__construct(
            $context,
            $subscriberFactory,
            $customerSession,
            $storeManager,
            $customerUrl,
            $customerAccountManagement,
            $data
        );
        $this->checkoutSession = $checkoutSession;
        $this->resultJsonFactory = $resultJsonFactory;
       
    }


    /**
     * @param string $email
     * @throws \Magento\Framework\Exception\LocalizedException
     * @return void
     */
    protected function validateEmailAvailable($email)
    {
        /**
         * Original function rewritten
         * Allow the subscription for user on checkout success page for the same email address
         */
        $orderEmail = $this->checkoutSession->getLastRealOrder()->getCustomerEmail();
        if ($orderEmail == $email) {
            return true;
        }
        $websiteId = $this->_storeManager->getStore()->getWebsiteId();
        if ($this->_customerSession->getCustomerDataObject()->getEmail() !== $email
            && !$this->customerAccountManagement->isEmailAvailable($email, $websiteId)
        ) {
            throw new \Magento\Framework\Exception\LocalizedException(
                __('This email address is already assigned to another user.')
            );
        }
    }

    public function execute()
    {
        if (!$this->getRequest()->isAjax()) {
           return parent::execute();
        }


        if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) {

            $email = (string)$this->getRequest()->getPost('email');
            $sucessMessage = '';

            try {
                $this->validateEmailFormat($email);
                $this->validateGuestSubscription();
                $this->validateEmailAvailable($email);

                $subscriber = $this->_subscriberFactory->create()->loadByEmail($email);
                if ($subscriber->getId()
                    && $subscriber->getSubscriberStatus() == \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED
                ) {
                    return $this->resultJsonFactory->create()->setData(
                        [
                            'errors' => true,
                            'message' => __('This email address is already subscribed.')
                        ]
                    );
                }

                $status = $this->_subscriberFactory->create()->subscribe($email);
                if ($status == \Magento\Newsletter\Model\Subscriber::STATUS_NOT_ACTIVE) {
                    $sucessMessage = __('The confirmation request has been sent.');
                } else {
                    $sucessMessage = __('Thank you for your subscription.');
                }
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                return $this->resultJsonFactory->create()->setData(
                    [
                        'errors' => true,
                        'message' => __('There was a problem with the subscription: %1', $e->getMessage())
                    ]
                );
            } catch (\Exception $e) {
                return $this->resultJsonFactory->create()->setData(
                    [
                        'errors' => true,
                        'message' => __('Something went wrong with the subscription.')
                    ]
                );
            }

            return $this->resultJsonFactory->create()->setData(
                [
                    'errors' => false,
                    'message' => $sucessMessage
                ]
            );
        }

    }

}

if anyone knows, please help me out.

Thanks

1 Answer 1

0

Looks like WeltPixel\ThankYouPage\Controller\Newsletter\Subscribe is an extend of Magento\Newsletter\Controller\Subscriber\NewAction which was changed in 2.4.2 with an additional parameter SubscriptionManagerInterface (https://github.com/magento/magento2/commit/47e3ada764fc943fc63d88671ce10b2877fe57d0#diff-244d4c9eb135a719f51ad6ac8521bd91d4582a6c33e3a5b78c1b05b04b5bfcc3)

So WeltPixel\ThankYouPage\Controller\Newsletter\Subscribe will need to be updated with this parameter as well.

public function __construct(
    Context $context,
    SubscriberFactory $subscriberFactory,
    Session $customerSession,
    StoreManagerInterface $storeManager,
    CustomerUrl $customerUrl,
    CustomerAccountManagement $customerAccountManagement,
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Newsletter\Model\SubscriptionManagerInterface $subscriptionManager,
    JsonFactory $resultJsonFactory,
    array $data = []
) {
     parent::__construct(
        $context,
        $subscriberFactory,
        $customerSession,
        $storeManager,
        $customerUrl,
        $customerAccountManagement,
        $subscriptionManager,
        $data
    );
    $this->checkoutSession = $checkoutSession;
    $this->resultJsonFactory = $resultJsonFactory;
   
}

But all-in-all I think you're better off looking for an extension update from the developer. As there might be more 2.4.2 compatibility issues with the extension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.