putyourlightson / craft-campaign

Send and manage email campaigns, contacts and mailing lists in Craft CMS.
https://putyourlightson.com/plugins/campaign
Other
63 stars 25 forks source link

mailing list query returns null #363

Closed 1stevengrant closed 1 year ago

1stevengrant commented 1 year ago

Kicking the tyres on Campaign for a project.

I've configured the plugin and in the control panel I can see mailing list, contacts but for some reason, when I query the mailing list based on id, as per the docs

{% set mailingList = craft.campaign.mailingLists.id(49861).one() %}

{% if mailingList %}
   <h1> Subscribe to {{ mailingList.title }}</h1>
{% endif %}

the mailing list definitely exists, I've tried deleting it and recreating but still the same issue. I can see the mailing list in the database also.

I'm stumped. Any gotchas that I'm missing?

I've also tried getting by handle and slug 🤷‍♂️

bencroker commented 1 year ago

Have you double checked that the mailing list is enabled and that the mailing list type is enabled for the site you're testing with?

1stevengrant commented 1 year ago

aha - that's what's thrown me...the relationship between a mailing list and site.

Here's my conundrum, we have multiple sites in this instance.

We have an organisation site and a site for a conference the organisation operates.

The mailing list was assigned to the organisation but being called on the conference site. Is there a way to do that?

bencroker commented 1 year ago

Have you tried adding .siteId(1) to the query, replacing 1 with the site ID the mailing list type is enabled for?

1stevengrant commented 1 year ago

brill, thanks so much @bencroker - just the job

1stevengrant commented 1 year ago

@bencroker another likely dumb question but with this snippet

<form method="post" action="">
                        {{ csrfInput() }}
                        <input type="hidden" name="action" value="campaign/forms/subscribe" />
                        <input type="hidden" name="mailingList" value="{{ mailingList.slug }}" />
                        <input type="hidden" name="redirect" value="{{ 'subscribe-success'|hash }}" />

when submitting the form I get the error Mailing list not found in /vendor/putyourlightson/craft-campaign/src/controllers/FormsController.php

is that because it's trying to find a form with that particular slug in the context of the current site?

bencroker commented 1 year ago

Yes, it looks like the same issue as above.

1stevengrant commented 1 year ago

Yes, it looks like the same issue as above.

so that will never work the way I intend then doing

{% set mailingList = craft.campaign.mailingLists.siteId(4).id(49863).one() %}

<form method="post" action="">
                        {{ csrfInput() }}
                        <input type="hidden" name="action" value="campaign/forms/subscribe" />
                        <input type="hidden" name="mailingList" value="{{ mailingList.slug }}" />
                        <input type="hidden" name="redirect" value="{{ 'subscribe-success'|hash }}" />
...
bencroker commented 1 year ago

Yeah I see what you mean. I may add an optional siteId parameter to the forms controller. Are you running Campaign 2?

1stevengrant commented 1 year ago

yeah, latest version. That would be useful. Appreciate this is probably quite edge case.

My workaround for now is to use a Freeform form to capture details and hook up their Campaign integration in the background.

bencroker commented 1 year ago

I added an optional siteId parameter to the front-end subscribe and unsubscribe forms in https://github.com/putyourlightson/craft-campaign/commit/5f068f268d3543621a16aa6b283c531476cf50d6 and released in version 2.5.3.

<input type="hidden" name="siteId" value="2" />
1stevengrant commented 1 year ago

what a man! thank you

bencroker commented 1 year ago

FYI I updated the docs at https://putyourlightson.com/plugins/campaign#front-end-forms