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

Losing wp_woocommerce_sessions value on page refresh. #819

Open
nd0nd0 opened this issue Nov 23, 2023 · 0 comments
Open

Losing wp_woocommerce_sessions value on page refresh. #819

nd0nd0 opened this issue Nov 23, 2023 · 0 comments
Labels
question Further information is requested

Comments

@nd0nd0
Copy link

nd0nd0 commented Nov 23, 2023

I am using Apollo's useMutation to add to cart. However on refetch on reload of page the wp_woocommerce_sessions value on my session_key is reset so I end up with { "data": { "cart": { "contents": { "nodes": [], "__typename": "CartToCartItemConnection" }, "appliedCoupons": null, "subtotal": "UGX 0", "subtotalTax": "UGX 0", "shippingTax": "UGX 0", "shippingTotal": "UGX 0", "total": "UGX 0", "totalTax": "UGX 0", "feeTax": "UGX 0", "feeTotal": "UGX 0", "discountTax": "UGX 0", "discountTotal": "UGX 0", "__typename": "Cart" } }.

Is my session getting messed up?

Apollo client

const httpLink = new HttpLink({
	uri: "http://localhost:10004/graphql",
});

export const middleware = new ApolloLink((operation, forward) => {
	
	if(typeof window !== 'undefined'){

		const session = localStorage.getItem("woo-session");
		if (session) {
			operation.setContext(({ headers = {} }) => ({
				headers: {
					"woocommerce-session": `Session ${session}`,
				},
			}));
		}
	}

	return forward(operation);
});

export const afterware = new ApolloLink((operation, forward) => {
	return forward(operation).map((response) => {
		if (!typeof window) {
			return response;
		}
	
		const context = operation.getContext();
		const {
			response: { headers },
		} = context;
		if(typeof window !== 'undefined'){
			const session = headers.get("woocommerce-session");

			if (session) {
				if (localStorage.getItem("woo-session") !== session) {
					localStorage.setItem("woo-session", headers.get("woocommerce-session"));
				}
			}
		}

		return response;
	});
});

const client = new ApolloClient({
	link: middleware.concat(afterware.concat(httpLink)),
	cache: new InMemoryCache(),
});

export default client

Product Add to cart

const {data, refetch } = useQuery<Cart>(GET_CART, {notifyOnNetworkStatusChange: true});`
	
useEffect(()=>{
		if(data){
			const updatedCart = getFormattedCart(data);
			localStorage.setItem("local-cart", JSON.stringify( updatedCart));
		}
},[data])

const [addToCart,
	] = useMutation<Add_To_CartMutation>(ADD_TO_CART, {
		variables: {input: productQtyInput},
		onCompleted: (data) => {refetch()},
	});
  • OS: Windows
  • Browser: Chrome

Plugin Versions

  • WooGraphQL Version: 0.18.2
  • WPGraphQL Version: 1.19.0
  • WordPress Version: 6.4.1
  • WooCommerce Version: 8.3.1
@kidunot89 kidunot89 added the question Further information is requested label Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants