From d23f8e1d219b051e0421738512faacd4f78ef932 Mon Sep 17 00:00:00 2001 From: Al Amin Ahamed <34349365+mralaminahamed@users.noreply.github.com> Date: Thu, 17 Jul 2025 10:20:17 +0600 Subject: [PATCH] fix: set order total 0 when vendor subscription trial active --- dokan-invoice.php | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/dokan-invoice.php b/dokan-invoice.php index 1ff98e3..70be2ce 100644 --- a/dokan-invoice.php +++ b/dokan-invoice.php @@ -184,6 +184,7 @@ public function init_hooks() { add_filter( 'wpo_wcpdf_shop_name', array( $this,'wpo_wcpdf_add_dokan_shop_name'), 10, 2 ); add_filter( 'wpo_wcpdf_shop_address', array( $this,'wpo_wcpdf_add_dokan_shop_details'), 10, 2 ); add_filter( 'wpo_wcpdf_check_privs', array( $this,'wpo_wcpdf_dokan_privs'), 50, 2 ); + add_filter( 'wpo_wcpdf_woocommerce_totals', [ $this, 'get_woocommerce_totals_on_trail' ], 10, 2 ); } /** @@ -398,6 +399,52 @@ public function get_order_id_parent_id( $document = null ) { return compact('order_id','parent_id'); } + /** + * Modify WooCommerce totals for trial period subscriptions. + * + * @param array $totals WooCommerce totals + * @param WC_Order $order Order object + * + * @return array + */ + public function get_woocommerce_totals_on_trail( $totals, $order ) { + if ( $order->get_meta( '_dokan_vendor_subscription_order' ) !== 'yes' ) { + return $totals; + } + + $vendor_id = $order->get_customer_id(); + if ( $this->is_trial_period_active( $vendor_id ) ) { + $totals['order_total']['value'] = wc_price( 0, array( 'currency' => $order->get_currency() ) ); + } + + return $totals; + } + + /** + * Check if a vendor's subscription trial period is active. + * + * This method retrieves the trial end date and trial status stored in user meta + * and compares the trial end date with the current date and time to determine + * if the trial period is still active. + * + * @param int $user_id The ID of the user (vendor) to check for an active trial period. + * + * @return bool True if the trial period is active, false otherwise. + */ + public function is_trial_period_active( $user_id ): bool { + $trial_until = get_user_meta( $user_id, '_dokan_subscription_trial_until', true ); + $is_on_trial = get_user_meta( $user_id, '_dokan_subscription_is_on_trial', true ); + + if ( $is_on_trial !== 'yes' || empty( $trial_until ) ) { + return false; + } + + $time = dokan_current_datetime(); + $time = $time->modify( $trial_until ); + + return $time > dokan_current_datetime(); + } + } $dokan_invoice = Dokan_Invoice::init();