Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Retry Woocommerce Pending and subscriptions if your gateway has an error or fails. Go to your site ?retry=1
<?php
/**
* Plugin Name: WooCommerce Subscriptions Pending payment fix
*/
$wc_pending_payment_fix = new wc_pending_payment_fix;
add_action('init', array($wc_pending_payment_fix, 'retry_payments'));
class wc_pending_payment_fix{
function retry_payments(){
global $wpdb;
if($_GET['retry'] == 1){
$failed_renewal_orders = get_posts( array(
'date_query' => array(
array(
'column' => 'post_date_gmt',
'after' => '1 month ago',
),
),
'posts_per_page' =>-1,
'post_status' => array('wc-failed','wc-pending'),
'post_type' => 'shop_order',
'orderby' => 'date',
'order' => 'desc'
) );
echo count($failed_renewal_orders);
foreach($failed_renewal_orders as $renewal_order){
$parent_subscription = wcs_get_subscription(get_post_meta($renewal_order->ID,'_subscription_renewal',true));
if($parent_subscription){
if($parent_subscription->post->post_status == 'wc-on-hold'){
if(get_post_meta($parent_subscription->id, '_payment_method_title', true) == 'Credit Card'){
$subscription = wcs_get_subscription($parent_subscription->id);
$subscription->update_status( 'active' );
do_action( 'woocommerce_scheduled_subscription_payment', $subscription->id );
wp_delete_post( $renewal_order->ID );
unset($subscription);
add_post_meta($parent_subscription->id, '_processed_retry', 1,true);
}
}
}
}
exit;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment