2

I am not sure why but i am trying to unset session after to add Custom data to Woo Commerce cat but its not working. Here is my code what i am trying.

add_filter('woocommerce_add_cart_item_data','wdm_add_item_data',11,2); 
if(!function_exists('wdm_add_item_data')){
  function wdm_add_item_data($cart_item_data,$product_id){
   global $woocommerce;
   session_start();  
    if (isset($_SESSION['custom_user_data'])) {
     $option = $_SESSION['custom_user_data'];
     $new_value = array('wdm_user_custom_data_value' => $option);  
    }
    if(empty($option)){
        return $cart_item_data;
    }else{    
        if(empty($cart_item_data)){
            return $new_value;
        }else{
            return array_merge($cart_item_data,$new_value);
        }
    }
    unset($_SESSION['custom_user_data']);
  }
}

But unset session is not working here. Anybody help me?? Thanks

Mr Shabir
  • 173
  • 3
  • 16
  • Try looking at this answer for how to properly start a session: https://stackoverflow.com/a/21952510/8823906. Then if you still want to use the global session use it. WC has its own session handling, so since you are already using WC why not use their session class `WC()->session->set([key], [value]);` – VanboDevelops Nov 07 '17 at 09:01

1 Answers1

1

You should need to it unset before returning anything in your filter hooked function…
Because after is too late.

So you will rearrange your code:

    ## ==> Unset before
    unset($_SESSION['custom_user_data']);

    ## ==> Return after
    if(empty($option)){
        return $cart_item_data;
    }else{    
        if(empty($cart_item_data)){
            return $new_value;
        }else{
            return array_merge($cart_item_data,$new_value);
        }
    }

Now instead you could use dedicated Woocommerce WC_Session this way:

1) Set the data in WC_Sessions:

$value = "the value to be set";
if( empty( WC()->session->get( 'custom_user_data' ) ) )
     WC()->session->set( 'custom_user_data', $value );

2) Add to cart the session data with the product:

add_filter( 'woocommerce_add_cart_item_data', 'custom_user_cart_item_data', 11, 2 );
function custom_user_cart_item_data( $cart_item_data, $product_id ){
    $cart_session_custom_data = WC()->session->get('custom_user_data');

    if( empty($cart_session_custom_data) ) return $cart_item_data;

    // Set the data
    $cart_item_data['custom_data']['user_value'] = $cart_session_custom_data;
    $cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );

    return $cart_item_data;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    Thanks for this. Works fine, But my problem was other. I was using two ajax function for add product, One was for woocommerce and other was our custom function. Now i remove woo commerce function and i just used one our custom function. And My problem is solved. – Mr Shabir Nov 07 '17 at 13:35