Skip to content

Instantly share code, notes, and snippets.

@arobbins
Last active January 23, 2018 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arobbins/6eccfbea544b626dd7295157d5a7808e to your computer and use it in GitHub Desktop.
Save arobbins/6eccfbea544b626dd7295157d5a7808e to your computer and use it in GitHub Desktop.
Custom order data example - AJAX receiver
<?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