I just noticed that your function above refers a "$attributes" which doesn't look like it's defined in that function. Hopefully your working copy of that function fixed that otherwise value => NULL which probably is why your update failed.
On the other hand I think your $product_attributes is badly defined (I'm not 100% sure, though).
I created a custom attribute called "my_attr_name" with the value "my_custom_value" and checked the MySQL database to see how it is stored. It is stored inside table "wp_postmeta" with meta_key=_product_attributes and the "meta_value" serialized. So I copied that serialized value and tried to unserialize it just to see how it should be defined in the first place. It looks like yours except that your attribute key (ie. 'type') and the value for the first key of the subarray should match.
This is my example:
Array
(
[john-doe] => Array
(
[name] => 'John Doe'
[value] => my_custom_value
[position] => 0
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 0
)
)
As you see the array's key john-doe ~ first key of the subarray (which is also John Doe). In your case:
type <> htmlspecialchars(stripslashes('Class'))
It worth mentioning that if your 'name' => 'John Doe' then the master key should be something like 'john-doe' (lower-case and minus as space replacement). I don't know if this is a must but at least this is the way WP 4.2.2 works.
As for the other keys:
- is_visible: if the attribute is visible on the product page
- position : the order of attribute (0=first,1=next attribute,etc); I think this one should be always defined otherwise you risk to overwrite the first (0) index; so you should find out how many they are before adding a new one (see $meta=get_post_meta (get_the_ID(), '_product_attributes'))
- is_variation: probably smth. to do with variable product (?!)
- is_taxonomy: I think is should be always false as the meta_key is not regarded as taxonomy (just a guess)
woocommerce_cart_loaded_from_session,woocommerce_before_cart, and (if using AJAX)woocommerce_ajax_added_to_cartget used to using$woocommerce->sessionand$woocommerce->cartto update in two locations if you're using AJAX. These are all necessary when programmatically adjusting attributes in a cart item. - Rebecca Dessonville