Hi,
This is an example for updating existing products. Of course, you can do this for new products as well, but this time I will explain how to update existing ones.
In order to have this work out, your products need to have all those locations already assigned. You can also assign locations to products using WP All import, but that is a different topic.
Lets begin now.
In WP All import, when you initiate an import, you will find an option called “Function editor”. There you can add your PHP code snippet.
But before you do that, you need to identify your custom fields for all your locations you are using for your stock.
Usually those fields are looking something like this: _stock_at_1234
This number part (1234) you will find under WooCommerce >> SLW settings. Those numbers are actually your stock location IDs. So they are different from website to website.
Lets pretend for this example that my stock location IDs are: 1234 and 5678, so I am using 2 stock locations.
So, now when we have all we need, this is the code snippet we have to implement in WP all import:
<?php
add_action( 'pmxi_saved_post', 'my_fix_stock', 10, 1 );
function my_fix_stock( $id ) {
$product = wc_get_product( $id );
if ( ! $product ) return;
$stock1 = $product->get_meta( '_stock_at_1234', true );
$stock2 = $product->get_meta( '_stock_at_5678', true );
if ( empty( $stock1 ) ) $stock1 = 0;
if ( empty( $stock2 ) ) $stock2 = 0;
$product->set_stock_quantity( $stock1 + $stock2 );
$product->save();
}
?>
You change these dummy values with your real ones, paste the snippet and hit save. Also, adjust this code for all your locations accordingly.
Upper, in the inventory tab, do not put any values in your stock quantity field, set manage stock to on and stock status has to be set automatically.
Those are all actions you need to take in that step. In the next step, you need to tell WP all import how to run the update, how to match the records.
So, records in your file will be matched with products on your site based on SKUs, product IDs, or whatever you are using as a special custom field (EANs, MPNs…)
You are updating existing products, so you set that checkbox and then you choose which data to update.
For this, you are using only custom fields and you are setting the option called: update only these custom fields and to leave the rest alone.
Custom fields are:
_stock_at_1234
_stock_at_5678
_stock
_manage_stock
Again, do not forget to change dummy numbers with your real values.
Save the settings, go to the next step and run the import.
Hopefully this helps.
-
This reply was modified 3 years, 9 months ago by
Elliot.
-
This reply was modified 3 years, 9 months ago by
Elliot.