1

I am working on a minor change to the woocommerce subscriptions operation for a specific scenario.

I would like to update the value of the ACF field after each payment for a subscription, and then if the field value is X then change the subscription date to null or blank. The idea is to disable subscription renewals in this scenario without changing the end date of the subscription.

My ACF fields:

  • towp_payment_count
  • number_of_billing_periods

Thats my code:

function towp_track_subscription_payments($subscription_id) {
    // Sprawdzenie, czy ID subskrypcji jest poprawne
    if (!$subscription_id) {
        error_log('Nieprawidłowe ID subskrypcji - track_subscription_payment');
        return;
    }

    $subscription = wcs_get_subscription($subscription_id);
    if (!$subscription || is_wp_error($subscription)) {
        error_log('Nie udało się pobrać id subskrypcji: ' . $subscription_id);
        return;
    }
    
    // Pobierz aktualną wartość 'towp_payment_count' z ACF      
        $payment_count = intval(get_field('towp_payment_count', $subscription_id)); 
        $payment_count++;
        // Zaktualizuj pole 'towp_payment_count' w ACF
        update_field('towp_payment_count', $payment_count, $subscription_id);
    
    $billing_periods = get_post_meta($subscription_id, 'number_of_billing_periods', true);
    if (!empty($billing_periods) && is_numeric($billing_periods)) {      
        if ($payment_count >= intval($billing_periods)) {
            $subscription->update_dates(array('next_payment' => null));  
        }
    }
    $subscription->save();
}
add_action('woocommerce_subscription_payment_complete', 'towp_track_subscription_payments', 10, 1);
1
  • May want to ask ACF about this... ...not entirely sure what their process is for updating their fields. Jan 24 at 16:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.