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

Remove duplicate cookie headers from the response #42828

Merged
merged 3 commits into from Dec 19, 2023

Conversation

mikejolley
Copy link
Member

Submission Review Guidelines:

Changes proposed in this Pull Request:

When adding multiple products to the cart programmatically, 2 headers are added per item. This PR ensures that duplicate headers are removed to prevent this occuring.

Closes #39670

How to test the changes in this Pull Request:

Developers:

To test this you can add the following code to your site somewhere. This will add 5 products to your cart when you visit any page, then redirect to the homepage.

add_action(
	'template_redirect',
	function() {
		if ( is_front_page() ) {
			return;
		}

		if ( is_page() ) {
			$products = wc_get_products(
				array(
					'posts_per_page' => 5,
					'status'         => 'publish',
					'type'           => 'simple',
				)
			);
			wc_empty_cart();
			foreach ( $products as $product ) {
				WC()->cart->add_to_cart( $product->get_id(), 1 );
			}
			wp_redirect( get_site_url() );
			exit;
		}
	},
	20
);

After this you can use browser tools to see what headers came back in the response:

Screenshot 2023-12-14 at 15 21 34

  1. With the above code in place, go to a page. Wait for the redirect then open network console and view the response of the page with the 302 redirect.
  2. With this PR you'll see 2 headers (cart hash and cart items)
  3. Without this PR you'll see ~10 headers, with duplicates of the above.

Non-developers:

  1. Please smoke test adding items to the cart to test for regressions.
  2. Add items to cart from the shop page.
  3. Add items to the cart from the product pages.
  4. View the cart and ensure results are correct.
  5. Test the above as a logged in user, and as a guest.

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

Remove duplicate cookie headers from the response when adding multiple items to the cart programmatically.

Comment

@mikejolley mikejolley self-assigned this Dec 14, 2023
@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Dec 14, 2023
@mikejolley mikejolley added the focus: cart Issues related to the cart. label Dec 14, 2023
Copy link
Contributor

github-actions bot commented Dec 14, 2023

Test Results Summary

Commit SHA: 3bbb2ed

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests25900202610m 37s
E2E Tests258003026115m 48s

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.

@mikejolley mikejolley force-pushed the fix/duplicate-cookies-39670 branch 2 times, most recently from a5360ae to 3f26ca0 Compare December 15, 2023 12:55
@mikejolley mikejolley requested review from a team and opr and removed request for a team December 15, 2023 15:06
Copy link
Contributor

Hi @opr,

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

@opr opr force-pushed the fix/duplicate-cookies-39670 branch from e9bac25 to 3bbb2ed Compare December 18, 2023 11:07
Copy link
Contributor

@opr opr left a comment

Choose a reason for hiding this comment

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

Working for me! Thanks Mike. Tests not passing though (unrelated) so waiting for a resolution before merging.

@opr opr merged commit fe37bf8 into trunk Dec 19, 2023
21 checks passed
@opr opr deleted the fix/duplicate-cookies-39670 branch December 19, 2023 11:10
@github-actions github-actions bot added this to the 8.5.0 milestone Dec 19, 2023
@github-actions github-actions bot added the needs: analysis Indicates if the PR requires a PR testing scrub session. label Dec 19, 2023
@alopezari alopezari added needs: external testing Indicates if the PR requires further testing conducted by testers external to the development team. 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 Dec 20, 2023
nerrad added a commit that referenced this pull request Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
focus: cart Issues related to the cart. needs: external testing Indicates if the PR requires further testing conducted by testers external to the development team. 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.

CallingWC_Cart::add_to_cart() multiple time can generate invalid http response
3 participants