Skip to content

Instantly share code, notes, and snippets.

@Crocoblock
Last active May 30, 2024 07:30
Show Gist options
  • Save Crocoblock/b5b6ef62436b01ce23bc60d1a554a85b to your computer and use it in GitHub Desktop.
Save Crocoblock/b5b6ef62436b01ce23bc60d1a554a85b to your computer and use it in GitHub Desktop.
Get JetEngine Related Items programmatically / Update related items / Get/Update relation meta
@AndresCarignano
Copy link

So helpful!

@kankadev
Copy link

kankadev commented May 23, 2024

Very helpful. Thanks.

I use the last action. It works for the initial connecting but not if I change the meta field value of a existing relation.

My usecase is:

I have a "Job CCT". The relation is one to many between Job CCT and user. There is one job and many people can apply for it. The relation has got a meta field called "status" where I select "applied", "meeting", "canceled" etc.

I want to fire actions if this meta field value changes. If the status changes from applied to canceld, I want to send an e-mail.

Which hook can I use for this?

Thank you very much.

EDIT:

I think I found a solution:

add_action('jet-engine/relation/update-all-meta/after', function ($parent_object, $child_object, $new_meta, $old_meta) {
    write_log('jet-engine/relation/update-all-meta/after');

    if (is_array($new_meta) && isset($new_meta['status'])) {

        $relation = jet_engine()->relations->get_active_relations(13);
        $user_rows = $relation->get_children($parent_object, 'all');
        $user_id = 0;
        if (is_array($user_rows) && count($user_rows) > 0)
            $user_id = (int)$user_rows[0]['child_object_id'];

        if ($user_id > 0):
            $user = get_user_by('ID', $user_id);
            if ($user instanceof WP_User):
                $user_email = $user->user_email;
                $user_name = $user->display_name;
                $user_first_name = $user->first_name;
                $user_last_name = $user->last_name;

                switch ($new_meta['status']):
                    case 'in planning':

                        break;
                    case 'planned':

                        wp_mail($user_email, 'Planned', 'Planned');
                        break;

                    case 'canceled':

                        break;

                    case 'realized':

                        break;
                endswitch;
            endif;
        endif;

    }
}, 12, 4);

@moxet
Copy link

moxet commented May 27, 2024

Very helpful, i was building a CRM in which the client need to upload leads from excel and assign it directly to agents. The assignment is relation but not to make agent as post author; thus this 3 lines of codes help a lot.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment