Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.