trey
2/20/2018 - 3:10 PM

update-stock-in-orders-import.php

<?php
add_action( 'pmxi_saved_post', 'my_update_stock', 10, 3 );

function my_update_stock( $order_id, $xml_data, $is_update ) {

	$import_id = ( isset( $_GET['id'] ) ) ? $_GET['id'] : $_GET['import_id'];
	
	if ( $import_id == "7" && $is_update == false ) { // change "7" to your import ID
		if ( $order = wc_get_order( $order_id ) ) {
			$items = $order->get_items();
			foreach( $items as $item ) {
				if ( isset( $item['product_id'] ) ) {
					$current_stock = get_post_meta( $item['product_id'], '_stock', true );
					$prod_id = $item['product_id'];
				} elseif( isset( $item['variation_id'] ) ) {
					$current_stock = get_post_meta( $item['variation_id'], '_stock', true );
					$prod_id = $item['variation_id'];
				}
				$new_stock = $current_stock - $item['qty'];
				if ( function_exists( 'wc_update_product_stock' ) ) {
					wc_update_product_stock( $prod_id, $new_stock );
				}
			}
		}
	}
}