Cart Fetch not working

SriVathson
Tourist
5 0 1

We are facing an issue when trying to access the cart in our app. On our page, the hasFetchCart dispatch object holds the value false. We have used the below code for checking the cart permission.

<body>
	<div id="container2"></div>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
	<script src="https://unpkg.com/@shopify/app-bridge"></script>
	<script>
		const AppBridge = window['app-bridge'];
		const actions = window['app-bridge'].actions;
		const createApp = AppBridge.createApp;
		const Cart = actions.Cart;
		const Group = actions.Group;
		const Features = actions.Features;
		
		$("#container2").append("<br></br> Start APP <br></br>");

		const app = createApp({
		  apiKey: 'api_key',  
		  shopOrigin: 'shop_origin'
		});

		const unsubscribe = app.subscribe(Features.ActionType.UPDATE, function () { 
			app.featuresAvailable(Group.Cart).then((features) => { 
				$("#container2").append("<br></br> features <br></br>");
				$("#container2").append(JSON.stringify(features));

				const hasFetchCart = features.Cart[Cart.Action.FETCH]; 
				$("#container2").append("<br></br> hasFetchCart <br></br>");
				$("#container2").append(JSON.stringify(hasFetchCart));		
				if (hasFetchCart) { 
					//unsubscribe(); 
					$("#container2").append("<br></br> unsubscribe fetch cart<br></br>");
					// Do something 
					const cart = Cart.create(app); 
					$("#container2").append("<br></br> cart created<br></br>");
					$("#container2").append(JSON.stringify(cart));
					var unsubscriber = cart.subscribe(Cart.Action.UPDATE, function (payload) {
						$("#container2").append("<br></br> unsubscriber<br></br>");
						$("#container2").append(SON.stringify(payload));
						unsubscriber();
					});
					cart.dispatch(Cart.Action.FETCH);
					$("#container2").append("<br></br> unsubscriber after<br></br>");
				} 
			}); 
		});


		$("#container2").append("<br></br>Start END");*/
	</script>
 </body>

Then we are getting Output like below

POS outputPOS output

 

What do we need to do to enable the dispatch value to true? Kindly let us know if we are missing anything on our side.

Thanks.

0 Likes