I want to mark not registered customers that already bought a product using either a cookie, a session variable, or some other way.
I have attempted it with the code below, but this approach doesn't seem to be working. In the first approach, I try to save something. Then, I try to get the session cookie already in use by woocommerce to save it in the database as a unique identifier. After, I can read it out of the database to see if it's a new unique user or not.
add_action( 'woocommerce_payment_complete', 'so_payment_complete' );
add_action( 'woocommerce_order_status_completed', 'so_payment_complete' );
function so_payment_complete( $order_id){
setcookie('so_payment_complete', 1, time()+7200);
WC()->session->set( 'so_payment_complete',true );
$session = new WC_Session_Handler();
$session_data = $session->get_session_data();
file_put_contents(__DIR__ . '/return_products.txt','cookies:'.(print_r($_COOKIE,true)) . "
" ,FILE_APPEND );
file_put_contents(__DIR__ . '/return_products.txt','get_session_data:'.(print_r($session_data,true)) . "
" ,FILE_APPEND );
}
It seems the cookie from setcookie
never reaches the client. WC()->session->set
never reaches the database. WC()->session->get
cannot get it on other pages either.
$_COOKIE
and $session_data
arrays are empty.