WP All Import Function To Deal With Serialised Array Data

826 Views Asked by At

I am using a multi inventory plug-in to store stock levels of products in Woocommerce between 3 locations - they are named 754, 753 and 752

They are stored in database in a serialised array like this:

Key: woocommerce_multi_inventory_inventories_stock

Value: a:3:{i:754;s:0:”5”;i:753;s:0:”5”;i:752;s:0:”5”;}

I am trying to use WP All Import to import stock levels into just 1 of these locations - 754 and leave the other two as they are. WP All Import can import into this serialised array but it then overrides the other 2 locations when it changes 754.

Would anyone know how a php function to use with WP All Import in order to change ONLY the stock level in 754 without adjusting the other 2?

1

There are 1 best solutions below

0
On

https://www.codegrepper.com/code-examples/php/php+serialize+to+array+online

<!DOCTYPE html>
<html>
<body>

<?php
 $array["a"] = "Foo";
    $array["b"] = "Bar";
    $array["c"] = "Baz";
    $array["d"] = "Wom";

    $str = serialize($array);
    print $str . "\n";
 
?>

</body>
</html>

Using the above method you can create serialize data for the custom field using the key available in wp all import options.

Explanation of serialize array is as follows:

Array ( [1] => elem 1 [2] => elem 2 [3] => elem 3 ) 

a:3:{i:1;s:6:"elem 1";i:2;s:6:"elem 2";i:3;s:7:" elem 3";}

In case you were still curious about the "secondary output", its fairly simple: a = array, 3 = of size three elements within the {}'s. inside that, you have i=integer/index equalling 1, string of len 6 equalling "elem 1", integer equalling 2.. etc etc.. Its fairly clear when you read it like that. You can imagine multiple levels of arrays/objects being easily contained within, however modification is very unwise, you should really unserialize modify then serialize to ensure consistency.

Here you can find more details:

URL: How to use php serialize() and unserialize()