Created
February 20, 2018 15:10
-
-
Save trey8611/6d75488d098e30b64fe3252ae1dbb5f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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 ); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment