-
-
Save ValeriiVasyliev/5324cb0aaca8667b6dc01f815a0f10d6 to your computer and use it in GitHub Desktop.
How to Create WooCommerce Refunds Programmatically | ibenic.com/how-to-create-woocommerce-refunds-programmatically
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 | |
| /** | |
| * Process Order Refund through Code | |
| * @return WC_Order_Refund|WP_Error | |
| */ | |
| function ibenic_wc_refund_order( $order_id, $refund_reason = '' ) { | |
| $order = wc_get_order( $order_id ); | |
| // IF it's something else such as a WC_Order_Refund, we don't want that. | |
| if( ! is_a( $order, 'WC_Order') ) { | |
| return; | |
| } | |
| // Get Items | |
| $order_items = $order->get_items(); | |
| // Refund Amount | |
| $refund_amount = 0; | |
| // Prepare line items which we are refunding | |
| $line_items = array(); | |
| if ( $order_items = $order->get_items()) { | |
| foreach( $order_items as $item_id => $item ) { | |
| $item_meta = $order->get_item_meta( $item_id ); | |
| $product_data = wc_get_product( $item_meta["_product_id"][0] ); | |
| $item_ids[] = $item_id; | |
| $tax_data = $item_meta['_line_tax_data']; | |
| $refund_tax = 0; | |
| if( is_array( $tax_data[0] ) ) { | |
| $refund_tax = array_map( 'wc_format_decimal', $tax_data[0] ); | |
| } | |
| $refund_amount = wc_format_decimal( $refund_amount ) + wc_format_decimal( $item_meta['_line_total'][0] ); | |
| $line_items[ $item_id ] = array( 'qty' => $item_meta['_qty'][0], 'refund_total' => wc_format_decimal( $item_meta['_line_total'][0] ), 'refund_tax' => $refund_tax ); | |
| } | |
| } | |
| $refund = wc_create_refund( array( | |
| 'amount' => $refund_amount, | |
| 'reason' => $refund_reason, | |
| 'order_id' => $order_id, | |
| 'line_items' => $line_items, | |
| 'refund_payment' => true | |
| ) ); | |
| return $refund; | |
| } |
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 | |
| /** | |
| * Process Order Refund through Code | |
| * @return WC_Order_Refund|WP_Error | |
| */ | |
| function ibenic_wc_refund_order( $order_id, $refund_reason = '' ) { | |
| $order = wc_get_order( $order_id ); | |
| // If it's something else such as a WC_Order_Refund, we don't want that. | |
| if( ! is_a( $order, 'WC_Order') ) { | |
| return new WP_Error( 'wc-order', __( 'Provided ID is not a WC Order', 'yourtextdomain' ) ); | |
| } | |
| if( 'refunded' == $order->get_status() ) { | |
| return new WP_Error( 'wc-order', __( 'Order has been already refunded', 'yourtextdomain' ) ); | |
| } | |
| // Get Items | |
| $order_items = $order->get_items(); | |
| // Refund Amount | |
| $refund_amount = 0; | |
| // Prepare line items which we are refunding | |
| $line_items = array(); | |
| // Other code will go here | |
| } | |
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 | |
| /** | |
| * Process Order Refund through Code | |
| * @return WC_Order_Refund|WP_Error | |
| */ | |
| function ibenic_wc_refund_order( $order_id, $refund_reason = '' ) { | |
| // ... | |
| if ( $order_items ) { | |
| foreach( $order_items as $item_id => $item ) { | |
| $item_meta = $order->get_item_meta( $item_id ); | |
| $tax_data = $item_meta['_line_tax_data']; | |
| $refund_tax = 0; | |
| if( is_array( $tax_data[0] ) ) { | |
| $refund_tax = array_map( 'wc_format_decimal', $tax_data[0] ); | |
| } | |
| $refund_amount = wc_format_decimal( $refund_amount ) + wc_format_decimal( $item_meta['_line_total'][0] ); | |
| $line_items[ $item_id ] = array( | |
| 'qty' => $item_meta['_qty'][0], | |
| 'refund_total' => wc_format_decimal( $item_meta['_line_total'][0] ), | |
| 'refund_tax' => $refund_tax ); | |
| } | |
| } | |
| // Order Items were processed. We can now create a refund | |
| } |
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 | |
| /** | |
| * Process Order Refund through Code | |
| * @return WC_Order_Refund|WP_Error | |
| */ | |
| function ibenic_wc_refund_order( $order_id, $refund_reason = '' ) { | |
| // ... | |
| $refund = wc_create_refund( array( | |
| 'amount' => $refund_amount, | |
| 'reason' => $refund_reason, | |
| 'order_id' => $order_id, | |
| 'line_items' => $line_items, | |
| 'refund_payment' => true | |
| )); | |
| return $refund; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment