🚀 Ship faster with premium components 🚀
Search documentation v1.3.0

Cart

Display a shopping cart with dynamic summaries

Source Report Issue
Command docs
How to add this block to your project
npm run add Cart

Use the Cart component to display a shopping cart in your Astro project. It allows users to view products they intend to purchase, adjust quantities, apply coupon codes, and see a summary of their order including subtotal, discounts, VAT, and total price.

Cart
  • Product 1 Product 1
    $59.98
  • Product 1 Product 2
    $49.99
Summary
Subtotal $109.97
VAT $13.20
Discount -$21.99
Total $101.17
Unlock code
How to use the Cart component in Astro
---
const products = [
{
img: {
src: '/images/product1.jpg',
alt: 'Product 1',
width: 50,
height: 50,
lazy: true
},
href: '/products/product1',
target: '_blank',
id: 1,
name: 'Product 1',
price: 29.99,
quantity: 2
},
{ ... }
]
---
<Cart
title="Cart"
products={products}
discount={20}
vat={15}
/>

The Cart component only requires a products prop, which is an array of product objects. Each product can be created without an image, link, or target. The only required properties are id, name, price, and quantity.

The Cart component also extends the Card component, so you can pass any CardProps to customize its appearance, such as a title.

Vat and Discount

In the above example, we set a vat of 15 and a discount of 20. These values represent percentages that will be applied to the total price of the products in the cart. When both values are provided, the discount is applied first, followed by the VAT calculation on the discounted amount. You can also use either one of them or none at all.

Discount only
  • Product 1 Product 1
    $59.98
  • Product 1 Product 2
    $49.99
Summary
Subtotal $109.97
Discount -$16.50
Total $93.47
VAT only
  • Product 1 Product 1
    $59.98
  • Product 1 Product 2
    $49.99
Summary
Subtotal $109.97
VAT $16.50
Total $126.47
Unlock code
How to use discounts
<Cart
products={products}
discount={15}
/>
Unlock code
How to use VAT
<Cart
products={products}
vat={15}
/>

Pricing Symbols

By default, the Cart component uses the dollar sign ($) as the currency symbol. You can change this by using the priceSymbol prop. Additionally, you can set the position of the currency symbol using the symbolPosition prop, which can be either prefix (default) or postfix.

Pounds
  • Product 1 Product 1
    £59.98
  • Product 1 Product 2
    £49.99
Summary
Subtotal £109.97
VAT £14.02
Discount -£16.50
Total £107.50
Euros with postfix
  • Product 1 Product 1
    59.98€
  • Product 1 Product 2
    49.99€
Summary
Subtotal 109.97€
VAT 14.02€
Discount -16.50€
Total 107.50€
Unlock code
How to set price symbols
<Cart
products={products}
discount={15}
vat={15}
priceSymbol="£"
/>
Unlock code
How to change symbol position
<Cart
products={products}
discount={15}
vat={15}
priceSymbol=""
symbolPosition="postfix"
/>

Counters

The Cart component uses the Counter component for its counters, meaning you can customize it by passing your own CounterProps settings via the counter prop.

Custom counters
  • Product 1 Product 1
    $59.98
  • Product 1 Product 2
    $49.99
Summary
Subtotal $109.97
Total $109.97
Disabled counters
  • Product 1 Product 1
    $59.98
  • Product 1 Product 2
    $49.99
Summary
Subtotal $109.97
Total $109.97
Unlock code
How to customize counters
<Cart
products={products}
counter={{ theme: 'success', rounded: true }}
/>
Unlock code
How to disable counters
<Cart
products={products}
counter={{ disabled: true }}
/>

Coupon Code

The Cart component includes a coupon code section that allows your users to enter and apply coupon codes for discounts. You can customize the input field and button used in this section by passing your own InputProps and ButtonProps config via the coupon.input and coupon.button props, respectively.

Cart with custom coupon settings
  • Product 1 Product 1
    $59.98
  • Product 1 Product 2
    $49.99
Summary
Subtotal $109.97
Total $109.97
Unlock code
How to customize coupon code
<Cart
products={products}
coupon={{
input: {
placeholder: 'Apply a coupon'
},
button: {
text: 'Add'
}
}}
/>

If you want to hide the coupon code section, you can set the coupon.show prop to false. Note that you will need to handle coupon code functionality in your own code as the Cart component does not include built-in logic for validating coupon codes.

You can apply the discount by updating this line in your component. For example, set the value to 20 to apply a 20% discount when a valid coupon code is entered. Make sure to implement your own validation logic for the coupon code before applying the discount.

Summary and Total

The summary section displays the subtotal, discount, VAT, and total price of the products in the cart. You can customize the labels and visibility of these items using the summary and total props:

Customized summary
  • Product 1 Product 1
    $59.98
  • Product 1 Product 2
    $49.99
Details:
Sub -> $109.97
Tax -> $13.20
Cut -> -$21.99
Due today: $101.17
Unlock code
How to customize the total and summary fields
<Cart
products={products}
discount={20}
vat={15}
total={{ label: 'Due today:' }}
summary={{
subtotalLabel: 'Sub ->',
discountLabel: 'Cut ->',
vatLabel: 'Tax ->',
title: 'Details:'
}}
/>

Each of these fields can be hidden by setting their respective show properties to false. For example, to hide the discount from the summary, set summary.showDiscount to false. You can also hide the entire summary section or the total price section by setting summary.show or total.show to false.

CTA Buttons

Lastly, you can add call-to-action buttons at the bottom of the cart using the buttons prop. This prop accepts an array of button configurations (in the form of ButtonProps), allowing you to define multiple buttons with different themes and actions.

CTA buttons
  • Product 1 Product 1
    $59.98
  • Product 1 Product 2
    $49.99
Summary
Subtotal $109.97
VAT $15.40
Discount -$32.99
Total $92.37
Unlock code
How to define CTA buttons
<Cart
products={products}
vat={20}
discount={30}
buttons={[
{ text: 'Checkout', href: '/checkout' },
{ text: 'Keep shopping', theme: 'secondary' , href: '/products' }
]}
/>

You can also combine this component with a Modal or Sheet component to create a cart overlay, or use the Popover component to create a mini cart in the navigation bar.

Events

In case you need to handle additional logic when the cart changes (e.g., updating the total number of items in a cart icon), the Cart component dispatches a cartOnChange event that you can listen to:

Unlock code
How to listen to cart change events
<script>
import { listen } from 'webcoreui'
listen('cartOnChange', event => console.log('Cart changed:', event))
</script>

The event object contains the following properties:

You can also dispatch additional information by changing this line in your component.

API

Unlock code
Component API reference
type Product = {
img?: {
src: string
alt: string
width: number
height: number
lazy?: boolean
}
href?: string
target?: '_self'
| '_blank'
| '_parent'
| '_top'
| '_unfencedTop'
id: number
name: string
price: number
quantity: number
}
type CartProps = {
products: Product[]
priceSymbol?: string
symbolPosition?: 'prefix' | 'postfix'
vat?: number
discount?: number
counter?: CounterProps
coupon?: {
show?: boolean
input?: InputProps
button?: {
text: string
icon?: string
} & ButtonProps
}
summary?: {
show?: boolean
showSubtotal?: boolean
showDiscount?: boolean
showVat?: boolean
subtotalLabel?: string
discountLabel?: string
vatLabel?: string
title?: string
}
total?: {
show?: boolean
label?: string
}
buttons?: ({
text: string
icon?: string
} & ButtonProps)[]
} & CardProps
PropDefaultPurpose
products [] Sets the products in the cart.
priceSymbol $ Sets the currency symbol for the prices.
symbolPosition prefix Sets the position of the currency symbol. Can be either prefix or postfix.
vat 0 Sets the VAT percentage to be applied to the discounted price.
discount 0 Sets the discount percentage to be applied to the total price.
counter - Sets the Counter component used for product quantity selection.
coupon.show true Sets whether to show the coupon code section.
coupon.input - Sets the Input component used for entering the coupon code.
coupon.button.text Apply Sets the text to be displayed on the coupon apply button.
coupon.button.icon - Sets the icon to be displayed on the coupon apply button.
summary.show true Sets whether to show the summary section.
summary.showSubtotal true Sets whether to show the subtotal in the summary.
summary.showDiscount true Sets whether to show the discount in the summary.
summary.showVat true Sets whether to show the VAT in the summary.
summary.subtotalLabel Subtotal Sets the label for the subtotal line item.
summary.discountLabel Discount Sets the label for the discount line item.
summary.vatLabel VAT Sets the label for the VAT line item.
summary.title Summary Sets the title for the summary section.
total.show true Sets whether to show the total price section.
total.label Total Sets the label for the total price.
buttons.text - Sets the text to be displayed on a CTA button.
buttons.icon - Sets the icon to be displayed on a CTA button.
className - Pass additional CSS classes for the component.
Product PropsPurpose
img Sets an image for the product.
img.src Sets the path for the image.
img.alt Sets an alternate text for the image.
img.width Sets the width of the image.
img.height Sets the height of the image.
img.lazy Set to true to enable lazy loading for the image.
href Sets a link for the product name.
target Sets the target of the link.
id Sets a unique identifier for the product.
name Sets the name of the product.
price Sets the price of the product.
quantity Sets the quantity of the product.