2

I am working on wordpress with woocommerce and using WCK plugin for custom fields. I am creating products programatically.

I need to save custom fields data as array in database progrmatically. But it is not saving correctly and not showing the custom field values in backend for products. I am using this code.

 $data= array(
    'alternative-product-names' => $alternative_pname,
    'manufacturers-part-number' => $manufature_park_number,
    'currently-packaged'=> $currently_packaged,
    'other-package-options' => $other_pkg_opt,
    'inner-pack-qty' => $inner_pack_qty,
    'inner-pack-dimensions' => $inner_pck_dimension,
    'packaging-picture'=>''
    );

update_post_meta( $post_id, 'productextrainfo1234', $data );

I need to save data in this format:

a:1:{i:0;a:15:{s:25:"alternative-product-names";s:4:"fgfg";s:25:"manufacturers-part-number";s:4:"gffg";s:18:"currently-packaged";s:4:"fgfg";s:21:"other-package-options";s:4:"fgfg";s:14:"inner-pack-qty";s:4:"fggf";s:21:"inner-pack-dimensions";s:17:"packaging-picture";s:3:"561";}}

1 Answer 1

4

After testing your code:

First, as you have 7 lines of key/values in your array, so your serialized string can't begin with a:1:{i:0;a:15:{ … but instead with a:1:{i:0;a:7:{ ….

Second, You need to embed your array in an empty array to get the correct format like you want:
a:1:{i:0;a:7:{ … }};.

So your code will have to be like this:

$data= array( 
    array(
        'alternative-product-names' => $alternative_pname,
        'manufacturers-part-number' => $manufature_park_number,
        'currently-packaged'        => $currently_packaged,
        'other-package-options'     => $other_pkg_opt,
        'inner-pack-qty'            => $inner_pack_qty,
        'inner-pack-dimensions'     => $inner_pck_dimension,
        'packaging-picture'         => ''
    )
);

update_post_meta( $product_id, 'productextrainfo1234', $data );

This way you will get this serialized data value in your database:

a:1:{i:0;a:7:{s:25:"alternative-product-names";N;s:25:"manufacturers-part-number";N;s:18:"currently-packaged";N;s:21:"other-package-options";N;s:14:"inner-pack-qty";N;s:21:"inner-pack-dimensions";N;s:17:"packaging-picture";s:0:"";}}

Instead of:

a:7:{s:25:"alternative-product-names";N;s:25:"manufacturers-part-number";N;s:18:"currently-packaged";N;s:21:"other-package-options";N;s:14:"inner-pack-qty";N;s:21:"inner-pack-dimensions";N;s:17:"packaging-picture";s:0:"";}

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot.you saved my lot of time.
I am getting same problem when saving products attribute.Can you please help?
You may ask another similar question providing the code you are using and the needed output format… I only can help on delimited case. So more information you give, better is the help…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.