> ## Documentation Index
> Fetch the complete documentation index at: https://help.experro.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Ecommerce service

The `EcommerceService` is used to get all the data related to the eCommerce operations such as get cart details, search products, add products to the wishlist. You can import the `EcommerceService` from the `experro-storefront` package.

```js theme={null}
import { EcommerceService } from 'experro-storefront';
```

Here is a list of functions that the `EcommerceService` provides.

1. [`getCart`](#getcart)
2. [`createCart`](#createcart)
3. [`updateCustomerId`](#updatecustomerid)
4. [`addToCart`](#addtocart)
5. [`updateCart`](#updatecart)
6. [`deleteItemInCart`](#deleteitemincart)
7. [`search`](#search)
8. [`facetedSearch`](#facetedSearch)
9. [`getFacetByCategoryName`](#getfacetbycategoryname)
10. [`getProductReviewsByProductId`](#getproductreviewsbyproductid)
11. [`addProductReview`](#addproductreview)
12. [`addCouponCode`](#addcouponcode)
13. [`removeCouponCodeById`](#removecouponcodeById)
14. [`createWishlist`](#createwishlist)
15. [`updateWishlist`](#updatewishlist)
16. [`deleteWishlist`](#deletewishlist)
17. [`getAllWishlists`](#getallwishlists)
18. [`getWishlistById`](#getwishlistbyid)
19. [`addItemToWishlist`](#additemtowishlist)
20. [`deleteItemFromWishlistById`](#deleteitemfromwishlistbyid)

***

## getCart

The function returns cart details.

```js theme={null}
const cartObj = await EcommerceService.getCart();
```

***

## createCart

The function is used to create a cart. It accepts an object with `customerId` and `line_items` as properties.

```js theme={null}
const cart = await EcommerceService.createCart({ customerId, line_items });
```

If the user is logged in, provide `userId` as a `customerId` and `line_items` is an array of the products object that need to be added in cart.

***

## updateCustomerId

The function updates the customer id for the existing cart. It accepts object with `customerId` and `cartId` the properties.

```js theme={null}
const cart = await EcommerceService.updateCustomerId({ customerId, cartId });
```

If the user is logged in, provide `userId` as a `customerId`.

***

## addToCart

The function adds a product to the cart. It accepts an object with `line_items` as the property.

```js theme={null}
const cart = await EcommerceService.addToCart({ line_items });
```

The `line_items` is an array of the product object.

***

## updateCart

The function updates the item in the cart. It accepts an object with `itemId` and `line_item` as the properties.

```js theme={null}
const cart = await EcommerceService.updateCart({ itemId, line_item });
```

The `itemId` is the id of the product item or `line_item` you want to update in the cart.

***

## deleteItemInCart

The function delete the items from the cart. It accepts an object with `itemId` as the property.

```js theme={null}
const cart = await EcommerceService.deleteItemInCart({ itemId });
```

***

## search

The function provide a ways to search the items or products available for the purchase.It accepts object with `searchObj` as property.

```js theme={null}
const result = await EcommerceService.search({ searchObj);
```

The `searchObj` can have the following properties.

```js theme={null}
searchObj: {
  body: {
    search_terms: text,
  },
  skip: 0,
  limit: 10,
  fieldsToQuery: 'images_ej, price_efi',
  sortBy: 'relevance',
  facet: []
},
```

| Property      | Description                       | Type   |
| ------------- | --------------------------------- | ------ |
| `search_term` | Search text                       | String |
| `skip`        | Skip the result (for pagination)  | Number |
| `limit`       | Limit the result (items per page) | Number |
| `sortBy`      | Sort the result by                | String |
| `facets`      | Filters applied to the search     | Array  |

***

## facetedSearch

This function returns facets based on search criteria. It accepts an object with `searchObj` property.

```js theme={null}
const result = await EcommerceService.facetedSearch({ searchObj });
```

***

## getFacetByCategoryName

The function returns facets based on searched category. It accepts string. The value of the string can **either** be the name of a category to search **or** `All` for a random search.

```js theme={null}
const result = await EcommerceService.getFacetByCategoryName(categoryName);
```

***

## getProductReviewsByProductId

The function returns reviews for the given product. It accepts an object with `productId` as the property.

```js theme={null}
const reviews = await EcommerceService.getProductReviewsByProductId({ productId });
```

***

## addProductReview

The function add a review for the given product. It accepts an object with `productId` and `body` as the properties.

```js theme={null}
const review = await EcommerceService.addProductReview({ productId, body });
```

The `productId` is the `provider_id_esi` of the particular product. `body` is an object with following properties.

| Property        | Description                       | Type   |
| --------------- | --------------------------------- | ------ |
| `title`         | Title of the review               | String |
| `date_reviewed` | Date of the submitting the review | Date   |
| `text`          | Review comment                    | String |
| `rating`        | Rating between 1 to 5             | Number |
| `name`          | Name of the user                  | String |
| `email`         | Email of the user                 | String |

```js theme={null}
body: {
  title: 'Title for the review',
  date_reviewed: '2023/12/03',
  text: 'Review comment',
  rating: 4,
  name: 'Kai Doe',
  email: 'kai@doe.com',
}
```

***

## addCouponCode

This function apply the coupon code for the products in the cart. It accepts an object with `body` as the property.

```js theme={null}
const result = await EcommerceService.addCouponCode({ body });
```

`body` is an object with following property

| Property      | Description | Type   |
| ------------- | ----------- | ------ |
| `coupon_code` | Coupon code | String |

```js theme={null}
body: {
  coupon_code: 'COUPON_CODE',
},
```

***

## removeCouponCodeById

The function removes the coupon code. It accepts an object with `couponId` as property.

```js theme={null}
const couponCode = await EcommerceService.removeCouponCodeById({ couponId });
```

The `couponId` is an id of the particular coupon that should be removed.

***

## createWishlist

This function create a new wishlist. It accepts an object with `body` property.

```js theme={null}
const wishlist = await EcommerceService.createWishlist({ body });
```

`body` is an object with following property.

| Property      | Description                       | Type    |
| ------------- | --------------------------------- | ------- |
| `customer_id` | Logged in customer id             | Number  |
| `is_public`   | Make this wishlist public or not  | Boolean |
| `name`        | Name of the wishlist              | String  |
| `items`       | Product items to add in wishlist. | Object  |

```js theme={null}
body: {
  customer_id: 12,
  is_public: false,
  name: "School Shopping",
  items: [
    {
      "product_id": 12,
      "variant_id": 152,
    },
  ],
}
```

***

## updateWishlist

This function update existing wishlist's items, name, and visibility. It accepts an object with `wishlistId` and `body` as the properties.

```js theme={null}
const wishlist = await EcommerceService.updateWishlist({ wishlistId, body });
```

`wishlistId` is an id of an existing wishlist. `body` is an object.

***

## deleteWishlist

This function deletes the wishlist. It accepts an object with `wishlistId` property.

```js theme={null}
const wishlist = await EcommerceService.deleteWishlist({ wishlistId });
```

***

## getAllWishlists

This function returns all the wishlist.

```js theme={null}
const wishlists = await EcommerceService.getAllWishlists();
```

***

## getWishlistById

This function returns `wishlist` by given id. It accepts an object with `wishlistId` as property.

```js theme={null}
const wishlist = await EcommerceService.getWishlistById({ wishlistId });
```

***

## addItemToWishlist

This function adds product item into an existing wishlist. The function accepts an object with `wishlistId` and `body` as properties.

```js theme={null}
const wishlist = await EcommerceService.addItemToWishlist({ wishlistId, body });
```

`wishlistId` is an id of the existing wishlist in which item needs to be added.

The `items` is an array of the object with `product_id` and `variant_id` properties.

```js theme={null}
{
  items: [
    {
      "product_id": 12,
      "variant_id": 152,
    },
  ],
}
```

***

## deleteItemFromWishlistById

This function deletes a particular item from the wishlist by item id. The function accepts `wishlistId` and `itemId` as properties.

```js theme={null}
const wishlist = await EcommerceService.deleteItemFromWishlistById({ wishlistId, itemId });
```

The `wishlistId` is an id of the existing wishlist and `itemId` is the product item id which is going to be deleted from wishlist.

***

## subscribeToNewsLetter

> This method is designed to handle newsletter subscriptions within the context of a BigCommerce store, for now.

This function serves as a valuable tool for incorporating newsletter subscription functionality into a BigCommerce store. You only need to provide an `email-id` as an input parameter to enable the integration.

```js theme={null}
 const formSubmit = await EcommerceService.subscribeToNewsLetter(data?.email);
```

Newsletter subscriptions for the BigCommerce store will be processed through this function. If the provided email ID is not already subscribed, the function will execute the subscription. Otherwise, it will return an appropriate error message.

***
