Question: I've created a custom user role and am attempting to change the users role from CUSTOMER to ADVOCATE on purchase of a particular product (using WooCommerce). I'm really close but struggling to get the correctly serialized data into my table: 

```
$order = new WC_Order( $order_id );
$items = $order->get_items();

$new_role = 'Array([advocate] => 1)';
$data = serialize( $new_role ); 

if ( $product_id == '786' ) {
    update_user_meta( $order->user_id, 'wp_capabilities', $data );
}

```

The correct table is being populated at the correct time but as

```
s:30:"s:22:"Array([advocate] => 1)";";

```

rather than what I need it to be which is

```
a:1:{s:8:"advocate";b:1;}

```

Where is my serialization tripping up?

Answer: 
I think you are not handling the array part correctly.
The right syntax is - 

```
$new_role = array("advocate" => 1);

```

The syntax that you have used is shown when you print some array on your screen but it should be written in above format in your PHP code. Currently, you are capturing it as a string and not an array.
In addition to syntax correction from WisdmLabs above I also found that I was effectively serialising the string TWICE as it will be automatically serialized when using update\_user\_meta. Not sure at which point I started serialising it myself but apparently that was completely unnecessary. 

<http://codex.wordpress.org/Function_Reference/update_user_meta>