Need help updating my product inventory on the WooCommerce manage orders page. I currently have some code that works, but it removes inventory from the wrong product. Does anyone know why?
add_action( 'woocommerce_after_order_itemmeta', 'action_woocommerce_order_item_add_button', 10, 2); function action_woocommerce_order_item_add_button($item_id, $item) { $product = $item->get_product(); $id = $item->get_product_id(); echo ''; } add_action('save_post', 'renew_save_again', 10, 3); function renew_save_again($post_id, $post, $update){ $slug = 'shop_order'; if(is_admin()){ // If this isn't a 'woocommercer order' post, don't update it. if ( $slug != $post->post_type ) { return; } if(isset($_POST['renew_order_single_product']) && $_POST['renew_order_single_product']){ //removes stock for specified product global $woocommerce; $quantity = 0; $product_id = $_POST['renew_order_single_product']; $woocmmerce_instance = new WC_Product($product_id); $new_quantity=wc_update_product_stock( $woocmmerce_instance, $quantity); } } }
The problem is, you added a button and an input box after each order item, so you have multiple hidden input boxes with the same name, it either gets the first hidden input box, or the last one, And that's what's updated, you need to change your logic and just add a hidden input box and change the value of the hidden input box on button click and then submit the form.