You are reading the documentation for SDK v2, which is no longer the latest version. Switch to current
removeProductFromCart
Method for removing product from cart.
This method sends a POST request to the removeFromCart endpoint of the commercetools API. It allows you to remove a product from a cart cart in commercetools by sending a request to its GraphQL API. The default GraphQL query used by this method can be found here.
In case you need to perform a more complex cart update with multiple update actions at once, use the updateCart method instead.
Signature
export declare function removeProductFromCart<Res extends PartialRemoveProductFromCartResponse = RemoveProductFromCartResponse>(
params: RemoveProductFromCartParams,
options?: MethodOptions<CustomQuery<"updateCart">>
): Promise<Res>;Parameters
| Name | Required | Type | Description |
|---|---|---|---|
params | Required | RemoveProductFromCartParams | Parameter object which can be used with this method. Refer to its type definition to learn about possible properties. |
options | Optional | MethodOptions<CustomQuery<"updateCart">> | Options that can be passed to additionally configure the request or customize the logic in a plugin. |
Returns
Returns a representation of the RemoveProductFromCartResponse.
Referenced Types
- PartialRemoveProductFromCartResponse
- RemoveProductFromCartResponse
- RemoveProductFromCartParams
- MethodOptions
- CustomQuery
Examples
Removing a product from a cart.
import { sdk } from '~/sdk.config.ts';
const { cart } = await sdk.commerce.removeProductFromCart({
cartId: 'cart-id',
cartVersion: 1,
product,
quantity: 1
});Creating a custom GraphQL query for the method in middleware.config.js.
module.exports = {
integrations: {
ct: {
// ...
customQueries: {
'remove-product-from-cart-custom-query': ({ variables, metadata }) => {
return {
variables,
query: `
mutation updateCart(
$id: String!,
$version: Long!,
$actions: [MyCartUpdateAction!]!,
) {
cart: updateMyCart(id: $id, version: $version, actions: $actions) {
${metadata}
}
}`
};
},
}
}
}
};Using the custom GraphQL query in the method. customQuery parameter to leverage the custom GraphQL query from the previous example.
import { sdk } from '~/sdk.config.ts';
const { cart } = await sdk.commerce.removeProductFromCart({
cartId: 'cart-id',
cartVersion: 1,
product,
quantity: 1
}, {
customQuery: 'remove-product-from-cart-custom-query'
metadata: 'id, version, lineItems { id, quantity, variant { id, sku } }'
});