[wp-trac] [WordPress Trac] #61629: In Retry Rule tehre is error

WordPress Trac noreply at wordpress.org
Thu Jul 11 07:45:20 UTC 2024


#61629: In Retry Rule tehre is error
--------------------------+----------------------
 Reporter:  traveroashu   |       Owner:  (none)
     Type:  defect (bug)  |      Status:  closed
 Priority:  normal        |   Milestone:
Component:  General       |     Version:  6.5.5
 Severity:  normal        |  Resolution:  invalid
 Keywords:                |     Focuses:
--------------------------+----------------------
Changes (by swissspidy):

 * status:  new => closed
 * resolution:   => invalid
 * milestone:  Awaiting Review =>


Old description:

> in woocommcerce i have simly add the retry rule if user order status is
> failed and their subscription status is on hold then user must not be
> able to access the platform after the default retry rule passed of 7 days
> . then user should be land on the memebrship-plan page
> and here ia have added the action
> // Hook into the WooCommerce scheduled subscription payment retry action
> add_action('woocommerce_scheduled_subscription_payment_retry',
> 'custom_check_payment_retry_status', 10, 2);
>
> function custom_check_payment_retry_status($subscription, $retry_count) {
>     // Ensure $subscription and $retry_count are both passed correctly
>     $order = $subscription->get_parent();
>
>     if (!$order) {
>         // Handle case where order is not found
>         error_log('Order not found for subscription ID: ' .
> $subscription->get_id());
>         return;
>     }
>
>     // Check if the retry count has reached the 7-day threshold
>     if ($retry_count >= 7) {
>         // Check the payment status of the order
>         $payment_status = $order->get_status();
>
>         // If the payment is still not successful, update the order
> status
>         if ($payment_status !== 'completed') {
>             $order->update_status('failed'); // or 'on-hold'
>
>             // Revoke user access
>             $user_id = $order->get_user_id();
>             revoke_user_access($user_id);
>
>             // Set a transient to redirect the user upon next login
>             set_transient('redirect_to_membership_' . $user_id, true,
> 3600);
>         }
>     }
> }
>
> function revoke_user_access($user_id) {
>     // Custom function to revoke user access
>     // This will depend on how your membership system is set up
>     // For example, you could remove user roles or capabilities
>     $user = new WP_User($user_id);
>     $user->remove_role('subscriber'); // or any other role
>     error_log('User access revoked for user ID: ' . $user_id);
> }
>
> // Redirect user on login if their order has failed
> add_action('wp_login', 'redirect_after_failed_payment', 10, 2);
>
> function redirect_after_failed_payment($user_login, $user) {
>     $user_id = $user->ID;
>
>     // Check if the transient is set for this user
>     if (get_transient('redirect_to_membership_' . $user_id)) {
>         // Delete the transient
>         delete_transient('redirect_to_membership_' . $user_id);
>
>         // Redirect to the membership plan page
>         wp_redirect(site_url('/membership-plan')); // Change this to your
> membership plan page URL
>         exit();
>     }
> }
>
> // Redirect user during regular page loads if their order has failed
> add_action('template_redirect', 'check_and_redirect_to_membership_plan');
>
> function check_and_redirect_to_membership_plan() {
>     if (is_user_logged_in()) {
>         $user_id = get_current_user_id();
>
>         // Check if the transient is set for this user
>         if (get_transient('redirect_to_membership_' . $user_id)) {
>             // Delete the transient
>             delete_transient('redirect_to_membership_' . $user_id);
>
>             // Redirect to the membership plan page
>             wp_redirect(site_url('/membership-plan')); // Change this to
> your membership plan page URL
>             exit();
>         }
>     }
> }

New description:

 in woocommcerce i have simly add the retry rule if user order status is
 failed and their subscription status is on hold then user must not be able
 to access the platform after the default retry rule passed of 7 days .
 then user should be land on the memebrship-plan page
 and here ia have added the action

 {{{#!php

 // Hook into the WooCommerce scheduled subscription payment retry action
 add_action('woocommerce_scheduled_subscription_payment_retry',
 'custom_check_payment_retry_status', 10, 2);

 function custom_check_payment_retry_status($subscription, $retry_count) {
     // Ensure $subscription and $retry_count are both passed correctly
     $order = $subscription->get_parent();

     if (!$order) {
         // Handle case where order is not found
         error_log('Order not found for subscription ID: ' .
 $subscription->get_id());
         return;
     }

     // Check if the retry count has reached the 7-day threshold
     if ($retry_count >= 7) {
         // Check the payment status of the order
         $payment_status = $order->get_status();

         // If the payment is still not successful, update the order status
         if ($payment_status !== 'completed') {
             $order->update_status('failed'); // or 'on-hold'

             // Revoke user access
             $user_id = $order->get_user_id();
             revoke_user_access($user_id);

             // Set a transient to redirect the user upon next login
             set_transient('redirect_to_membership_' . $user_id, true,
 3600);
         }
     }
 }

 function revoke_user_access($user_id) {
     // Custom function to revoke user access
     // This will depend on how your membership system is set up
     // For example, you could remove user roles or capabilities
     $user = new WP_User($user_id);
     $user->remove_role('subscriber'); // or any other role
     error_log('User access revoked for user ID: ' . $user_id);
 }

 // Redirect user on login if their order has failed
 add_action('wp_login', 'redirect_after_failed_payment', 10, 2);

 function redirect_after_failed_payment($user_login, $user) {
     $user_id = $user->ID;

     // Check if the transient is set for this user
     if (get_transient('redirect_to_membership_' . $user_id)) {
         // Delete the transient
         delete_transient('redirect_to_membership_' . $user_id);

         // Redirect to the membership plan page
         wp_redirect(site_url('/membership-plan')); // Change this to your
 membership plan page URL
         exit();
     }
 }

 // Redirect user during regular page loads if their order has failed
 add_action('template_redirect', 'check_and_redirect_to_membership_plan');

 function check_and_redirect_to_membership_plan() {
     if (is_user_logged_in()) {
         $user_id = get_current_user_id();

         // Check if the transient is set for this user
         if (get_transient('redirect_to_membership_' . $user_id)) {
             // Delete the transient
             delete_transient('redirect_to_membership_' . $user_id);

             // Redirect to the membership plan page
             wp_redirect(site_url('/membership-plan')); // Change this to
 your membership plan page URL
             exit();
         }
     }
 }
 }}}

--

Comment:

 Hi there and welcome to WordPress Trac!

 Here we discuss issues in WordPress core itself. For help with third-party
 plugins such as WooCommerce, please use their dedicated support forums at
 https://wordpress.org/support/plugin/woocommerce/.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/61629#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list