John Davidson

php - WP All Import Function To Deal With Serialised Array Data

0 comments
Message:


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?



My Answer:
You can achieve this by first retrieving the existing serialised array data, then modifying the stock level for location 754, and finally updating the database with the modified data. Here is a sample PHP function that you can use with WP All Import:

php
function update_stock_level_for_location_754($product_id, $new_stock_level) {
// Get the existing serialised array data
$existing_stock_data = get_post_meta($product_id, 'woocommerce_multi_inventory_inventories_stock', true);

// Unserialize the data
$stock_data = unserialize($existing_stock_data);

// Update the stock level for location 754
$stock_data[754] = $new_stock_level;

// Serialize the updated data
$updated_stock_data = serialize($stock_data);

// Update the database with the modified data
update_post_meta($product_id, 'woocommerce_multi_inventory_inventories_stock', $updated_stock_data);
}

// Usage example
$product_id = 123; // Replace with the actual product ID
$new_stock_level = 10; // Replace with the new stock level for location 754
update_stock_level_for_location_754($product_id, $new_stock_level);


You can call this function within your WP All Import import process to update the stock level for location 754 without affecting the other locations. Just make sure to replace the placeholder values with the actual product ID and stock level.

Rate this post

3 of 5 based on 2147 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us