Add «Skip» button to Woocommerce Subscriptions

add_filter( 'wcs_view_subscription_actions', 'add_skip_action', 10, 3 );
    
public function subscription_skip_handler(){

		global $post;

		// If the current user doesn't own the subscription, remove the query arg from the URL
		if ( isset( $_GET['subscription_id'] ) ) {

			$subscription = wcs_get_subscription( absint( $_GET['subscription_id'] ) );

			// Visiting a switch link for someone elses subscription or if the switch link doesn't contain a valid nonce
			if ( ! is_object( $subscription ) || empty( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( $_GET['_wpnonce'] ), $subscription->get_id() . '_skip' ) ) {

				wp_redirect( remove_query_arg( array( 'skip' ) ) );
				exit();

			} else {

                $date = date('Y-m-d H:i:s', $subscription->get_time( 'next_payment' ));
                $new_date = date('Y-m-d H:i:s', strtotime($date. ' + 1 months') );  

                try {

                    $subscription->update_dates( ['next_payment' => $new_date], 'gmt' );
                    
                    wp_cache_delete( $subscription->get_id(), 'posts' );
                        
                    wc_add_notice( "Your next renewal payment is in: {$new_date}", 'success' );

                    wp_redirect( remove_query_arg( array( 'skip', 'subscription_id', '_wpnonce' ) ) );

                } catch ( \Exception $e ) {
                    wcs_add_admin_notice( $e->getMessage(), 'error' );
                }

            }
        
        }

    }

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *