Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a segment programatically #490

Open
scandella opened this issue Aug 31, 2024 · 1 comment
Open

Adding a segment programatically #490

scandella opened this issue Aug 31, 2024 · 1 comment
Labels
question Further information is requested

Comments

@scandella
Copy link
Contributor

Support Request

Is there a way to add segments programmatically?
As for contacts we have:
FormService::createAndSubscribe()

Is there a method for segments to be added from a module?

Thanks for your help.

Plugin Version

2.15.3

@scandella scandella added the question Further information is requested label Aug 31, 2024
@bencroker
Copy link
Collaborator

Creating a segment can be done as with any element. The challenge is going to be to construct the contact condition, which can be passed in using a ContactCondition class or an array. The simplest way to figure this out might be go inspect the request parameters in the Network tab of your browser’s dev tools when creating/saving a segment in the CP.

use Craft;
use putyourlightson\campaign\elements\SegmentElement;

$segment = new SegmentElement();
$segment->title = 'Test Segment';
$segment->slug = 'test-segment';
$segment->segmentType = 'regular';

// Pass in a ContactCondition class or an array
$segment->setContactCondition([]);

Craft::$app->getElements()->saveElement($segment);

@bencroker bencroker changed the title Ading segment Adding a segment programatically Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants