WP All Import stock ovewritte

I’m currently using WP All Import to import data (specifically stock quantity). When I import the stock quantity it overwrites my current data, and what I would like for it to do is to update data. Lets say WooCommerce had 5 and my CSV sheet has 5, I would like those two values to add (equaling 10).

I did email WP All Import support to provide some indication on what was required to get the above to work, and here is their:

Read More

“If you are good with code you can make that work using a custom post-processing function. Simply import your stock value into a placeholder custom field. Then through a function attached to the “pmxi_saved_post” action you would access both the actual stock custom field and the placeholder one. Add the values of both and update the final stock.
http://www.wpallimport.com/documentation/developers/action-reference/“;

I have no clue where to start. Any input would be appreciated it.

Related posts

3 comments

  1. The solution is right front of your.

    Approach:

    • Add the below function into the theme’s functions.php file

      add_action(‘pmxi_saved_post’, ‘wdm_post_saved’, 10, 1);

      function wdm_post_saved($id) {

      $original_stock = get_post_meta($id, ‘_stock’, true);
      $new_stock = get_post_meta($id, ‘_custom_stock_placeholder’, true);

      $combined_stock= $original_stock + $new_stock

      update_post_meta($id, ‘_stock’, $combined_stock);

      }

    • Add another custom field from dashboard or while using WP ALL IMPORT say ‘_custom_stock_placeholder’ to the products.

    • While using WP ALL IMPORT assign the new stock value to the above mentioned custom field rather than original ‘_stock’ field.

    Rest the function will calculate and do the needful for you.

  2. I have the same needs. But trying to manage it with the wp all import “orders” option. When importing new orders from wp all import the stock don’t change. But WP all import support gave me this function to add into the settings :

    <?php
    
    add_action( 'pmxi_saved_post', 'my_update_stock', 10, 1 );
    
    function my_update_stock( $order_id ) {
    
    $import_id = ( isset( $_GET['id'] ) ) ? $_GET['id'] : $_GET['import_id'];
    
     if ( $import_id == "37" ) {
        $order = new WC_Order( $order_id );
        $items = $order->get_items();
        foreach( $items as $item ) {
            $current_stock = get_post_meta( $item['product_id'], '_stock', true );
            $new_stock = $current_stock - $item['qty'];
            update_post_meta( $item['product_id'], '_stock', $new_stock );
        }
    }
    }
    
    ?>
    

    You’ll want to change the import ID from to match the ID of your order import (I’ve set 37 here as an example).

    It “almost” works. I mean new orders affect the stocks, but only the main product’s one. My orders are about variation products, and with this tip the variation stock don’t change. I made a short video here :

    http://moroccanye.cluster011.ovh.net/stock/DEBUG_ORDER_IMPORT_SOFIA.mov

    better than my poor english. If anybody have an idea to improve that code for variations products orders to affect variations products stock… many thanks.

Comments are closed.