Skip to content

fix: set order total 0 when vendor subscription trial active #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

mralaminahamed
Copy link
Member

@mralaminahamed mralaminahamed commented Jul 17, 2025

@mralaminahamed mralaminahamed self-assigned this Jul 17, 2025
Copy link

coderabbitai bot commented Jul 17, 2025

Walkthrough

A filter hook is introduced to modify WooCommerce order totals for vendor subscription orders during their trial period. Two new methods are added: one to determine if an order is in a trial period, and another to set the order total to zero when the trial is active, based on user meta data and current date.

Changes

File(s) Change Summary
dokan-invoice.php Added a filter hook for WooCommerce order totals; introduced methods for trial period logic.

Sequence Diagram(s)

sequenceDiagram
    participant Customer
    participant WooCommerce
    participant Dokan_Invoice

    WooCommerce->>Dokan_Invoice: Apply wpo_wcpdf_woocommerce_totals filter
    Dokan_Invoice->>Dokan_Invoice: get_woocommerce_totals_on_trail(totals, order)
    Dokan_Invoice->>Dokan_Invoice: is_trial_period_active(user_id)
    alt Trial period active
        Dokan_Invoice->>WooCommerce: Set order total to zero
    else Not in trial
        Dokan_Invoice->>WooCommerce: Return original totals
    end
Loading

Poem

In the land of invoices, a change hops in,
Trials for vendors, let the free days begin!
If your trial is live, your total is naught,
The code now checks, so nothing is bought.
With a twitch of my nose and a hop in my code,
Zeroed-out orders lighten your load!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
dokan-invoice.php (1)

410-410: Add type hints for consistency

For consistency with the is_trial_period_active method, consider adding type hints to the method parameters.

-	public function get_woocommerce_totals_on_trail( $totals, $order ) {
+	public function get_woocommerce_totals_on_trial( array $totals, WC_Order $order ): array {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 954418e and d23f8e1.

📒 Files selected for processing (1)
  • dokan-invoice.php (2 hunks)
🔇 Additional comments (2)
dokan-invoice.php (2)

434-446: Fix date parsing in is_trial_period_active
The call to modify() treats $trial_until as a relative format, but the meta value is almost certainly stored as an absolute timestamp (e.g. “2025-07-15 12:00:00”). This mismatch can yield incorrect comparisons or errors.

Please verify how _dokan_subscription_trial_until is stored (date format and timezone) and update the method to parse it as an absolute date. For example:

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;
    }

    $now = dokan_current_datetime();
    // Parse stored absolute date instead of modifying now
    $trial_end = \DateTime::createFromFormat(
        'Y-m-d H:i:s',
        $trial_until,
        $now->getTimezone()
    );

    if ( ! $trial_end ) {
        // Invalid format — treat as not active
        return false;
    }

    return $trial_end > $now;
}

• Confirm the exact date format and timezone used when saving _dokan_subscription_trial_until.
• Replace modify() with createFromFormat() (or adjust to match the stored format).
• Add error handling if parsing fails.


402-421: Rename method and ensure vendor validation

  1. Rename get_woocommerce_totals_on_trail to get_woocommerce_totals_on_trial to fix the typo.
  2. Before zeroing out the total, verify that the customer is actually a vendor. For example, if Dokan provides dokan_is_user_seller(), use that; otherwise, use your vendor-lookup API:
-	public function get_woocommerce_totals_on_trail( $totals, $order ) {
+	public function get_woocommerce_totals_on_trial( $totals, $order ) {
 		if ( $order->get_meta( '_dokan_vendor_subscription_order' ) !== 'yes' ) {
 			return $totals;
 		}

 		$vendor_id = $order->get_customer_id();
+		// Skip if this customer isn’t a registered vendor
+		if ( ! function_exists( 'dokan_is_user_seller' ) || ! dokan_is_user_seller( $vendor_id ) ) {
+			return $totals;
+		}

 		if ( $this->is_trial_period_active( $vendor_id ) ) {
 			$totals['order_total']['value'] = wc_price( 0, array( 'currency' => $order->get_currency() ) );
 		}

 		return $totals;
 	}

Please verify that dokan_is_user_seller() (or an equivalent helper) exists in this codebase and returns true only for valid vendors.

@@ -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 );
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix typo in method name: "trail" should be "trial"

The method name get_woocommerce_totals_on_trail contains a typo. It should be get_woocommerce_totals_on_trial to maintain consistency with the domain terminology used throughout the codebase.

-		add_filter( 'wpo_wcpdf_woocommerce_totals', [ $this, 'get_woocommerce_totals_on_trail' ], 10, 2 );
+		add_filter( 'wpo_wcpdf_woocommerce_totals', [ $this, 'get_woocommerce_totals_on_trial' ], 10, 2 );
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
add_filter( 'wpo_wcpdf_woocommerce_totals', [ $this, 'get_woocommerce_totals_on_trail' ], 10, 2 );
add_filter( 'wpo_wcpdf_woocommerce_totals', [ $this, 'get_woocommerce_totals_on_trial' ], 10, 2 );
🤖 Prompt for AI Agents
In dokan-invoice.php at line 187, the method name used in the add_filter call
has a typo: "get_woocommerce_totals_on_trail" should be corrected to
"get_woocommerce_totals_on_trial" to match the correct terminology and maintain
consistency. Update the method name in the filter registration accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant