I have been trying to save a parameter from my url example.com/?lc=Business1 to a session, and then use this variable to auto-select from a list at checkout in woocommerce.
The dropdown always displays "None" at the moment.
This is my code:
//Start Session
add_action('init', 'myStartSession', 1);
add_action('wp_logout', 'myEndSession');
add_action('wp_login', 'myEndSession');
function myStartSession() {
if(!session_id()) {
session_start();
}
}
function myEndSession() {
session_destroy ();
}
//* Write url parameter to session variable lc
if (isset($_GET['lc'])) {
$_SESSION['lc'] = $_GET['lc'];
}
//* Add Local C drop-down to checkout
add_action('woocommerce_after_order_notes', 'local_c_add_checkout');
function local_c_add_checkout( $checkout ) {
$data = WC()->session->get('lc');
woocommerce_form_field( 'C_form', array(
'type' => 'select',
'class' => array( 'drop-champ' ),
'label' => __( 'Local C' ),
'default' => 'lc',
'options' => array(
'blank' => __( 'None' ),
'Business1' => __( 'Business 1' ),
'Business2' => __( 'Business 2')
)
),
$checkout->get_value( 'C_form' ));
}
I'm sure there is something I am doing wrong/overlooking? and would appreciate guidance as to how to get my code to work.