• Resolved gutding

    (@gutding)


    Hi there,
    I’ve developed a special registration form on which users can also opt-in for different MailPoet lists. The procedure is that anybody can apply for an account, but it is pending. After manual review the user will get either approved or rejected. In case of an approval, the user will be subscribed to the selected MP lists using the API. As the user will always get automatically added by Mailpoet to the MP wordpress user list (meaning that he always exists when my afetr-approval function runs), I use the $mailpoet_api->subscribeToLists() function. That all works well. The only thing is that I haven’t found a way to update subscriber fields after the auto creation by MailPoet. I have custom fields set up on the regular MP form, which I would like to populate by my program. As there is no API function for updating subscriber data or a hook that could do that, has anybody an idea how to achieve that?

    Thank you for your help.

    regards, Rado

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Gui A. a11n

    (@guicmazeredo)

    Hi there,

    Thanks for reaching out!

    Indeed, as you can see on our current API methods, although we do have a method that allows you to add a subscriber field, there isn’t one available to update them.

    An alternative, would be using an Automation with the Update Subscriber action. However, you’d need to use one of the available triggers. If none of the triggers match what you need, you may be able to create a custom trigger.

    I hope that works for you!

    Thread Starter gutding

    (@gutding)

    Thanks Gui for the hints. I’ve looked into the ‘Update subscriber action’. As far as I can see, I could only update a custom field with a fixed value here:

    Couldn’t find anything about inserting something like a shortcode here…
    So i went the brutal way, inserting my custom field value directly into the db. That works as intended. If anybody has the same problem, here is part of the code how to do it:

    // Prepare subscriber data
    $subscriber_data = [
    'email' => $email,
    'first_name' => $first_name,
    'last_name' => $last_name,
    //'custom_fields' => [
    'cf_2' => $shop_name
    //]
    ];
    if (class_exists(\MailPoet\API\API::class)) {
    $mailpoet_api = \MailPoet\API\API::MP('v1');
    // first see if the user exists
    try {
    $subscriber = $mailpoet_api->getSubscriber( $email );
    } catch ( \Throwable $th ) {
    error_log('getSubscriber: ' . $th->getMessage());
    }
    // If the subscriber doesn't exist, add them.
    if ( ! $subscriber ) {
    try {
    $result = $mailpoet_api->addSubscriber( $subscriber_data, $list_array );
    } catch (\Throwable $th) {
    error_log('addSubscriber: ' . $th->getMessage());
    }
    } else {
    try {
    $result = $mailpoet_api->subscribeToLists( $subscriber['id'], $list_array );
    // put the shop name directly into the db, as there is no update->subscriberData
    // in the API
    global $wpdb;
    // Define MailPoet table name
    $table_name = $wpdb->prefix . 'mailpoet_subscriber_custom_field';
    $subscriber_id = $subscriber['id'];
    $custom_field_id = 2;
    // Construct and execute the insert query
    $inserted = $wpdb->insert(
    $table_name,
    array(
    'subscriber_id' => $subscriber_id,
    'custom_field_id' => $custom_field_id,
    'value' => $shop_name,
    'created_at' => current_time('mysql'),
    'updated_at' => current_time('mysql')
    ),
    array(
    '%d', // subscriber_id (int)
    '%d', // custom_field_id (int)
    '%s', // value (text)
    '%s', // created_at (timestamp)
    '%s' // updated_at (timestamp)
    )
    );
    error_log('Insert customer shop name in MailPoet: ' . $inserted);
    } catch ( \Throwable $th ) {
    error_log('subscribeToLists: ' . $th->getMessage());
    }
    }
    //error_log('MailPoet addSubscriber/subscribeToLists return info: ' . print_r($result, true));
    // Check for errors
    if (isset($result['error'])) {
    error_log('MailPoet subscription error: ' . $result['error']['message']);
    }
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Update custom field by API or hook’ is closed to new replies.