Skip to content

custom api endpoint for newsletter unsubscription question

I wrote an API endpoint to handle unsubscribe thru API call
It is working well. However, I don’t want Magento to send out unsubscribe email to customer, how can I achieve this?

Thanks

<?php

namespace ABCNewsletterEndpointModel;

use ABCNewsletterEndpointApiSubscriptionInterface;
use MagentoFrameworkExceptionNoSuchEntityException;

class Subscription implements SubscriptionInterface
{
    protected $subscriberFactory;
    protected $customer;
    protected $storemanager;

    public function __construct(
        MagentoNewsletterModelSubscriberFactory $subscriberFactory,
        MagentoCustomerModelCustomerFactory $customer,
        MagentoStoreModelStoreManagerInterface $storemanager
    ) {
        $this->subscriberFactory = $subscriberFactory;
        $this->customer = $customer;
        $this->storemanager = $storemanager;
    }

    /**
     * Returns user email
     * @param string $email
     * @return void
     */
    public function subscribeByEmail($email)
    {
        $websiteId = $this->storemanager->getStore()->getWebsiteId();
        $customerId = $this->customer->create()->setWebsiteId($websiteId)->loadByEmail($email)->getId();
        if ($customerId) {
            $this->subscriberFactory->create()->subscribeCustomerById($customerId);
        } else {
            $this->subscriberFactory->create()->subscribe($email);
        }
    }

    /**
     * @param string $email
     * @return string
     */
    public function isSubscribed($email)
    {
        $subscriber = $this->subscriberFactory->create()->loadByEmail($email);
        if ($subscriber->getId()) {
            if ($subscriber->isSubscribed()) {
                return "Email found. Subscription status: Subscribed";
            } else {
                return "Email found. Subscription status: Not Subscribed";
            }
        } else {
            throw new NoSuchEntityException(__('Email not found'));
        }
    }

    /**
     * @param string $email
     * @return void
     */
    public function deleteByEmail($email)
    {
        $websiteId = $this->storemanager->getStore()->getWebsiteId();
        $customerId = $this->customer->create()->setWebsiteId($websiteId)->loadByEmail($email)->getId();

        // Check if the email belongs to a customer
        if ($customerId) {
            $this->subscriberFactory->create()->unsubscribeCustomerById($customerId, true); // Disable email sending
            return; // Unsubscribed successfully
        }

        // Check if the email exists in the newsletter subscriber table
        $subscriber = $this->subscriberFactory->create()->loadByEmail($email);
        if ($subscriber->getId()) {
            $subscriber->unsubscribe(true); // Disable email sending
            return; // Unsubscribed successfully
        }

        // If email is neither a customer nor a newsletter subscriber, do nothing
    }