admin管理员组

文章数量:1291014

I have some custom meta data bound to a product which I want to increase the count of when an order is refunded. When a refund is issued you can of course only refund some or all of the total quantity ordered. I'm looking to fetch only that specific quantity refunded.

If you have an order of 5 of the same item and refund 1 my function returns 1, if you then refund 2 more after that initially 1 has been refunded. My function returns 3 which is the total not the specific number in that session.

Would it be possible to grab only the quantity for that specific action, not the total?

function action_woocommerce_order_refunded( $order_id, $refund_id ) 
{ 
    //init
    $restock_items = $_POST['restock_refunded_items'];
    $order = wc_get_order( $order_id );
    $order_refunds = $order->get_refunds();


// If restock isn't checked skip
if($restock_items == 'true'){
    foreach( $order_refunds as $refund ){
        foreach( $refund->get_items() as $item_id => $item ){

            $refunded_quantity = $item->get_quantity();         // returns negative number e.g. -1
            $refunded_quantity = substr($refunded_quantity, 1); // trim the negative "-" from the string

            $usa_stock_toggle = get_post_meta($item->get_product_id() , '_usa_stock_toggle', true);

            if(!empty($usa_stock_toggle)){
                $usa_stock_levels = get_post_meta($item->get_product_id(), '_usa_stock_field', true); // Get stock level
                $updated_usa_stock_levels = $usa_stock_levels + $refunded_quantity; // Work out new stock level
                update_post_meta($item->get_product_id() , '_usa_stock_field', $updated_usa_stock_levels ); // Update stock level
            }
            
        }
    }
}
}
add_action( 'woocommerce_order_refunded', 'action_woocommerce_order_refunded', 10, 2 ); 

I have some custom meta data bound to a product which I want to increase the count of when an order is refunded. When a refund is issued you can of course only refund some or all of the total quantity ordered. I'm looking to fetch only that specific quantity refunded.

If you have an order of 5 of the same item and refund 1 my function returns 1, if you then refund 2 more after that initially 1 has been refunded. My function returns 3 which is the total not the specific number in that session.

Would it be possible to grab only the quantity for that specific action, not the total?

function action_woocommerce_order_refunded( $order_id, $refund_id ) 
{ 
    //init
    $restock_items = $_POST['restock_refunded_items'];
    $order = wc_get_order( $order_id );
    $order_refunds = $order->get_refunds();


// If restock isn't checked skip
if($restock_items == 'true'){
    foreach( $order_refunds as $refund ){
        foreach( $refund->get_items() as $item_id => $item ){

            $refunded_quantity = $item->get_quantity();         // returns negative number e.g. -1
            $refunded_quantity = substr($refunded_quantity, 1); // trim the negative "-" from the string

            $usa_stock_toggle = get_post_meta($item->get_product_id() , '_usa_stock_toggle', true);

            if(!empty($usa_stock_toggle)){
                $usa_stock_levels = get_post_meta($item->get_product_id(), '_usa_stock_field', true); // Get stock level
                $updated_usa_stock_levels = $usa_stock_levels + $refunded_quantity; // Work out new stock level
                update_post_meta($item->get_product_id() , '_usa_stock_field', $updated_usa_stock_levels ); // Update stock level
            }
            
        }
    }
}
}
add_action( 'woocommerce_order_refunded', 'action_woocommerce_order_refunded', 10, 2 ); 
Share Improve this question asked Jun 19, 2020 at 13:58 SamXronnSamXronn 1951 gold badge3 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I was looking for a solution to the same problem - I couldn't find any help with this online but I was able to figure it out myself, so for anyone else here is the solution:

Change the hook from woocommerce_order_refunded to woocommerce_order_partially_refunded. This will only fire when an order is partially refunded. If you refund the full remaining quantity of items in the order then the order status will change to Refunded and the woocommerce_order_refunded hook will fire instead.

Below is the working code for this question. Be sure to click the Recalculate button on the Edit Order screen after processing a partial refund - I discovered a bug in WooCommerce when issuing additional partial refunds separately for a different line item in the same order - the original refund amount will get changed incorrectly. The recalculate button fixes this but only if you press it before issuing proceeding partial refunds.

function action_woocommerce_order_refunded( $order_id, $refund_id )
{ 
    //init
    $restock_items = $_POST['restock_refunded_items'];
    
    // If restock isn't checked skip
    if( $restock_items == 'true' ) {
        
        $refund = wc_get_order( $refund_id );
        $refunded_items = $refund->get_items();
        
        foreach( $refunded_items as $refunded_item_id => $refunded_item ) {
            
            $refunded_quantity = $refunded_item->get_quantity(); // returns negative number e.g. -1
            $refunded_quantity = $refunded_quantity *-1; // convert to positive number
            $refunded_product_id = $refunded_item->get_product_id();
            $usa_stock_toggle = get_post_meta( $refunded_product_id, '_usa_stock_toggle', true );
            
            if( !empty( $usa_stock_toggle ) ){
                $usa_stock_levels = get_post_meta( $refunded_product_id  '_usa_stock_field', true ); // Get stock level
                $updated_usa_stock_levels = $usa_stock_levels + $refunded_quantity; // Work out new stock level
                update_post_meta( $refunded_product_id, '_usa_stock_field', $updated_usa_stock_levels ); // Update stock level
            }
        }
    }
}
add_action( 'woocommerce_order_partially_refunded', 'action_woocommerce_order_refunded', 10, 2 );

You would need to calculate the difference of a refunded quantity when an order changes from partially refunded to fully refunded, I started working on this but haven't found the solution yet.

You also might want to look at using the hook woocommerce_refund_deleted in case you need to delete a refund that was issued by mistake and need to restock your metadata.

本文标签: post metaWooCommerce order refund get qty refunded