Next Commerce
Data AttributesActions

Cart Actions

Every action that changes cart contents is triggered by a data-next-action attribute. The element can be a button, link, image, or any other clickable element.


add-to-cart

The most common action. Adds a package to the cart.

<button
  data-next-action="add-to-cart"
  data-next-package-id="42"
  data-next-quantity="1">
  Add to Cart
</button>
AttributeRequiredDescription
data-next-package-idyesPackage ref_id to add
data-next-quantitynoQuantity to add. Default 1
data-next-clear-cartnoClear the cart before adding. Default false
data-next-urlnoURL to redirect to after the cart write succeeds
data-next-selector-idnoRead the package ID from a linked selector instead of data-next-package-id

When data-next-selector-id is present, the button reads the currently selected card from the matching selector. See Cart System → Buttons for the full pattern.


data-next-toggle="cart"

Toggle a package between in-cart and not-in-cart. Perfect for wishlist-style buttons.

<div
  data-next-toggle="cart"
  data-next-package-id="42"
  class="product-card">
  <span data-add-text="♡ Add to favorites">♡ Add to favorites</span>
  <span data-remove-text="♥ In favorites">♥ In favorites</span>
</div>

The SDK:

  • Adds next-in-cart to the element when the package is in the cart
  • Toggles between add and remove on click
  • Updates ARIA states for screen readers
AttributeDescription
data-next-toggle="cart"Activates the toggle
data-next-package-idPackage to toggle
data-next-quantityQuantity used when adding
data-next-package-syncComma-separated package IDs whose total quantity should mirror this toggle (e.g. one warranty per product). See Cart System → Buttons
data-add-text / data-remove-textText shown in each state

remove

Explicitly remove a package from the cart. Use this when you want a dedicated remove button rather than a toggle.

<button
  data-next-action="remove"
  data-next-package-id="42">
  Remove
</button>

The button is auto-disabled (next-disabled class) when the package is not currently in the cart.


clear-cart

Empty the entire cart. Useful for "start over" buttons.

<button data-next-action="clear-cart">
  Empty Cart
</button>

No package ID is needed — this clears every item.


swap

Swap the cart contents with a single package. Equivalent to clearing the cart and adding one package, but in a single store update so display elements only re-render once.

<button
  data-next-action="swap"
  data-next-package-id="56">
  Switch to Premium
</button>

For multi-package swaps, use a Bundle Selector instead — BundleSelectorEnhancer is the right tool for atomic multi-package writes.

Do not use swap to chain a clear-cart + add-to-cart from JavaScript. The atomic store update is the whole point — using clear-cart + add-to-cart separately produces two re-renders and one momentary "empty cart" state.


State Containers

Wrap a button in a state container to switch markup based on cart state. The container reads data-next-package-id from itself or a child action button.

<div data-next-state-container data-next-package-id="42">
  <div data-next-state="not-in-cart">
    <button data-next-action="add-to-cart" data-next-package-id="42">
      Add to Cart
    </button>
  </div>

  <div data-next-state="in-cart">
    ✓ Already in your cart
    <button data-next-action="remove" data-next-package-id="42">
      Remove
    </button>
  </div>
</div>

The container shows whichever child matches the current state. See Cart System → Buttons for the full pattern.


Conflicts

Do not combine these on the same element:

CombinationWhy it breaks
add-to-cart + data-next-toggle="cart"Both fire on click. The toggle wins, the add-to-cart never runs.
add-to-cart + accept-upsellOne writes to the cart store, the other writes to the order API. Same package added twice.
swap on the same page as BundleSelectorEnhancer for multi-package bundlesUse the bundle selector — it is the atomic swap mechanism for bundles.

Next Steps

On this page