{"id":13795,"date":"2023-01-05T20:41:11","date_gmt":"2023-01-05T19:41:11","guid":{"rendered":"https:\/\/www.welaunch.io\/en\/?post_type=faq&#038;p=13795"},"modified":"2024-02-27T18:14:33","modified_gmt":"2024-02-27T17:14:33","slug":"update-wc-multi-inventories-using-wp-all-import","status":"publish","type":"faq","link":"https:\/\/www.welaunch.io\/en\/knowledge-base\/faq\/update-wc-multi-inventories-using-wp-all-import\/","title":{"rendered":"Update WC Multi Inventories using WP All Import"},"content":{"rendered":"<p>Beside our REST API you can also use WP All import to import multiple inventories into your WooCommerce products. We decide between having one inventory stock file or multiple files.<\/p>\n<h2>One Inventory Import file<\/h2>\n<p>To import one inventory file, you need to use custom fields in WP All Import. Set the name to &#8220;woocommerce_multi_inventory_inventories_stock&#8221;. Then make sure it is an array and click to specify the key and value element like in the image below.<\/p>\n<p>Note: the key is the ID of the inventory term.\u00a0<\/p>\n\t<div class=\"img has-hover x md-x lg-x y md-y lg-y\" id=\"image_1936739264\">\n\t\t<a class=\"image-lightbox lightbox-gallery\" title=\"\" href=\"https:\/\/www.welaunch.io\/en\/wp-content\/uploads\/sites\/8\/2023\/01\/woocommerce-import-multi-inventories-stock-in-wp-all-import-1024x347.png\">\t\t\t\t\t\t<div class=\"img-inner box-shadow-2 dark\" >\n\t\t\t<img loading=\"lazy\" decoding=\"async\" width=\"1020\" height=\"346\" src=\"https:\/\/www.welaunch.io\/en\/wp-content\/uploads\/sites\/8\/2023\/01\/woocommerce-import-multi-inventories-stock-in-wp-all-import-1024x347.png\" class=\"attachment-large size-large\" alt=\"woocommerce import multi inventories stock in wp all import\" srcset=\"https:\/\/www.welaunch.io\/en\/wp-content\/uploads\/sites\/8\/2023\/01\/woocommerce-import-multi-inventories-stock-in-wp-all-import-1024x347.png 1024w, https:\/\/www.welaunch.io\/en\/wp-content\/uploads\/sites\/8\/2023\/01\/woocommerce-import-multi-inventories-stock-in-wp-all-import-300x102.png 300w, https:\/\/www.welaunch.io\/en\/wp-content\/uploads\/sites\/8\/2023\/01\/woocommerce-import-multi-inventories-stock-in-wp-all-import-768x260.png 768w, https:\/\/www.welaunch.io\/en\/wp-content\/uploads\/sites\/8\/2023\/01\/woocommerce-import-multi-inventories-stock-in-wp-all-import-1536x521.png 1536w, https:\/\/www.welaunch.io\/en\/wp-content\/uploads\/sites\/8\/2023\/01\/woocommerce-import-multi-inventories-stock-in-wp-all-import-2048x694.png 2048w, https:\/\/www.welaunch.io\/en\/wp-content\/uploads\/sites\/8\/2023\/01\/woocommerce-import-multi-inventories-stock-in-wp-all-import-600x203.png 600w, https:\/\/www.welaunch.io\/en\/wp-content\/uploads\/sites\/8\/2023\/01\/woocommerce-import-multi-inventories-stock-in-wp-all-import-510x173.png 510w\" sizes=\"auto, (max-width: 1020px) 100vw, 1020px\" \/>\t\t\t\t\t\t\n\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/a>\t\t\n<style>\n#image_1936739264 {\n  width: 100%;\n}\n<\/style>\n\t<\/div>\n\t\n\t<div id=\"gap-172986581\" class=\"gap-element clearfix\" style=\"display:block; height:auto;\">\n\t\t\n<style>\n#gap-172986581 {\n  padding-top: 30px;\n}\n<\/style>\n\t<\/div>\n\t\n<p>Then you have to add the following code into your child theme &gt; functions.php file. This ensures, that inventory terms &amp; taxonomies will get updated correctly during import.<\/p>\n<pre>function welaunch_update_inventory_stock( $post_id, $meta_key, $meta_value) {\n\n    if($meta_key == \"woocommerce_multi_inventory_inventories_stock\") {\n\n        $product = wc_get_product($post_id);\n        if(!$product) {\n            return false;\n        }\n\n        if(!is_array($meta_value)) {\n            die('Inventories data must be an array');\n        }\n\n        $meta_value = array_filter($meta_value);\n\n        $productId = $product-&gt;get_id();\n        if($product-&gt;get_type() == \"variation\") {\n            $parentProduct = wc_get_product( $product-&gt;get_parent_id() );\n            if(!$parentProduct) {\n                return false;\n            }\n            $product = $parentProduct;\n        }\n\n        $existingTerms = get_the_terms($product-&gt;get_id(), 'inventories');\n        if(!empty($existingTerms)) {\n\n            $existingTerms = wp_list_pluck($existingTerms, 'term_id');\n            $existingTerms = array_combine($existingTerms, $existingTerms);\n\n            foreach($meta_value as $inventory_id =&gt; $inventory_stock) {\n                if(empty($inventory_stock) &amp;&amp; isset($existingTerms[$inventory_id]) ) {\n                    unset($existingTerms[$inventory_id]);\n                } elseif($inventory_stock &gt; 0 &amp;&amp; !isset($existingTerms[$inventory_id]) ) {\n                    $existingTerms[$inventory_id] = $inventory_id;\n                }\n            }\n\n        } else {\n            $existingTerms = array_keys($meta_value);\n        }\n\n\n        wp_set_post_terms($productId, $existingTerms, 'inventories');\n\n        $product-&gt;save();\n    }\n\n}\nadd_action( 'pmxi_update_post_meta', 'welaunch_update_inventory_stock', 10, 3 );\n<\/pre>\n<h2>Importing Multiple Inventory Files<\/h2>\n<p>Having multiple imports and inventory files means you need to control the inventory ID manually. See the code below. Make sure you change the following parameters in this script:<\/p>\n<ul>\n<li>import id (the ID of the wp all import job)<\/li>\n<li>inventoryId (the inventory term ID you find at products &gt; inventories)<\/li>\n<li>stockKey (the stock key in your XML \/ CSV file)<\/li>\n<\/ul>\n<pre>function welaunch_update_inventory_stock( $post_id, $xml_node, $is_update ) {\n\n    \/\/ Retrieve the import ID.\n    $import_id = (int) wp_all_import_get_import_id(); \n\n    $inventoryId = 0; \n    $stockKey = '';\n\n    \/\/ inventory import ONE\n    \/\/ import id 359 = inventory 5957\n    if ( $import_id == 359 ) {\n        $inventoryId = 5957;\n        $stockKey = 'stock_amount';\n    \/\/ inventory import TWO\n    \/\/ import id 335 = inventory 5956\n    } elseif ( $import_id == 335 ) {\n        $inventoryId = 5956;\n        $stockKey = 'free_stock';\n    } \n\n    if(empty($inventoryId)) {\n        return;\n    }\n\n    \/\/ Convert SimpleXml object to array for easier use.\n    $record = json_decode( json_encode( ( array ) $xml_node ), 1 );\n\n    if(!isset($record[$stockKey])) {\n        return;\n    }\n\n    $stock = (int) $record[$stockKey];\n\n    $product = wc_get_product($post_id);\n    if(!$product) {\n        return false;\n    }\n\n    $existingInventoryStock = $product-&gt;get_meta('woocommerce_multi_inventory_inventories_stock');\n    if(empty($existingInventoryStock)) {\n        $existingInventoryStock = array(\n            $inventoryId =&gt; $stock\n        );\n    } else {\n        $existingInventoryStock[$inventoryId] = $stock;\n    }\n    \/\/ var_dump($existingInventoryStock);\n    \/\/ die();\n\n    $newTotalFrontendStock = 0;\n    foreach($existingInventoryStock as $inventoryId =&gt; $stockAmount) {\n        $frontend = get_term_meta($inventoryId, 'woocommerce_multi_inventory_frontend', true);\n        if($frontend) {\n            $newTotalFrontendStock += (int) $stockAmount;\n        }           \n    }\n\n\n    $product-&gt;set_stock_quantity($newTotalFrontendStock);\n    $product-&gt;update_meta_data('woocommerce_multi_inventory_inventories_stock', $existingInventoryStock);\n    $product-&gt;save();\n\n    \/\/ Update Terms\n    $productToGetTermsFrom = $product;\n    $productId = $product-&gt;get_id();\n    if($product-&gt;get_type() == \"variation\") {\n        $productToGetTermsFrom = wc_get_product( $product-&gt;get_parent_id() );\n        if(!$productToGetTermsFrom) {\n            return false;\n        }\n    }\n\n    $existingTerms = get_the_terms($productToGetTermsFrom-&gt;get_id(), 'inventories');\n    if(!empty($existingTerms)) {\n\n        $existingTerms = wp_list_pluck($existingTerms, 'term_id');\n        $existingTerms = array_combine($existingTerms, $existingTerms);\n\n        foreach($existingInventoryStock as $inventory_id =&gt; $inventory_stock) {\n            if(empty($inventory_stock) &amp;&amp; isset($existingTerms[$inventory_id]) ) {\n                unset($existingTerms[$inventory_id]);\n            } elseif($inventory_stock &gt; 0 &amp;&amp; !isset($existingTerms[$inventory_id]) ) {\n                $existingTerms[$inventory_id] = $inventory_id;\n            }\n        }\n\n    } else {\n        $existingTerms = array_keys($existingInventoryStock);\n    }\n\n\n    wp_set_post_terms($productToGetTermsFrom-&gt;get_id(), $existingTerms, 'inventories');\n\n    $product-&gt;save();\n\n}\nadd_action( 'pmxi_saved_post', 'welaunch_update_inventory_stock', 10, 3 );\n<\/pre>\n<p>Wp All import overrides all custom metas by default. To avoid this make sure you select: update all meta except: &#8220;woocommerce_multi_inventory_inventories_stock&#8221;. See below configuration example.<\/p>\n\t<div class=\"img has-hover x md-x lg-x y md-y lg-y\" id=\"image_2037603636\">\n\t\t\t\t\t\t\t\t<div class=\"img-inner dark\" >\n\t\t\t<img loading=\"lazy\" decoding=\"async\" width=\"1020\" height=\"777\" src=\"https:\/\/www.welaunch.io\/en\/wp-content\/uploads\/sites\/8\/2023\/05\/do-not-update-multi-inventory-meta-data-1-1024x780.png\" class=\"attachment-large size-large\" alt=\"\" srcset=\"https:\/\/www.welaunch.io\/en\/wp-content\/uploads\/sites\/8\/2023\/05\/do-not-update-multi-inventory-meta-data-1-1024x780.png 1024w, https:\/\/www.welaunch.io\/en\/wp-content\/uploads\/sites\/8\/2023\/05\/do-not-update-multi-inventory-meta-data-1-300x229.png 300w, https:\/\/www.welaunch.io\/en\/wp-content\/uploads\/sites\/8\/2023\/05\/do-not-update-multi-inventory-meta-data-1-768x585.png 768w, https:\/\/www.welaunch.io\/en\/wp-content\/uploads\/sites\/8\/2023\/05\/do-not-update-multi-inventory-meta-data-1-1536x1170.png 1536w, https:\/\/www.welaunch.io\/en\/wp-content\/uploads\/sites\/8\/2023\/05\/do-not-update-multi-inventory-meta-data-1-600x457.png 600w, https:\/\/www.welaunch.io\/en\/wp-content\/uploads\/sites\/8\/2023\/05\/do-not-update-multi-inventory-meta-data-1-510x389.png 510w, https:\/\/www.welaunch.io\/en\/wp-content\/uploads\/sites\/8\/2023\/05\/do-not-update-multi-inventory-meta-data-1.png 2042w\" sizes=\"auto, (max-width: 1020px) 100vw, 1020px\" \/>\t\t\t\t\t\t\n\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\n<style>\n#image_2037603636 {\n  width: 100%;\n}\n<\/style>\n\t<\/div>\n\t\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","product_cat":[],"class_list":["post-13795","faq","type-faq","status-publish","hentry","faq_topics-multi-inventory"],"_links":{"self":[{"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/faq\/13795","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/faq"}],"about":[{"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/types\/faq"}],"author":[{"embeddable":true,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/comments?post=13795"}],"version-history":[{"count":7,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/faq\/13795\/revisions"}],"predecessor-version":[{"id":17352,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/faq\/13795\/revisions\/17352"}],"wp:attachment":[{"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/media?parent=13795"}],"wp:term":[{"taxonomy":"product_cat","embeddable":true,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/product_cat?post=13795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}