I am building a woocommerce plugin for facebook popup modal login. if user click on add to cart button, modal will displayed for login, after successful login, product will be added to cart and redirect to cart page.
I have tried to handle login with php and popup with javascript, but failed.
Help me to correct my code
here is my php code
function modal_tamplate_html() {
session_start();
define('FB_APP_ID', 'xxxxxxxxx');
define('FB_APP_SECRET', 'xxxxxxxxxx');
//$redirect_url = rtrim(home_url(), "/") . '/';
$redirect_url = 'http://localhost/crushslimdev/wordpress/cart/';
error_log('url:'.$redirect_url);
$fb = new Facebook([
'app_id' => 'xxxxxxx',
'app_secret' => 'xxxxxxxxxxx',
]);
$helper = $fb->getRedirectLoginHelper();
$login_url = $helper->getLoginUrl($redirect_url, ['email']);
$fb_login_btn = '<div class="option facebook-button">
<a href="' . esc_url($login_url) . '" id="facebook-login">
<img src="wp-content/plugins/facebook login plugin/assets/fb.png" alt="Google Icon" class="facebook-icon">
Sign in with Facebook
</a>
</div>';
if (isset($_GET['code'])) {
error_log('Facebook Callback - Code Parameter Detected');
$fb = new Facebook([
'app_id' => 'xxxxxxxxxxxx',
'app_secret' => 'xxxxxxxxxxxxx'
]);
$helper = $fb->getRedirectLoginHelper();
error_log('Facebook Callback - Helper created');
$access_token = $helper->getAccessToken();
if (isset($access_token)) {
error_log('Facebook Callback - Access Token Obtained');
$fb->setDefaultAccessToken($access_token);
$response = $fb->get('/me?fields=id,name,email,picture');
$user = $response->getGraphUser();
$email = $user->getEmail();
$user_id = $user->getId();
error_log('Facebook Callback - Email: ' . $email);
error_log('Facebook Callback - User ID: ' . $user_id);
$existing_user = get_user_by('email', $email);
if ($existing_user) {
wp_set_auth_cookie($existing_user->ID, true);
error_log('Facebook Callback - Existing User Logged In');
} else {
$username = $user->getName(); // Use Facebook name as username
$password = wp_generate_password(); // Generate a random password
$user_id = wp_create_user($username, $password, $email);
if (!is_wp_error($user_id)) {
wp_update_user(['ID' => $user_id, 'display_name' => $username]);
$profile_picture_url = $user['picture']['url'];
update_user_meta($user_id, 'profile_picture', $profile_picture_url);
wp_set_auth_cookie($user_id, true);
} else {
// Handle user creation error
error_log('Facebook Callback - User Creation Error: ' . $user_id->get_error_message());
}
}
}
} else {
// Handle the case where the 'code' parameter is not set (e.g., user canceled login)
}
?>
<div id="signupModal" class="modal" style="display:none";>
<div class="modal-content" >
<?php echo $fb_login_btn; ?>
</div>
</div>
<?php
}
add_action('wp_footer', 'modal_tamplate_html');
here is my jquery script
if (!custom_vars.userIsLoggedIn) {
// Add an event listener for all links on the page
$('.single_add_to_cart_button.button.alt.wp-element-button').click(function(e) {
console.log("Site title clicked.");
// Check if the user is logged out
if ($('body.logged-out').length) {
$('#signupModal').show();
// Hide modal when close icon is clicked
$(".close").click(function(e) {
$("#signupModal").hide();
return false;
});
Top comments (0)