2

I tried to update it with the following values:

PUT https://example.com/rest/V1/customers/3

BODY

{
  "customer": {
    "id": "47",
    "email": "[email protected]",
    "firstname": "John",
    "lastname": "Johnson",
    "store_id": 1,
    "website_id": 1,
    "extension_attributes": {
     "is_subscribed": true
    }
  }
}

I get a 200 respond back but the newsletter update didn't update to true.

1 Answer 1

1

I believe you can't set is_subscribed by the customer API because what determines if a customer is subscribed to the newsletter is actually determined by the \Magento\Newsletter\Model\Subscriber Model. So if you want to do this via api you'll need to create a custom API call to do such. Here's a quick module I threw together for you.

Create a new module

<Vendor>_Newsletter

Create module.xml file

app/code/<Vendor>/Newsletter/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Newsletter" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Newsletter" />
        </sequence>
    </module>
</config>

Create webapi.xml file

app/code/<Vendor>/Newsletter/etc/webapi.xml

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/newsletter/subscribe/:email" method="GET">
        <service class="Magento\Newsletter\Model\Subscriber" method="subscribe"/>
        <resources>
            <resource ref="Magento_Newsletter::subscriber"/>
        </resources>
    </route>
</routes>

Now you should be able to subscribe a customer by simply making a API GET request to https://your_magento_store.com/rest/V1/newsletter/subscribe/[email protected]. This will call \Magento\Newsletter\Model\Subscriber::subscribe and pass in the email provided in the request uri as the parameter to the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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