I am trying to grab data from an external form using PHP session and setting it to Woocommerce session. Here is my form code with session
<form action="" method="POST">
<input type="text" name="ninsta" id="inuser">
<input type="submit" name="continue" value="Continue" />
</form>
<?php
session_start();
if(isset($_REQUEST['continue'])){
$_SESSION['ninsta'] = $_POST['ninsta'];
header('Location: http://localhost/wps/wptest/shop/');
}
// Test if your are on Back Office, WC()->session isn't set
if( !is_admin() ){
$indata = $_SESSION['ninsta'];
WC()->session->set( 'instagramuser' , $indata );
$retrive_data = WC()->session->get( 'instagramuser' );
}
After that I have another file named custom-data-product-single-to-order.php Where I am sending this session data to woocommerce cart and checkout page. At that page my code is like below
For Cart page I am sending data with below code
function wdm_add_item_meta($item_data, $cart_item)
{
if(!isset($_SESSION))
{
session_start();
}
// Test if your are on Back Office, WC()->session isn't set
if( !is_admin() ){
$indata = $_SESSION['ninsta'];
WC()->session->set( 'instagramsuer' , $indata );
$retrive_data = WC()->session->get( 'instagramsuer' );
}
if(array_key_exists('insuser', $cart_item) && array_key_exists('nmlike', $cart_item))
{
$username_details = $cart_item['insuser'];
$like_details = $cart_item['nmlike'];
$item_data[] = array(
'key' => 'Instagram Username',
'value' => $username_details
);
$item_data[] = array(
'key' => 'Like Number',
'value' => $like_details
);
$item_data[] = array(
'key' => 'Session Instagram Username',
'value' => $retrive_data
);
}
return $item_data;
And for Adding Custom Details as Order Line Items below is the code
add_action( 'woocommerce_checkout_create_order_line_item', 'wdm_add_custom_order_line_item_meta',10,4 );
function wdm_add_custom_order_line_item_meta($item, $cart_item_key, $values, $order)
{
if(array_key_exists('insuser', $values) && array_key_exists('nmlike', $values))
{
$item->add_meta_data('Instagram Username',$values['insuser']);
$item->add_meta_data('Like Number',$values['nmlike']);
$item->add_meta_data('Session Instagram Username',$values['$retrive_data']);
}
}
So Its all working good. First two fields "Instagram Username" and ""Like Number"" is inputted in product single page by user and the Third Field "Session Instagram Username" is taking the value from the session. So The session data shows in cart page and checkout page also. Check the below images
So far its all good. But when the order is made, that session data is not carried to Order details and in order details it only shows these two fields that’s inputted by user at product single page but the session data don’t shows in order detail page
Not sure What I am doing wrong. Any help to fix this will be appreciated