Skip to content
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

refactor: Improve coupon validation with extensible filter hook #2509

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from

Conversation

mralaminahamed
Copy link
Member

@mralaminahamed mralaminahamed commented Jan 10, 2025

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 added proper labels to this pull request

Changes proposed in this Pull Request:

This PR refactors the coupon validation logic in Dokan by:

  1. Renaming ensure_vendor_coupon() to ensure_is_coupon_valid() for better clarity
  2. Removing redundant cart validation logic and simplifying the code
  3. Adding a new filter hook dokan_coupon_is_valid that provides more flexibility for extending coupon validation
  4. Improving code documentation with proper PHPDoc blocks

Related Pull Request(s)

Closes

How to test the changes in this PR:

  1. Add products from multiple vendors to cart
  2. Try applying different types of coupons:
    • Vendor-specific coupons
    • Admin coupons
    • Fixed cart coupons with multiple vendors
  3. Verify coupon validation works as expected
  4. Test custom validation by using the new dokan_coupon_is_valid filter

Changelog entry

- **update:** Refactor Coupon Validation System
  • Refactored coupon validation logic to be more maintainable and extensible
  • Added new dokan_coupon_is_valid filter hook for custom coupon validation rules
  • Improved code documentation and naming conventions
  • Removed redundant cart validation checks

Before Changes:

  • Coupon validation was tightly coupled with specific implementation
  • Limited ability to extend validation rules
  • Redundant cart validation logic
  • Less clear method naming

After Changes:

  • Cleaner, more maintainable coupon validation logic
  • New filter hook for extending validation rules
  • Improved code documentation
  • Better method naming following WordPress conventions

PR Self Review Checklist:

  • Code follows WordPress coding standards
  • Method names are clear and descriptive
  • Logic is simplified and redundancy removed
  • Proper documentation added
  • New filter hook added for extensibility

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced coupon validation process with more flexible filtering.
    • Introduced a new filter dokan_coupon_is_valid for custom coupon validation.
  • Refactor

    • Renamed method ensure_vendor_coupon to ensure_coupon_is_valid.
    • Simplified coupon validation logic.
    • Removed specific vendor-related coupon restrictions.
    • Updated method signature for improved clarity and type safety.
    • Added exception handling for invalid coupons based on vendor count and spend requirements.

@mralaminahamed mralaminahamed self-assigned this Jan 10, 2025
Copy link
Contributor

coderabbitai bot commented Jan 10, 2025

Walkthrough

The pull request modifies the Hooks class in the includes/Order/Hooks.php file, focusing on coupon validation. The primary change involves renaming the method ensure_vendor_coupon to ensure_coupon_is_valid, which signals a broader approach to coupon validation. The method's implementation has been simplified and made more flexible by introducing new validation logic, including filters for minimum and maximum amount requirements.

Changes

File Change Summary
includes/Order/Hooks.php - Renamed method ensure_vendor_coupon to ensure_coupon_is_valid
- Updated method signature with type hints
- Revised internal logic for coupon validation, introducing filters for minimum and maximum amounts
- Updated method docblock to reflect changes and removed old docblock

Assessment against linked issues

Objective Addressed Explanation
Validate coupon for target products as per settings (199)
Ensure flexibility in coupon validation logic (199)

Possibly related PRs

Suggested labels

:+1: Dev Review Done, Need Dev Review Only, Upcoming Release

Suggested reviewers

  • shohag121
  • mrabbani

Poem

🐰 A coupon's path, once narrow and tight,
Now flows with filters, a more flexible might!
Renamed and refined, with elegance clear,
Validation dances, no vendor's fear!
Code hops along, with freedom so bright! 🎉


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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
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: 1

🧹 Nitpick comments (2)
includes/Order/Hooks.php (2)

423-432: Fix indentation inconsistencies.

The code uses a mix of tabs and spaces for indentation. Maintain consistent indentation using spaces.

-	    foreach ( $discount->get_items() as $item ) {
-		    if ( ! isset( $item->product ) || ! $item->product instanceof \WC_Product ) {
-			    continue;
-		    }
-
-		    $item_id = $item->product->get_id();
-
-		    $available_vendors[]  = (int) dokan_get_vendor_by_product( $item_id, true );
-		    $available_products[] = $item_id;
-	    }
+        foreach ( $discount->get_items() as $item ) {
+            if ( ! isset( $item->product ) || ! $item->product instanceof \WC_Product ) {
+                continue;
+            }
+
+            $item_id = $item->product->get_id();
+
+            $available_vendors[]  = (int) dokan_get_vendor_by_product( $item_id, true );
+            $available_products[] = $item_id;
+        }

440-453: Enhance filter documentation and consider early returns.

  1. Replace DOKAN_SINCE placeholder in the filter documentation
  2. Consider restructuring with early returns for better readability
     /**
      * Filter the validity of a coupon.
      *
-     * @since DOKAN_SINCE
+     * @since 3.9.0
      *
      * @param boolean       $valid              The validity of the coupon.
      * @param \WC_Coupon    $coupon             The coupon object.
      * @param array         $available_vendors  List of available vendors.
      * @param array         $available_products List of available products.
      * @param \WC_Discounts $discount           The discount object, which contains the order details.
      */
-    if ( apply_filters( 'dokan_coupon_is_valid', $valid, $coupon, $available_vendors, $available_products, $discount ) ) {
-        return true;
-    }
-
-    return $valid;
+    return apply_filters( 'dokan_coupon_is_valid', $valid, $coupon, $available_vendors, $available_products, $discount );
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 00d3c5d and 20278b8.

📒 Files selected for processing (1)
  • includes/Order/Hooks.php (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: api tests (1, 1)
  • GitHub Check: e2e tests (3, 3)
  • GitHub Check: e2e tests (2, 3)
  • GitHub Check: e2e tests (1, 3)
🔇 Additional comments (2)
includes/Order/Hooks.php (2)

49-49: LGTM! Hook registration is correct.

The renamed method is properly hooked into WooCommerce's coupon validation filter with appropriate priority and argument count.


434-438: LGTM! Multiple vendor validation is well implemented.

The validation logic for fixed cart coupons with multiple vendors is clear and throws an appropriate exception.

includes/Order/Hooks.php Outdated Show resolved Hide resolved
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: 0

🧹 Nitpick comments (1)
includes/Order/Hooks.php (1)

419-432: Add input parameter validation.

Consider adding early validation for the input parameters to ensure they are of the expected types. This would make the code more robust and prevent potential issues.

 public function ensure_is_coupon_valid( $valid, $coupon, $discount ) {
+    if ( ! $coupon instanceof \WC_Coupon ) {
+        return $valid;
+    }
+
+    if ( ! $discount instanceof \WC_Discounts ) {
+        return $valid;
+    }
+
     $available_vendors  = [];
     $available_products = [];
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 20278b8 and fd5671e.

📒 Files selected for processing (1)
  • includes/Order/Hooks.php (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: e2e tests (3, 3)
  • GitHub Check: e2e tests (2, 3)
  • GitHub Check: e2e tests (1, 3)
  • GitHub Check: api tests (1, 1)
🔇 Additional comments (4)
includes/Order/Hooks.php (4)

49-49: LGTM! Hook registration updated correctly.

The hook registration has been properly updated to use the new method name while maintaining the same priority and argument count for backward compatibility.


410-417: Update the @SInCE version tag.

The PHPDoc block still contains the placeholder "DOKAN_SINCE". Please replace it with the actual version number.

-@since DOKAN_SINCE Refactored to make it more flexible, and added filter
+@since 3.9.0 Refactored to make it more flexible, and added filter

440-450: Update the filter hook documentation.

The filter hook documentation still contains the placeholder "DOKAN_SINCE". Please replace it with the actual version number.

-@since DOKAN_SINCE
+@since 3.9.0

419-454: LGTM! Well-structured implementation with improved extensibility.

The refactored implementation is clean and maintainable with:

  1. Clear variable names
  2. Proper separation of concerns
  3. New filter hook for extensibility
  4. Improved type hints for better IDE support

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: 0

🧹 Nitpick comments (3)
includes/Order/Hooks.php (3)

413-420: Update the @since version tag.

Replace the DOKAN_SINCE placeholder with the actual version number.

-@since DOKAN_SINCE Refactored to make it more flexible, and added filter
+@since 3.9.0 Refactored to make it more flexible, and added filter

422-435: Consider restructuring with early returns for better readability.

The method could be more readable with early returns and better variable initialization.

 public function ensure_is_coupon_valid( bool $valid, WC_Coupon $coupon, WC_Discounts $discounts ): bool {
+    if (!$valid) {
+        return false;
+    }
+
     $available_vendors  = [];
     $available_products = [];
 
     foreach ( $discounts->get_items() as $item ) {
         if ( ! isset( $item->product ) || ! $item->product instanceof WC_Product ) {
             continue;
         }
 
         $item_id = $item->product->get_id();
 
         $available_vendors[]  = (int) dokan_get_vendor_by_product( $item_id, true );
         $available_products[] = $item_id;
     }

443-456: Update the filter hook documentation version.

The @since tag in the filter documentation still contains the placeholder version.

 /**
  * Filter the validity of a coupon.
  *
- * @since DOKAN_SINCE
+ * @since 3.9.0
  *
  * @param boolean      $valid              The validity of the coupon.

Additionally, consider adding code examples in the documentation to show how to use the new filter hook.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between fd5671e and 7ee67f5.

📒 Files selected for processing (1)
  • includes/Order/Hooks.php (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: e2e tests (3, 3)
  • GitHub Check: e2e tests (2, 3)
  • GitHub Check: e2e tests (1, 3)
  • GitHub Check: api tests (1, 1)
🔇 Additional comments (2)
includes/Order/Hooks.php (2)

6-7: LGTM! Proper type imports added.

The new imports for WC_Coupon and WC_Discounts are correctly added, improving type safety.


52-52: LGTM! Hook registration updated to match renamed method.

The hook registration is correctly updated to use the new method name while maintaining the same priority and parameter count.

includes/Order/Hooks.php Outdated Show resolved Hide resolved
@mralaminahamed mralaminahamed added Needs: Testing This requires further testing Needs: Dev Review It requires a developer review and approval labels Jan 10, 2025
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: 0

🧹 Nitpick comments (3)
includes/Order/Hooks.php (3)

426-435: Consider adding early return for empty items.

The loop could be optimized by adding an early return if there are no items to process.

+    if ( empty( $discounts->get_items() ) ) {
+        return $valid;
+    }
+
     foreach ( $discounts->get_items() as $item ) {

443-454: Enhance filter documentation with specific array types.

The array parameter types in the filter documentation could be more specific about their structure.

      * @param boolean      $valid              The validity of the coupon.
      * @param WC_Coupon    $coupon             The coupon object.
-     * @param array        $available_vendors  List of available vendors.
-     * @param array        $available_products List of available products.
+     * @param int[]        $available_vendors  Array of vendor IDs from cart items.
+     * @param int[]        $available_products Array of product IDs from cart items.
      * @param WC_Discounts $discounts          The discount object, which contains the order details.

422-454: LGTM! Well-structured coupon validation logic.

The refactored implementation:

  • Correctly handles vendor-specific validation
  • Properly validates fixed cart coupons
  • Introduces extensibility through the new filter hook

The new filter hook dokan_coupon_is_valid provides a clean extension point for custom validation rules, aligning well with the PR's objective of improving extensibility.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 7ee67f5 and 2624bee.

📒 Files selected for processing (1)
  • includes/Order/Hooks.php (3 hunks)
🔇 Additional comments (2)
includes/Order/Hooks.php (2)

6-7: LGTM! Clean updates to use statements and hook registration.

The new use statements are properly added and the hook registration is correctly updated to match the renamed method.

Also applies to: 52-52


413-420: Replace DOKAN_SINCE placeholder with actual version.

The PHPDoc block still contains the placeholder "DOKAN_SINCE" which needs to be replaced with the actual version number.

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: 1

🧹 Nitpick comments (1)
includes/Order/Hooks.php (1)

455-463: Refactor duplicate error code.

Both exception throws use the same error code 108. Consider defining this as a constant to avoid magic numbers and ensure consistency.

+ private const ERROR_CODE_AMOUNT_RESTRICTION = 108;

- ), 108
+ ), self::ERROR_CODE_AMOUNT_RESTRICTION

- ), 108
+ ), self::ERROR_CODE_AMOUNT_RESTRICTION

Also applies to: 478-486

🧰 Tools
🪛 GitHub Check: Run PHPCS inspection

[failure] 459-459:
Mismatched text domain. Expected 'dokan-lite' but got 'dokan'.

🪛 GitHub Actions: Inspections

[error] Mismatched text domain. Expected 'dokan-lite' but got 'dokan'.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2624bee and 974c0d6.

📒 Files selected for processing (1)
  • includes/Order/Hooks.php (3 hunks)
🧰 Additional context used
🪛 GitHub Check: Run PHPCS inspection
includes/Order/Hooks.php

[failure] 459-459:
Mismatched text domain. Expected 'dokan-lite' but got 'dokan'.


[failure] 482-482:
Mismatched text domain. Expected 'dokan-lite' but got 'dokan'.

🪛 GitHub Actions: Inspections
includes/Order/Hooks.php

[error] Mismatched text domain. Expected 'dokan-lite' but got 'dokan'.

⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: e2e tests (3, 3)
  • GitHub Check: e2e tests (2, 3)
  • GitHub Check: e2e tests (1, 3)
  • GitHub Check: api tests (1, 1)
🔇 Additional comments (3)
includes/Order/Hooks.php (3)

6-7: LGTM! Proper type imports added.

The addition of explicit type imports for WC_Coupon and WC_Discounts improves code clarity and type safety.

🧰 Tools
🪛 GitHub Actions: Inspections

[error] Mismatched text domain. Expected 'dokan-lite' but got 'dokan'.


413-422: Update DOKAN_SINCE placeholder with actual version number.

The docblock still contains the placeholder DOKAN_SINCE. This should be replaced with the actual version number (e.g., '3.9.0') as suggested in the previous review.

🧰 Tools
🪛 GitHub Actions: Inspections

[error] Mismatched text domain. Expected 'dokan-lite' but got 'dokan'.


426-435: LGTM! Clean and efficient item processing.

The loop efficiently processes items while properly type-checking the product instances.

🧰 Tools
🪛 GitHub Actions: Inspections

[error] Mismatched text domain. Expected 'dokan-lite' but got 'dokan'.

includes/Order/Hooks.php Outdated Show resolved Hide resolved
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: 0

🧹 Nitpick comments (1)
includes/Order/Hooks.php (1)

422-500: Well-structured refactoring with improved extensibility.

The refactoring successfully:

  1. Separates concerns between validation types
  2. Adds proper type safety
  3. Introduces extension points via filters
  4. Maintains backward compatibility

Consider adding integration tests to verify the behavior of the new filter hooks with various coupon scenarios.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 974c0d6 and de6e176.

📒 Files selected for processing (1)
  • includes/Order/Hooks.php (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: e2e tests (3, 3)
  • GitHub Check: e2e tests (2, 3)
  • GitHub Check: e2e tests (1, 3)
  • GitHub Check: api tests (1, 1)
🔇 Additional comments (6)
includes/Order/Hooks.php (6)

6-7: LGTM! Clean import additions.

The new imports for WC_Coupon and WC_Discounts are properly added to support the type hints in the refactored method.


413-422: Replace DOKAN_SINCE placeholder with actual version.

The PHPDoc block still contains the placeholder DOKAN_SINCE. This should be replaced with the actual version number where this change is being introduced.


422-422: LGTM! Improved method signature.

The method signature changes are excellent:

  • Descriptive rename from ensure_vendor_coupon to ensure_coupon_is_valid
  • Added type hints for parameters and return type
  • Return type declaration matches the PHPDoc

443-463: LGTM! Well-structured minimum amount validation.

The minimum amount validation is well-implemented with:

  • Clear filter hook naming
  • Comprehensive docblock
  • Proper error handling

466-486: LGTM! Well-structured maximum amount validation.

The maximum amount validation mirrors the minimum amount implementation with consistent structure and error handling.


489-500: LGTM! Flexible validation hook.

The dokan_coupon_is_valid filter provides excellent extensibility by exposing all relevant data:

  • Coupon object
  • Available vendors
  • Available products
  • Discounts object

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: 0

🧹 Nitpick comments (3)
includes/Order/Hooks.php (3)

461-461: Document the error code constant.

The hardcoded error code 108 should be defined as a constant with a descriptive name to improve maintainability.

+ const ERROR_CODE_AMOUNT_RESTRICTION = 108;
...
-                108
+                self::ERROR_CODE_AMOUNT_RESTRICTION

Also applies to: 483-483


448-448: Update @SInCE tags in filter documentation.

Replace DOKAN_SINCE with the actual version number in the filter documentation blocks:

  • dokan_coupon_validate_minimum_amount
  • dokan_coupon_validate_maximum_amount
  • dokan_coupon_is_valid

Also applies to: 470-470, 490-490


443-462: Well-structured filter implementation.

The addition of new filter hooks improves extensibility:

  • dokan_coupon_validate_minimum_amount
  • dokan_coupon_validate_maximum_amount
  • dokan_coupon_is_valid

This allows for custom validation rules while maintaining clean separation of concerns.

Consider documenting common use cases for these filters in the developer documentation to help plugin developers understand when and how to use them effectively.

Also applies to: 465-484, 487-498

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between de6e176 and e10d98f.

📒 Files selected for processing (1)
  • includes/Order/Hooks.php (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: e2e tests (3, 3)
  • GitHub Check: e2e tests (2, 3)
  • GitHub Check: e2e tests (1, 3)
  • GitHub Check: api tests (1, 1)
🔇 Additional comments (4)
includes/Order/Hooks.php (4)

6-7: LGTM! Import statements are properly organized.

The new imports for WC_Coupon and WC_Discounts align well with the type hints in the refactored method.


413-422: Update the @SInCE version number.

The @since DOKAN_SINCE placeholder needs to be replaced with the actual version number.


458-458: Fix text domain inconsistency.

The text domain should be 'dokan-lite' instead of 'dokan' for consistency.

Also applies to: 480-480


426-435: LGTM! Improved item validation logic.

The refactored code properly validates items using type checking and collects vendor and product information efficiently.

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: 0

🧹 Nitpick comments (1)
includes/Order/Hooks.php (1)

426-429: Add early return for invalid items.

Consider adding an early return if no valid items are found to improve performance and readability.

 foreach ( $discounts->get_items() as $item ) {
     if ( ! isset( $item->product ) || ! $item->product instanceof WC_Product ) {
-        continue;
+        return false;
     }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e10d98f and 21b13d2.

📒 Files selected for processing (1)
  • includes/Order/Hooks.php (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: e2e tests (3, 3)
  • GitHub Check: e2e tests (2, 3)
  • GitHub Check: e2e tests (1, 3)
  • GitHub Check: api tests (1, 1)
🔇 Additional comments (3)
includes/Order/Hooks.php (3)

6-7: LGTM! Clean import additions and hook registration update.

The new imports and hook registration changes align well with the refactoring objectives.

Also applies to: 52-52


413-420: Update version placeholder in PHPDoc.

The @since DOKAN_SINCE placeholder needs to be replaced with the actual version number.


443-454: LGTM! Well-documented filter hook.

The new filter hook dokan_coupon_is_valid provides comprehensive context with all necessary parameters for third-party extensions.

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: 0

🧹 Nitpick comments (2)
includes/Order/Hooks.php (2)

425-431: Fix code alignment and add null checks.

  1. Fix alignment issues flagged by PHPCS:
-    $available_vendors  = [];
+    $available_vendors = [];
-    $available_vendors[]  = (int) dokan_get_vendor_by_product( $item->product->get_id(), true );
+    $available_vendors[] = (int) dokan_get_vendor_by_product( $item->product->get_id(), true );
  1. Consider adding a null check for dokan_get_vendor_by_product() return value to handle cases where a product might not have an associated vendor.
🧰 Tools
🪛 GitHub Check: Run PHPCS inspection

[warning] 430-430:
Equals sign not aligned correctly; expected 1 space but found 2 spaces


439-448: Update filter documentation version.

The @since DOKAN_SINCE tag in the filter documentation needs to be updated with the actual version number.

Additionally, consider adding a code example in the documentation to show how to use this filter, as it's a new extension point.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 21b13d2 and b1d0195.

📒 Files selected for processing (1)
  • includes/Order/Hooks.php (3 hunks)
🧰 Additional context used
🪛 GitHub Check: Run PHPCS inspection
includes/Order/Hooks.php

[warning] 423-423:
Equals sign not aligned correctly; expected 1 space but found 2 spaces


[warning] 430-430:
Equals sign not aligned correctly; expected 1 space but found 2 spaces

⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: e2e tests (3, 3)
  • GitHub Check: e2e tests (2, 3)
  • GitHub Check: e2e tests (1, 3)
  • GitHub Check: api tests (1, 1)
🔇 Additional comments (3)
includes/Order/Hooks.php (3)

6-7: LGTM! Well-organized use statements.

The new use statements for WC_Coupon and WC_Discounts are properly added and organized.


413-420: Update DOKAN_SINCE placeholder.

The PHPDoc block still contains the placeholder DOKAN_SINCE. This should be replaced with the actual version number where this change is being introduced.


422-448: Verify backward compatibility of the coupon validation changes.

The refactored coupon validation logic might impact existing implementations. Let's verify there are no breaking changes:

✅ Verification successful

Coupon validation changes enhance functionality without breaking compatibility

The changes introduce better handling of fixed cart coupons with multiple vendors and provide improved extensibility through the dokan_coupon_is_valid filter. No active implementations of the old method were found that could be impacted by these changes.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for existing implementations of the old method name
rg "ensure_vendor_coupon" -l

# Search for existing filter usage
rg "add_filter.*dokan_coupon_is_valid" -l

Length of output: 109


Script:

#!/bin/bash
# Check content of files mentioning the old method
echo "=== Content from whats-new.php ==="
rg -A 5 -B 5 "ensure_vendor_coupon" templates/whats-new.php

echo -e "\n=== Content from CHANGELOG.md ==="
rg -A 5 -B 5 "ensure_vendor_coupon" CHANGELOG.md

# Search for broader coupon-related filters
echo -e "\n=== Coupon-related filters ==="
rg "apply_filters.*dokan.*coupon" -A 2

# Search for coupon-related hooks
echo -e "\n=== Coupon-related actions ==="
rg "do_action.*dokan.*coupon" -A 2

Length of output: 2523

🧰 Tools
🪛 GitHub Check: Run PHPCS inspection

[warning] 423-423:
Equals sign not aligned correctly; expected 1 space but found 2 spaces


[warning] 430-430:
Equals sign not aligned correctly; expected 1 space but found 2 spaces

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs: Dev Review It requires a developer review and approval Needs: Testing This requires further testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants