How can I implement a free trial for Dokan vendor subscriptions using custom code to prevent immediate charges?How can I implement a free trial for Dokan vendor subscriptions using custom code to prevent immediate charges?

Hello guys how are you? Welcome back to my blog therichpost.com. Guys today in this post, I will tell you How can I implement a free trial for Dokan vendor subscriptions using custom code to prevent immediate charges?

Guys if you are new in  WordPress or in WooCommerce then please check the below links for some good tutorials:
  1. WooCommerce Hooks
  2. WordPress Tricks
  3. WordPress Hooks
  4. Dokan

Here is the working steps and please follow carefully:

Guys if you want to handle the free trial for Dokan vendor subscriptions using custom code, you’ll need to modify how payments are processed when a vendor subscribes. Here’s a structured approach to implementing this:

1. Override the Default Subscription Payment Logic

You’ll need to hook into WooCommerce Subscription and Dokan Subscription Module to modify when payments are triggered.

2. Steps to Implement Custom Free Trial Logic

A. Modify the Subscription Checkout Process

In your theme’s functions.php or a custom plugin, add this filter to adjust the initial payment:

add_filter('woocommerce_subscriptions_calculated_initial_payment', 'custom_dokan_free_trial_initial_payment', 10, 3);

function custom_dokan_free_trial_initial_payment($initial_payment, $cart_item, $recurring_cart) {
    // Check if the product is a Dokan subscription
    if (isset($cart_item['data']) && is_a($cart_item['data'], 'WC_Product_Subscription')) {
        $trial_length = $cart_item['data']->get_trial_length();

        // If there's a trial, set initial payment to 0
        if ($trial_length > 0) {
            return 0;
        }
    }

    return $initial_payment;
}

B. Prevent Immediate Charge for Vendors

Modify the wcs_new_order_created action to ensure vendors aren’t charged at sign-up:

add_action('wcs_new_order_created', 'custom_dokan_free_trial_order_adjustment', 10, 2);

function custom_dokan_free_trial_order_adjustment($order_id, $subscription) {
    // Get the order and check if it's a Dokan vendor subscription
    $order = wc_get_order($order_id);

    foreach ($order->get_items() as $item) {
        if (is_a($item->get_product(), 'WC_Product_Subscription')) {
            $trial_length = $item->get_product()->get_trial_length();

            // If there's a trial, adjust order status
            if ($trial_length > 0) {
                $order->update_status('pending'); // Prevents auto-charge
            }
        }
    }
}

C. Automatically Charge Vendors After the Free Trial

You can use a scheduled event to trigger the payment after the trial ends:

add_action('woocommerce_scheduled_subscription_payment', 'custom_dokan_charge_after_trial', 10, 2);

function custom_dokan_charge_after_trial($amount_to_charge, $subscription) {
    $trial_end_date = $subscription->get_date('trial_end');

    if ($trial_end_date && strtotime($trial_end_date) <= time()) {
        // Trial has ended, proceed with charging the vendor
        WC_Subscriptions_Manager::process_subscription_payments_for_order($subscription->get_id());
    }
}

3. Final Steps

  • Test the subscription workflow by creating a test vendor and subscribing.
  • Ensure your payment gateway supports delayed charges.
  • Debug by logging order details and payment statuses.

Would you like to customize it further, such as handling sign-up fees separately or integrating a notification system? 🚀 Then feel free to comment below.

Ajay

Thanks

By therichpost

Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.