Skip to content

enhance: Add additional tax fields and new order types for Dokan order stats table.#3099

Open
MdAsifHossainNadim wants to merge 1 commit intodevelopfrom
enhance/add-order-types-for-dokan-order-stats-and-apply-upgrader
Open

enhance: Add additional tax fields and new order types for Dokan order stats table.#3099
MdAsifHossainNadim wants to merge 1 commit intodevelopfrom
enhance/add-order-types-for-dokan-order-stats-and-apply-upgrader

Conversation

@MdAsifHossainNadim
Copy link
Contributor

@MdAsifHossainNadim MdAsifHossainNadim commented Feb 6, 2026

All Submissions:

  • My code follow the WordPress' coding standards
  • My code satisfies feature requirements
  • My code is tested
  • My code passes the PHPCS tests
  • My code has proper inline documentation
  • I've included related pull request(s) (optional)
  • I've included developer documentation (optional)
  • I've added proper labels to this pull request

Changes proposed in this Pull Request:

Related Pull Request(s)

  • Full PR Link

Closes

How to test the changes in this Pull Request:

  • Steps or issue link

Changelog entry

Title

Detailed Description of the pull request. What was previous behaviour
and what will be changed in this PR.

Before Changes

Describe the issue before changes with screenshots(s).

After Changes

Describe the issue after changes with screenshot(s).

Feature Video (optional)

Link of detailed video if this PR is for a feature.

PR Self Review Checklist:

  • Code is not following code style guidelines
  • Bad naming: make sure you would understand your code if you read it a few months from now.
  • KISS: Keep it simple, Sweetie (not stupid!).
  • DRY: Don't Repeat Yourself.
  • Code that is not readable: too many nested 'if's are a bad sign.
  • Performance issues
  • Complicated constructions that need refactoring or comments: code should almost always be self-explanatory.
  • Grammar errors.

FOR PR REVIEWER ONLY:

As a reviewer, your feedback should be focused on the idea, not the person. Seek to understand, be respectful, and focus on constructive dialog.

As a contributor, your responsibility is to learn from suggestions and iterate your pull request should it be needed based on feedback. Seek to collaborate and produce the best possible contribution to the greater whole.

  • Correct — Does the change do what it’s supposed to? ie: code 100% fulfilling the requirements?
  • Secure — Would a nefarious party find some way to exploit this change? ie: everything is sanitized/escaped appropriately for any SQL or XSS injection possibilities?
  • Readable — Will your future self be able to understand this change months down the road?
  • Elegant — Does the change fit aesthetically within the overall style and architecture?

Summary by CodeRabbit

  • New Features

    • Support for four new order types: advertisement products, advertisement refunds, subscriptions, and subscription refunds
    • Enhanced financial tracking with separate vendor and admin tax, shipping tax, and earnings fields
  • Chores

    • Updated database schema to support new order types and financial tracking capabilities
    • Added automatic upgrade process for version 4.2.10 with data regeneration

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 6, 2026

📝 Walkthrough

Walkthrough

This change implements an analytics migration for version 4.2.10, introducing four new order types (advertisement product/refund, subscription/refund), extending the order stats table with vendor and admin tax columns, and adding migration logic to safely alter the database schema and regenerate analytics data.

Changes

Cohort / File(s) Summary
Order Type Constants
includes/Analytics/Reports/OrderType.php
Added four new public constants for advertisement and subscription order types and their refunds; updated get_all_order_types() to include them and refined its docblock.
Analytics Data Mapping
includes/Analytics/Reports/Orders/Stats/DataStore.php
Replaced commented vendor tax fields with vendor_shipping_tax and vendor_order_tax; added admin fields admin_earning, admin_shipping_tax, and admin_order_tax mapped from respective methods; updated format array placeholders.
Database Schema
includes/Install/Installer.php
Extended order_type enum with values 6–9 for new order types; added five new monetary columns (vendor_shipping_tax, vendor_order_tax, admin_earning, admin_shipping_tax, admin_order_tax) to dokan_order_stats; added index on order_type.
Upgrade Registration & Implementation
includes/Upgrade/Upgrades.php, includes/Upgrade/Upgrades/V_4_2_10.php
Registered version 4.2.10 upgrade entry; introduced new V_4_2_10 class that conditionally alters dokan_order_stats table (creates if missing, adds columns if absent), updates order_type comment, and regenerates order statistics data.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • Fix/revenue reports #2423: Updates QueryFilter to import and use OrderType constants for vendor and admin order-type filtering, directly complementing the new order types introduced in this PR.

Suggested labels

Needs: Testing, Dev Review Done

Suggested reviewers

  • mrabbani
  • shohag121
  • kzamanbd

Poem

🐰 Four new order types hopped in today,
Tax columns aligned in a schema ballet,
Vendor and admin both get their due share,
A migration upgrade handles it with care! 📊✨

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch enhance/add-order-types-for-dokan-order-stats-and-apply-upgrader

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@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: 3

🤖 Fix all issues with AI agents
In `@includes/Upgrade/Upgrades/V_4_2_10.php`:
- Around line 9-14: The docblock `@since` tags in this file are incorrect: they
say "3.15.0" while the upgrade class is V_4_2_10 and registered as version
4.2.10; update the file-level and any method-level docblocks inside this file to
use "@since 4.2.10" (search for every occurrence of "3.15.0" in this file and
replace it), ensuring the class declaration V_4_2_10 and any related methods'
docblocks reflect the correct 4.2.10 version.
- Around line 42-48: The upgrade script's $columns_to_add sets the
'admin_earning' ALTER TABLE to be added AFTER `vendor_shipping_tax`, which
misorders columns compared to Installer.php (expected order: `vendor_order_tax`
→ `admin_earning` → `admin_commission`); update the 'admin_earning' entry in the
$columns_to_add array so its ALTER TABLE uses AFTER `vendor_order_tax` (keeping
other column definitions unchanged) to match the Installer.php schema and avoid
schema drift.
- Around line 70-86: Replace the synchronous call to
ReportsSync::regenerate_report_data() inside regenerate_order_stats_data() with
background queueing using the existing DokanBackgroundProcesses flow: create a
task payload that signals ReportsSync regeneration (e.g., class/method name and
args), call DokanBackgroundProcesses::push_to_queue($payload) and then
DokanBackgroundProcesses::dispatch_process() so the heavy
ReportsSync::regenerate_report_data(null, false) work runs asynchronously during
upgrade; update regenerate_order_stats_data() to only enqueue and dispatch the
job.
🧹 Nitpick comments (2)
includes/Analytics/Reports/Orders/Stats/DataStore.php (1)

145-154: admin_earning and admin_commission are both sourced from get_admin_commission() — clarify intent.

Lines 148 and 149 both call $order_commission->get_admin_commission(). If admin_earning is intended to eventually diverge from admin_commission, this is fine as a transitional step, but a brief inline comment explaining the distinction (or planned distinction) would help future maintainers.

Also, per the linked issue, admin_earning should be "populated via get_admin_commission", so the current mapping is consistent with the spec — just redundant at this point.

includes/Upgrade/Upgrades/V_4_2_10.php (1)

50-61: No error handling after $wpdb->query() — failed ALTERs are silently ignored.

If an ALTER TABLE fails (e.g., due to permissions or disk space), $wpdb->query() returns false and no error is logged. On the next plugin load the upgrade won't re-run (version is already bumped), leaving the schema in a partial state. Consider logging a warning on failure.

Proposed improvement
                 if ( empty( $column_exists ) ) {
-                    $wpdb->query( $alter_query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+                    $result = $wpdb->query( $alter_query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+                    if ( false === $result ) {
+                        dokan_log( sprintf( 'Failed to add column %s to %s: %s', $column_name, $table_name, $wpdb->last_error ) );
+                    }
                 }

Comment on lines +9 to +14
/**
* Upgrade class for version 3.15.0.
*
* @since 3.15.0
*/
class V_4_2_10 extends DokanUpgrader {
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Docblock version mismatch: @since 3.15.0 vs upgrade key 4.2.10.

The @since tags throughout this file reference 3.15.0, but the class is named V_4_2_10 and registered under version 4.2.10 in Upgrades.php. Align the docblocks.

🤖 Prompt for AI Agents
In `@includes/Upgrade/Upgrades/V_4_2_10.php` around lines 9 - 14, The docblock
`@since` tags in this file are incorrect: they say "3.15.0" while the upgrade
class is V_4_2_10 and registered as version 4.2.10; update the file-level and
any method-level docblocks inside this file to use "@since 4.2.10" (search for
every occurrence of "3.15.0" in this file and replace it), ensuring the class
declaration V_4_2_10 and any related methods' docblocks reflect the correct
4.2.10 version.

Comment on lines +42 to +48
$columns_to_add = [
'vendor_shipping_tax' => "ALTER TABLE `{$table_name}` ADD COLUMN `vendor_shipping_tax` double NOT NULL DEFAULT '0' AFTER `vendor_discount`",
'vendor_order_tax' => "ALTER TABLE `{$table_name}` ADD COLUMN `vendor_order_tax` double NOT NULL DEFAULT '0' AFTER `vendor_shipping_tax`",
'admin_earning' => "ALTER TABLE `{$table_name}` ADD COLUMN `admin_earning` double NOT NULL DEFAULT '0' AFTER `vendor_shipping_tax`",
'admin_shipping_tax' => "ALTER TABLE `{$table_name}` ADD COLUMN `admin_shipping_tax` double NOT NULL DEFAULT '0' AFTER `admin_discount`",
'admin_order_tax' => "ALTER TABLE `{$table_name}` ADD COLUMN `admin_order_tax` double NOT NULL DEFAULT '0' AFTER `admin_shipping_tax`",
];
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

admin_earning column placed AFTER vendor_shipping_tax — inconsistent with Installer.php schema.

In Installer.php, the column order is: vendor_order_taxadmin_earningadmin_commission. But line 45 places admin_earning after vendor_shipping_tax, which puts it before vendor_order_tax in upgraded databases.

This doesn't affect functionality (queries use column names, not ordinal positions), but it creates a schema drift between fresh installs and upgrades.

Proposed fix
-                'admin_earning'       => "ALTER TABLE `{$table_name}` ADD COLUMN `admin_earning` double NOT NULL DEFAULT '0' AFTER `vendor_shipping_tax`",
+                'admin_earning'       => "ALTER TABLE `{$table_name}` ADD COLUMN `admin_earning` double NOT NULL DEFAULT '0' AFTER `vendor_order_tax`",
🤖 Prompt for AI Agents
In `@includes/Upgrade/Upgrades/V_4_2_10.php` around lines 42 - 48, The upgrade
script's $columns_to_add sets the 'admin_earning' ALTER TABLE to be added AFTER
`vendor_shipping_tax`, which misorders columns compared to Installer.php
(expected order: `vendor_order_tax` → `admin_earning` → `admin_commission`);
update the 'admin_earning' entry in the $columns_to_add array so its ALTER TABLE
uses AFTER `vendor_order_tax` (keeping other column definitions unchanged) to
match the Installer.php schema and avoid schema drift.

Comment on lines +70 to +86
// Regenerate the order stats data
self::regenerate_order_stats_data();
}

/**
* Regenerate order stats data by syncing WC order stats.
*
* @since 3.15.0
*
* @return void
*/
private static function regenerate_order_stats_data() {
// Sync the WC order stats to regenerate dokan order stats
if ( class_exists( ReportsSync::class ) ) {
ReportsSync::regenerate_report_data( null, false );
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

# Find the ReportsSync class and regenerate_report_data method
find . -type f -name "*.php" | xargs grep -l "class ReportsSync" 2>/dev/null | head -5

Repository: getdokan/dokan

Length of output: 40


🏁 Script executed:

# Search for regenerate_report_data method definition
rg -n "regenerate_report_data" --type php -B 2 -A 15 | head -100

Repository: getdokan/dokan

Length of output: 832


🏁 Script executed:

# Check how regenerate_report_data is implemented in ReportsSync
fd -e php | xargs grep -l "ReportsSync" | head -10

Repository: getdokan/dokan

Length of output: 135


🏁 Script executed:

# Check the top of V_4_2_10.php for imports
head -30 includes/Upgrade/Upgrades/V_4_2_10.php

Repository: getdokan/dokan

Length of output: 744


🏁 Script executed:

# Check if ReportsSync is imported or namespaced
rg -n "use.*ReportsSync|ReportsSync" includes/Upgrade/Upgrades/V_4_2_10.php

Repository: getdokan/dokan

Length of output: 227


🏁 Script executed:

# Search for any background job queuing patterns in upgrade files
rg -n "as_enqueue_async_action|schedule_single_action|ActionScheduler" includes/Upgrade/ -B 2 -A 2

Repository: getdokan/dokan

Length of output: 40


🏁 Script executed:

# Search for WC Action Scheduler usage patterns in entire codebase
rg -n "as_enqueue|as_schedule|ActionScheduler" --type php | head -30

Repository: getdokan/dokan

Length of output: 994


🏁 Script executed:

# Check if background processing is used in other parts of the plugin
rg -n "wp_schedule|wp_remote_post|background" --type php includes/ | head -20

Repository: getdokan/dokan

Length of output: 2407


🏁 Script executed:

# Search for how other expensive operations are handled in the codebase
rg -n "async|background|queue" includes/ --type php | grep -i "job\|task\|queue" | head -20

Repository: getdokan/dokan

Length of output: 1914


🏁 Script executed:

# Check other upgrade files to see if any use background processing
ls -la includes/Upgrade/Upgrades/ | head -20

Repository: getdokan/dokan

Length of output: 1215


🏁 Script executed:

# Check how background processing is implemented in V_2_8_3.php
cat -n includes/Upgrade/Upgrades/V_2_8_3.php

Repository: getdokan/dokan

Length of output: 823


🏁 Script executed:

# Check the BackgroundProcess directory to understand the pattern
ls -la includes/Upgrade/BackgroundProcesses/

Repository: getdokan/dokan

Length of output: 142


🏁 Script executed:

# Look at how the processor is used
rg -n "push_to_queue|dispatch_process" includes/Upgrade/Upgrades/ -B 5 -A 2

Repository: getdokan/dokan

Length of output: 7023


🏁 Script executed:

# Find BackgroundProcesses directory
find . -type d -name "BackgroundProcesses" | head -5

Repository: getdokan/dokan

Length of output: 105


🏁 Script executed:

# Look for background processor classes used in upgrades
rg -n "class.*Background|extends.*Background" includes/Upgrade/ --type php -B 2 -A 5

Repository: getdokan/dokan

Length of output: 7153


🏁 Script executed:

# Check if there's a dedicated background processor for ReportsSync or similar analytics operations
rg -n "ReportsSync|Analytics|regenerate" includes/BackgroundProcess/ --type php 2>/dev/null | head -20

Repository: getdokan/dokan

Length of output: 212


Queue ReportsSync::regenerate_report_data() as a background task during upgrade.

Calling this WooCommerce analytics regeneration synchronously will timeout on stores with large order volumes. The codebase uses DokanBackgroundProcesses with push_to_queue() and dispatch_process() throughout upgrade routines (V_2_8_3, V_2_9_4, V_3_0_10, V_3_6_2, V_3_7_19, etc.) for expensive operations. This method should follow the same pattern instead of executing inline. Note: This same issue appears in V_3_13_0.php line 15.

🤖 Prompt for AI Agents
In `@includes/Upgrade/Upgrades/V_4_2_10.php` around lines 70 - 86, Replace the
synchronous call to ReportsSync::regenerate_report_data() inside
regenerate_order_stats_data() with background queueing using the existing
DokanBackgroundProcesses flow: create a task payload that signals ReportsSync
regeneration (e.g., class/method name and args), call
DokanBackgroundProcesses::push_to_queue($payload) and then
DokanBackgroundProcesses::dispatch_process() so the heavy
ReportsSync::regenerate_report_data(null, false) work runs asynchronously during
upgrade; update regenerate_order_stats_data() to only enqueue and dispatch the
job.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments