Last active
April 14, 2025 11:27
-
-
Save xlplugins/65ef1c0b065bcf293059fb7d18a39168 to your computer and use it in GitHub Desktop.
Update order meta based on order note for stripe failures & remove when order moved to completed stasuses
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
/** * Add a note to the order when the order status changes to failed | |
* | |
* @param int $order_id The ID of the order. | |
* @param WC_Order $order The order object. | |
* @param array $status_transition The status transition data. | |
*/ | |
add_action( 'woocommerce_order_status_failed', function ( $order_id, $order, $status_transition ) { | |
if ( $order->get_payment_method() !== 'fkwcs_stripe' ) { | |
return; | |
} | |
if ( ! isset( $status_transition['note'] ) || empty( $status_transition['note'] ) ) { | |
return; | |
} | |
//save the message in order meta | |
$order = wc_get_order( $order_id ); | |
if ( $order ) { | |
$order->update_meta_data( 'fkwcs_order_failed_message', $status_transition['note'] ); | |
$order->save_meta_data(); | |
} | |
}, 10, 3 ); | |
add_action( 'woocommerce_order_status_processing', 'remove_order_failed_message_meta', 10, 1 ); | |
add_action( 'woocommerce_order_status_completed', 'remove_order_failed_message_meta', 10, 1 ); | |
function remove_order_failed_message_meta( $order_id ) { | |
$order = wc_get_order( $order_id ); | |
if ( $order ) { | |
// Remove the 'fkwcs_order_failed_message' metadata | |
$order->delete_meta_data( 'fkwcs_order_failed_message' ); | |
$order->save(); // Save the order to persist the change | |
} | |
} | |
add_action( 'woocommerce_order_status_changed', function( $order_id, $old_status, $new_status ) { | |
if ( in_array( $new_status, ['processing', 'completed'] ) ) { | |
$order = wc_get_order( $order_id ); | |
if ( $order ) { | |
// Remove the 'fkwcs_order_failed_message' metadata | |
$order->delete_meta_data( 'fkwcs_order_failed_message' ); | |
$order->save(); // Save the order to persist the change | |
} | |
} | |
}, 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment