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

Add a filter for the items to apply coupons array. #45791

Merged
merged 5 commits into from Mar 25, 2024
Merged

Conversation

rawdreeg
Copy link
Contributor

@rawdreeg rawdreeg commented Mar 21, 2024

Submission Review Guidelines:

Changes proposed in this Pull Request:

I am working on an issue where fixed_cart coupon needs to be applied to only a subset of the cart items. The items to apply coupon array is built by get_items_to_apply_coupon method in WC_Discounts class.

I propose a filter in get_items_to_apply_coupon modify the items to apply coupons to.

Closes #45790 .

How to test the changes in this Pull Request:

Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions:

You can use the filter in your child's theme's function.php file.

add_filter( 'woocommerce_coupon_get_items_to_apply',  'get_items_to_apply' , 10, 2 );
function get_items_to_apply ($items_to_apply, $coupon ) {

                // Any Product ID to exclude
		$excluded_product_ids = array ( 25 ); // Replace with your test product ID.

		return array_filter( $items_to_apply, function( $item ) use ( $excluded_product_ids ) {
			return ! in_array( $item->product->get_id(), $excluded_product_ids );
		} );

	}
  1. Check out this branch.
  2. Create a fixed cart coupon
  3. Add a few items in your cart, including the product exclude in the filter usage (above).
  4. Navigate to WP Admin > WC > Orders and take a look at the order. Discount is displayed as applied to only the product allowed to accept discounts.

Changelog entry

  • Automatically create a changelog entry from the details below.

Significance

  • Patch
  • Minor
  • Major

Type

  • Fix - Fixes an existing bug
  • Add - Adds functionality
  • Update - Update existing functionality
  • Dev - Development related task
  • Tweak - A minor adjustment to the codebase
  • Performance - Address performance issues
  • Enhancement - Improvement to existing functionality

Message

WC_Discount: Add a filter for the items to apply coupons array.

Comment

@rawdreeg rawdreeg self-assigned this Mar 21, 2024
@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Mar 21, 2024
@rawdreeg rawdreeg requested a review from a team March 21, 2024 12:33
Copy link
Contributor

github-actions bot commented Mar 21, 2024

Hi @budzanowski,

Apart from reviewing the code changes, please make sure to review the testing instructions as well.

You can follow this guide to find out what good testing instructions should look like:
https://github.com/woocommerce/woocommerce/wiki/Writing-high-quality-testing-instructions

Copy link
Contributor

github-actions bot commented Mar 21, 2024

Test Results Summary

Commit SHA: e34fe28

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests25900202610m 39s
E2E Tests10035603570m 31s

To view the full API test report, click here.
To view the full E2E test report, click here.
To view all test reports, visit the WooCommerce Test Reports Dashboard.

Copy link
Contributor

@budzanowski budzanowski left a comment

Choose a reason for hiding this comment

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

Looks good but we need the documentation.

@@ -333,7 +333,7 @@ protected function get_items_to_apply_coupon( $coupon ) {

$items_to_apply[] = $item_to_apply;
}
return $items_to_apply;
return apply_filters( 'woocommerce_coupon_get_items_to_apply', $items_to_apply, $coupon, $this );
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @budzanowski . I added the missing docbloc here: ace39fa

@rawdreeg rawdreeg merged commit b3a1d8c into trunk Mar 25, 2024
35 checks passed
@rawdreeg rawdreeg deleted the tweak/45790 branch March 25, 2024 15:23
@github-actions github-actions bot added this to the 8.9.0 milestone Mar 25, 2024
@github-actions github-actions bot added the needs: analysis Indicates if the PR requires a PR testing scrub session. label Mar 25, 2024
@alopezari alopezari added needs: internal testing Indicates if the PR requires further testing conducted by Solaris status: analysis complete Indicates if a PR has been analysed by Solaris and removed needs: analysis Indicates if the PR requires a PR testing scrub session. labels Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: internal testing Indicates if the PR requires further testing conducted by Solaris plugin: woocommerce Issues related to the WooCommerce Core plugin. status: analysis complete Indicates if a PR has been analysed by Solaris
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Enhancement]: Add a filter for the items to apply coupons array.
3 participants