• Resolved ollkar

    (@ollkar)


    Hi!

    Currently trying to implement a custom button on my clients website. Im trying to add a button on all items in admin order page. I want the button to clear the stock for that specific item.
    Have some code that somewhat works but it return wrong product id. How do i make sure that i get the right product id before updating the stock value?

    Current code:

    
    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 '<button type="button" onclick="document.post.submit();" class="button generate-items" style="background: red; color: white;">' . __( 'T?m lagersaldo', 'hungred' ) . 
                '<input type="hidden" value="'.$id.'" name="renew_order_single_product" /></button>';
        
    }
    
    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);
            
             }
        }
    }
    
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Custom button admin order items’ is closed to new replies.