Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
897 views
in Technique[技术] by (71.8m points)

php - WooCommerce serialized meta value array in wp_postmeta table

I am inserting items and attributes via SQL. Everything works fine, but i cannot figure it out how the _product_attributes in wp_postmeta

I understand the logic, except the s:7:"pa_hrup" <-- where do i get 7 and s:31:"pa_kapaciteta-rezervoarja-za-go" <-- where do i get 31..

a:2:{s:7:"pa_hrup";a:6:{s:4:"name";s:7:"pa_hrup";s:5:"value";s:0:"";s:8:"position";i:0;s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}s:31:"pa_kapaciteta-rezervoarja-za-go";a:6:{s:4:"name";s:31:"pa_kapaciteta-rezervoarja-za-go";s:5:"value";s:0:"";s:8:"position";i:1;s:10:"is_visible";i:1;s:12:"is_variation";i:0;s:11:"is_taxonomy";i:1;}}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is a meta data string serialized array and s:31 is the length of pa_kapaciteta-rezervoarja-za-go in this case. a:6 is the number ok key/value pairs items for each array (or sub array).

So a seriallized array:

  • always start with a: for array, plus the number of items key/value pairs in it and :.
  • then { for items start
  • then s: for string, plus the lenght of the string item (key or value) and :, plus the key or value string.
  • then ; to separate each key or value component
  • then } for items end

Serialized arrays, can be unserialized using WordPress maybe_unserialize() (or unserialize() in PHP).
A normal array can be serialized using using WordPress maybe_serialize() (or serialize() in PHP).

Wordpress functions like add_post_meta() or update_post_meta() will always serialize an arrays before saving a meta_value in wp_postmeta table.

Same thing for WooCommerce with some related WC_Data method as save() on CRUD Objects and all related data stores classes.

using maybe_unserialize() on your serialized string array will give:

$values = array( 
    'pa_hrup' => array(
        'name'         => 'pa_hrup',
        'value'        => '',
        'position'     => '0',
        'is_visible'   => '1',
        'is_variation' => '0',
        'is_taxonomy'  => '1'
    ),
    'pa_kapaciteta-rezervoarja-za-go' => array(
        'name'         => 'pa_kapaciteta-rezervoarja-za-go',
        'value'        => '',
        'position'     => '1',
        'is_visible'   => '1',
        'is_variation' => '0',
        'is_taxonomy'  => '1'
    )
);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...