1

My users can request a discount (or cancel their subscription) on their renewal order through a form.

Using the form submission hook I created a function that if the action is === "cancel" it cancels the subscription, but if the action is === "discount" I don't know how to apply a (existing) coupon to the subscription.

The coupon name is "40off-renewal"

add_action('frm_after_create_entry', 'csuf_cancel_subscription', 100, 2);
add_action('frm_after_update_entry', 'csuf_cancel_subscription', 100, 2);

function csuf_cancel_subscription($entry_id, $form_id)
{
    if ($form_id == 3 && isset($_POST['item_meta'][11]) && isset($_POST['item_meta'][39]) && 'cancel' == $_POST['item_meta'][39]) {
        $user = get_user_by('email', $_POST['item_meta'][11]);
        if (!empty($user) && function_exists('wcs_get_users_subscriptions')) {
            $users_subscriptions = wcs_get_users_subscriptions($user->ID);
            if (is_array($users_subscriptions)) {
                foreach ($users_subscriptions as $subscription) {
                    if ($subscription->has_status(array('active'))) {
                        $subscription->update_status('cancelled');
                        break;
                    }
                }
            }
        }
    }
}
Hichem BOUSSETTA
  • 1,791
  • 1
  • 21
  • 27

0 Answers0