Last active
January 23, 2018 17:51
-
-
Save arobbins/6eccfbea544b626dd7295157d5a7808e to your computer and use it in GitHub Desktop.
Custom order data example - AJAX receiver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Step 2. Save any dynamic values to the users Session | |
We're calling this function from the frontend javascript via the 'save_dynamic_values_to_session' AJAX action. | |
*/ | |
function save_dynamic_values_to_session() { | |
// Gain access to the global $_SESSION variable | |
if (session_status() == PHP_SESSION_NONE) { | |
session_start(); | |
} | |
// Clearing any existing values set to our Session variable first | |
unset($_SESSION['dynamic_values']); | |
// Checking to see if the data was passed to the server correctly | |
if (isset($_POST['dynamicValues']) && $_POST['dynamicValues']) { | |
// Set our session variable to the data coming from the client | |
$_SESSION['dynamic_values'] = $_POST['dynamicValues']; | |
} else { | |
wp_send_json_error('No dynamic values found'); | |
} | |
wp_send_json_success(); | |
} | |
add_action( 'wp_ajax_save_dynamic_values_to_session', 'save_dynamic_values_to_session'); | |
add_action( 'wp_ajax_nopriv_save_dynamic_values_to_session', 'save_dynamic_values_to_session'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment