Learndash and make automation using action hook php - learndash_lesson_completed

214 Views Asked by At

I am trying to connect Learndash with Make , using action hook. I have tried the code below , but when clicking the complete lesson on Learndash I get a PHP Fatal error.

The code:

function my_custom_lesson_completed_action( $user_id, $lesson_id ) {
// Retrieve necessary information about the user and lesson
$user = get_user_by( 'ID', $user_id );
$lesson = get_post( $lesson_id );
// Prepare the payload for the webhook
$payload = array(
'user_id' => $user_id,
'user_email' => $user->user_email,
'lesson_id' => $lesson_id,
'lesson_title' => $lesson->post_title,
// Include any other relevant data you want to send
);
// Set the webhook URL provided by Integromat or your preferred integration service
$webhook_url = 'https://hook.integromat.com/your_webhook_url';
// Send the webhook request
$response = wp_remote_post( $webhook_url, array(
'method' => 'POST',
'headers' => array(
'Content-Type' => 'application/json',
),
'body' => wp_json_encode( $payload ),
) );
// Check if the webhook request was successful
if ( is_wp_error( $response ) ) {
error_log( 'Webhook request failed: ' . $response->get_error_message() );
} else {
// Webhook request successful, you can perform any additional actions or logging here
}
}
// Hook the custom function to the learndash_lesson_completed hook
add_action( 'learndash_lesson_completed', 'my_custom_lesson_completed_action', 10, 2 );

This is the error recived : [20-Jun-2023 16:49:47 UTC] PHP Fatal error Uncaught ArgumentCountError: Too few arguments to function my_custom_lesson_completed_action(), 1 passed in /home/development/public_html/wp-includes/class-wp-hook.php on line 308 and exactly 6 expected in /home/development/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(582) : eval()'d code:1

Stack trace:

#0 /home/development/public_html/wp-includes/class-wp-hook.php(308): my_custom_lesson_completed_action() #1 /home/development/public_html/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters() #2 /home/development/public_html/wp-includes/plugin.php(517): WP_Hook->do_action() #3 /home/development/public_html/wp-content/plugins/sfwd-lms/includes/course/ld-course-progress.php(945): do_action() #4 /home/development/public_html/wp-content/plugins/sfwd-lms/includes/course/ld-course-progress.php(420): learndash_process_mark_complete() #5 /home/development/public_html/wp-includes/class-wp-hook.php(308): learndash_mark_complete_process() #6 /home/development/public_html/wp-includes/class-wp-ho in /home/development/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(582) : eval()'d code on line 1

Any help will be much appreciated!

When lesson complete trigger a webhook and send user details

1

There are 1 best solutions below

0
On

This should fix the issue.

function my_custom_lesson_completed_action( $user_id, $lesson_id, $course_id, $step_id, $is_completed = 0 ) {
  // Retrieve necessary information about the user and lesson
  $user = get_user_by( 'ID', $user_id );
  $lesson = get_post( $lesson_id );
  // Prepare the payload for the webhook
  $payload = array(
    'user_id' => $user_id,
    'user_email' => $user->user_email,
    'lesson_id' => $lesson_id,
    'lesson_title' => $lesson->post_title,
    // Include any other relevant data you want to send
  );
  // Set the webhook URL provided by Integromat or your preferred integration service
  $webhook_url = 'https://hook.integromat.com/your_webhook_url';
  // Send the webhook request
  $response = wp_remote_post( $webhook_url, array(
    'method' => 'POST',
    'headers' => array(
      'Content-Type' => 'application/json',
    ),
    'body' => wp_json_encode( $payload ),
  ) );
  // Check if the webhook request was successful
  if ( is_wp_error( $response ) ) {
    error_log( 'Webhook request failed: ' . $response->get_error_message() );
  } else {
    // Webhook request successful, you can perform any additional actions or logging here
  }
}

// Hook the custom function to the learndash_lesson_completed hook
add_action( 'learndash_lesson_completed', 'my_custom_lesson_completed_action', 10, 5 );