Add to Cart

The Add to Cart button click sends an call back to our Web API to indicate you are adding the product to your cart. While in the backend we are not performing any specific action to add this to the cart, here we will see how we can send a call back to services as POST request.

Add the following line of code just after the previous code block.

 GameObject cart = toolBar.transform.FindChild("AddToCart").gameObject;
ProductAddToCart prr =
cart.AddComponent<ProductAddToCart>();
prr.addToCartProduct = this.launchedObject.name;

The preceding code does the same operation in the toolbar control as we did for the Close button. Here, we find the child control name AddToCart and attached a Script called ProductAddToCart with the cart Game Object. Finally, pass the name of the product to your ProductAddToCart script, so that we can send this back to our WebAPI to indicate which product the user has purchased.

Similar to the ProductRemover class, create another script ProductAddToCart.cs inside the Scripts folder. The class must inherit from the InputClickHandler interface and implement the OnInputClicked() method as shown as follows:

public string addToCartProduct;
public void OnInputClicked(InputEventData eventData)
{
APIConnector.Instance.PurchaseProduct(addToCartProduct);
}

Here in an one-liner code block, which just call the Instance of APIConnector that we already created and call the method PurchaseProduct() by passing the product name.

The following is the new snippet that you need to add in the APIConnector. The API Connector class just send a post webRequest to the services end points with product name.

public void PurchaseProduct(string productName)
{
UnityWebRequest webRequest =
UnityWebRequest.Post("http://<<servicename>>
.azurewebsites.net/API/Purchase/AddToCart",
productName);
}
UnityWebRequest.Post() to send form data to a server via HTTP POST. In this case we are sending a Post call to our asset Web API with product name.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.12.140