At the end of [Routing](getting-started/routing "Getting Started: Routing"), the online store application has a product catalog with two views: a product list and product details.
Users can click on a product name from the list to see details in a new view, with a distinct URL (route).
In this section, you'll create the shopping cart. You'll:
* Update the product details page to include a "Buy" button, which adds the current product to a list of products managed by a cart service.
* Add a cart component, which displays the items you added to your cart.
* Add a shipping component, which retrieves shipping prices for the items in the cart by using Angular's HttpClient to retrieve shipping data from a `.json` file.
{@a services}
## Services
Services are an integral part of Angular applications. In Angular, a service is an instance of a class that can be made available to any part of your application using Angular's [dependency injection system](guide/glossary#dependency-injection-di "dependency injection definition").
Services are the place where you share data between parts of your application. For the online store, the cart service is where you store your cart data and methods.
{@a create-cart-service}
## Create the shopping cart service
Up to this point, users can view product information, and simulate sharing and being notified about product changes. They cannot, however, buy products.
In this section, you'll add a "Buy" button the product details page.
You'll also set up a cart service to store information about products in the cart.
<divclass="alert is-helpful">
Later, in the [Forms](getting-started/forms "Getting Started: Forms") part of this tutorial, this cart service also will be accessed from the page where the user checks out.
</div>
{@a generate-cart-service}
### Define a cart service
1. Generate a cart service.
1. Right click on the `app` folder, choose `Angular Generator`, and choose `**Service**`. Name the new service `cart`.
To check: StackBlitz includes the constructor. If it's important (and not obvious) that the methods be below the constructor, then we should show it or say something.
-->
<!--
* The `addToCart()` method appends a product to an array of `items`.
* The `getItems()` method collects the items added to the cart and returns each item with its associated quantity.
* The `clearCart()` method returns an empty array of items.
-->
{@a product-details-use-cart-service}
### Use the cart service
In this section, you'll update the product details component to use the cart service.
You'll add a "Buy" button to the product details view.
When the "Buy" button is clicked, you'll use the cart service to add the current product to the cart.
1. Open `product-details.component.ts`.
1. Set up the component to be able to use the cart service.
To create the cart page, you begin by following the same steps you did to create the product details component and to set up routing for the new component.
To do: Can we shorten the example code to remove the extra at the bottom?
-->
1. To see the new cart component, click the "Checkout" button. You can see the "cart works!" default text, and the URL has the pattern `https://getting-started.stackblitz.io/cart`, where `getting-started.stackblitz.io` may be different for your StackBlitz project.
(Note: The "Checkout" button that we provided in the top-bar component was already configured with a `routerLink` for `/cart`.)
<figure>
<imgsrc='generated/images/guide/getting-started/cart-works.png'alt="Display cart page before customizing">
</figure>
### Display the cart items
Services can be used to share data across components:
* The product details component already uses the cart service (`CartService`) to add products to the cart.
* In this section, you'll update the cart component to use the cart service to display the products in the cart.
1. Open `cart.component.ts`.
1. Set up the component to be able to use the cart service. (This is the same way you set up the product details component to use the cart service, above.)
1. Import the `CartService` from the `cart.service.ts` file.
1. Set the items using the cart service's `getItems()` method. (You defined this method [when you generated `cart.service.ts`](#generate-cart-service).)
The resulting `CartComponent` class should look like this:
1. Click on "My Store" to go to the product list page.
1. Click on a product name to display its details.
1. Click "Buy" to add the product to the cart.
1. Click "Checkout" to see the cart.
1. To add another product, click "My Store" to return to the product list. Repeat the steps above.
<figure>
<imgsrc='generated/images/guide/getting-started/cart-page-full.png'alt="Cart page with products added">
</figure>
<divclass="alert is-helpful">
StackBlitz tip: Any time the preview refreshes, the cart is cleared. If you make changes to the app, the page refreshes, and you'll need to buy products again to populate the cart.
</div>
<!--
To do: New screen shot. No shipping prices link yet. Show a few products in the cart.
-->
<divclass="alert is-helpful">
Learn more: See [Introduction to Services and Dependency Injection](guide/architecture-services "Architecture > Intro to Services and DI") for more information about services.
</div>
## Retrieve shipping prices
<!-- Accessing data with the HTTP client -->
Data returned from servers often takes the form of a stream.
Streams are useful because they make it easy to transform the data that is returned, and to make modifications to the way data is requested.
The Angular HTTP client (`HttpClient`) is a built-in way to fetch data from external APIs and provide them to your application as a stream.
In this section, you'll use the HTTP client to retrieve shipping prices from an external file.
### Predefined shipping data
For the purpose of this Getting Started, we have provided shipping data in `assets/shipping.json`.
You'll use this data to add shipping prices for items in the cart.
To do: Should ReactiveFormsModule already be here? Currently, it is in the starter stackblitz, so this doc assumes it is already included and not added in the forms section.
-->
<!--
To do: Should ReactiveFormsModule already be here?
-->
### Enable HttpClient for cart service
1. Open `cart.service.ts`.
1. Import `HttpClient` from the `@angular/common/http` package.
As you've seen, multiple components can leverage the same service.
Later in this tutorial, the shipping component will use the cart service to retrieve shipping data via HTTP from the `shipping.json` file.
Here you'll define the `get()` method that will be used.
1. Continue working in `cart.service.ts`.
1. Below the `clearCart()` method, define a new `getShippingPrices()` method that uses the `HttpClient#get()` method to retrieve the shipping data (types and prices).
The new shipping component isn't hooked into any other component yet, but you can see it in the preview pane by entering the URL specified by its route. The URL has the pattern: `https://getting-started.stackblitz.io/shipping` where the `getting-started.stackblitz.io` part may be different for your StackBlitz project.
1. Modify the shipping component so it uses the cart service to retrieve shipping data via HTTP from the `shipping.json` file.
Congratulations! You have an online store application with a product catalog and shopping cart. You also have the ability to look up and display shipping prices.
To continue exploring Angular, choose either of the following options:
* [Continue to the "Forms" section](getting-started/forms "Getting Started: Forms") to finish the app by adding the shopping cart page and a form-based checkout feature. You'll create a form to collect user information as part of checkout.
* [Skip ahead to the "Deployment" section](getting-started/deployment "Getting Started: Deployment") to deploy your app to Firebase or move to local development.