Skip to content

Cart Manipulation script services reference

Cart Manipulation script services reference

services.cart (Shopware\Core\Checkout\Cart\Facade\CartFacade)

The cart service allows you to manipulate the cart. You can use the cart service to add line-items, change prices, add discounts, etc. to the cart.

items()

products()

  • The product() method returns all products of the current cart for further manipulation.

    Similar to the items() method, but the line-items are filtered, to only contain product line items.

  • Returns Shopware\Core\Checkout\Cart\Facade\ProductsFacade

    A ProductsFacade containing all product line-items in the current cart as a collection.

calculate()

  • The calculate() method recalculates the whole cart.

    Use this to get the correct prices after you made changes to the cart. Note that after calling the calculate() all collections (e.g. items(), products()) get new references, so if you still hold references to things inside the cart, these are outdated after calling calculate().

    The calculate() method will be called automatically after your cart script executed.

price()

  • The price() method returns the current price of the cart.

    Note that this price may be outdated, if you changed something inside the cart in your script. Use the calculate() method to recalculate the cart and update the price.

  • Returns Shopware\Core\Checkout\Cart\Facade\CartPriceFacade

    The calculated price of the cart.

errors()

  • The errors() method returns the current errors of the cart.

    You can use it to add new errors or warning or to remove existing ones.

  • Returns Shopware\Core\Checkout\Cart\Facade\ErrorsFacade

    A ErrorsFacade containing all cart errors as a collection (may be an empty collection if there are no errors)

states()

discount()

  • The discount() methods creates a new discount line-item with the given type and value.

  • Returns Shopware\Core\Checkout\Cart\Facade\DiscountFacade

    Returns the newly created discount line-item.

  • Arguments:

    • string key: The id for the new discount.
    • string type: The type of the discount, e.g. percentage, absolute
    • float|\PriceCollection value: The value of the discount, a float for percentage discounts or a PriceCollection for absolute discounts.
    • string label: The label of the discount line-item.
  • Examples:

    • Add an absolute discount to the cart.

      twig
      {# @var services \Shopware\Core\Framework\Script\ServiceStubs #}
      {% do services.cart.products.add(hook.ids.get('p1')) %}
      
      {% if services.cart.items.count <= 0 %}
          {% return %}
      {% endif %}
      
      {% if services.cart.items.has('my-discount') %}
          {% return %}
      {% endif %}
      
      {% set price = services.cart.price.create({
          'default': { 'gross': -19.99, 'net': -19.99}
      }) %}
      
      {% do services.cart.discount('my-discount', 'absolute', price, 'Fanzy discount') %}
    • Add a relative discount to the cart.

      twig
      {# @var services \Shopware\Core\Framework\Script\ServiceStubs #}
      
      {% do services.cart.products.add(hook.ids.get('p1')) %}
      
      {% if services.cart.has('my-discount') %}
          {% return %}
      {% endif %}
      
      {% do services.cart.discount('my-discount', 'percentage', -10, 'Fanzy discount') %}

surcharge()

  • The surcharge() methods creates a new surcharge line-item with the given type and value.

  • Returns Shopware\Core\Checkout\Cart\Facade\DiscountFacade

    Returns the newly created surcharge line-item.

  • Arguments:

    • string key: The id for the new surcharge.
    • string type: The type of the surcharge, e.g. percentage, absolute
    • float|\PriceCollection value: The value of the surcharge, a float for percentage surcharges or a PriceCollection for absolute surcharges.
    • string label: The label of the surcharge line-item.
  • Examples:

    • Add an absolute surcharge to the cart.#

      twig
      {# @var services \Shopware\Core\Framework\Script\ServiceStubs #}
      {% do services.cart.products.add(hook.ids.get('p1')) %}
      
      {% set price = services.cart.price.create({
          'default': { 'gross': 19.99, 'net': 19.99}
      }) %}
      
      {% do services.cart.surcharge('my-surcharge', 'absolute', price, 'Fanzy surcharge') %}
    • Add a relative surcharge to the cart.

      twig
      {# @var services \Shopware\Core\Framework\Script\ServiceStubs #}
      
      {% do services.cart.products.add(hook.ids.get('p1')) %}
      
      {% if services.cart.has('my-surcharge') %}
          {% return %}
      {% endif %}
      
      {% do services.cart.surcharge('my-surcharge', 'percentage', -10, 'Fanzy discount') %}

get()

  • get() returns the line-item with the given id from this collection.

  • Returns Shopware\Core\Checkout\Cart\Facade\ItemFacade | null

    The line-item with the given id, or null if it does not exist.

  • Arguments:

    • string id: The id of the line-item that should be returned.

remove()

  • remove() removes the given line-item or the line-item with the given id from this collection.

  • Arguments:

    • string|\ItemFacade id: The id of the line-item or the line-item that should be removed.
  • Examples:

    • Add and then remove a product line-item from the cart.

      twig
      {% do services.cart.products.add(hook.ids.get('p1')) %}
      
      {% do services.cart.products.remove(hook.ids.get('p1')) %}

has()

  • has() checks if a line-item with the given id exists in this collection.

  • Returns bool

    Returns true if the given line-item or a line-item with the given id already exists in the collection, false otherwise.

  • Arguments:

    • string|\ItemFacade id: The id or a line-item that should be checked if it already exists in the collection.

count()

  • count() returns the count of line-items in this collection.

    Note that it does only count the line-items directly in this collection and not child line-items of those.

  • Returns int

    The number of line-items in this collection.


Shopware\Core\Checkout\Cart\Facade\CartPriceFacade

The CartPriceFacade is a wrapper around the calculated price of a cart.

getNet()

  • getNet() returns the net price of the cart.

  • Returns float

    Returns the net price of the cart as float.

getTotal()

  • getTotal() returns the total price of the cart that has to be paid by the customer.

    Depending on the tax settings this may be the gross or net price. Note that this price is already rounded, to get the raw price before rounding use getRaw().

  • Returns float

    The rounded total price of the cart as float.

getPosition()

  • getPosition() returns the sum price of all line-items in the cart.

    In the position price the shipping costs are excluded. Depending on the tax settings this may be the gross or net price og the line-items.

  • Returns float

    The position price as float.

getRounded()

  • Alias for getTotal().

  • Returns float

    The rounded total price of the cart as float.

getRaw()

  • `getRaw() returns the total price of the cart before rounding.

  • Returns float

    The total price before rounding as float.

create()

  • create() creates a new PriceCollection based on an array of prices.

  • Returns Shopware\Core\Framework\DataAbstractionLayer\Pricing\PriceCollection

    Returns the newly created PriceCollection.

  • Arguments:

    • array price: The prices for the new collection, indexed by the currency-id or iso-code of the currency.
  • Examples:

    • Create a new Price in the default currency.

      twig
      {% set price = services.cart.price.create({
          'default': { 'gross': 19.99, 'net': 19.99}
      }) %}

Shopware\Core\Checkout\Cart\Facade\DiscountFacade

The DiscountFacade is a wrapper around a newly created discount. Note that this wrapper is independent from the line-item that was added for this discount.

getId()

  • getId() returns the id of the line-item that was added with this discount.

  • Returns string

    The id of the discount line-item.

getLabel()

  • getLabel() returns the translated label of the line-item that was added with this discount.

  • Returns string | null

    The translated label of the discount line-item.


Shopware\Core\Checkout\Cart\Facade\ErrorsFacade

The ErrorsFacade is a wrapper around the errors of a cart. You can use it to add new errors to the cart or remove existing ones.

error()

  • The error() method adds a new error of type error to the cart.

    The error will be displayed to the user and the checkout will be blocked if at least one error was added.

  • Arguments:

    • string key: The snippet-key of the message that should be displayed to the user.

    • string | null id: An optional id that can be used to reference the error, if none is provided the $key will be used as id.

      Default: null

    • array parameters: Optional: Any parameters that the snippet for the error message may need.

      Default: array ( )

  • Examples:

    • Add a error to the cart.

      twig
      {% do services.cart.errors.error('NO_PRODUCTS_IN_CART') %}

warning()

  • The warning() method adds a new error of type warning to the cart.

    The warning will be displayed to the user, but the checkout won't be blocked.

  • Arguments:

    • string key: The snippet-key of the message that should be displayed to the user.

    • string | null id: An optional id that can be used to reference the error, if none is provided the $key will be used as id.

      Default: null

    • array parameters: Optional: Any parameters that the snippet for the error message may need.

      Default: array ( )

  • Examples:

    • Add a warning to the cart.

      twig
      {% do services.cart.errors.notice('YOU_SHOULD_REALLY_ADD_PRODUCTS') %}

notice()

  • The notice() method adds a new error of type notice to the cart.

    The notice will be displayed to the user, but the checkout won't be blocked.

  • Arguments:

    • string key: The snippet-key of the message that should be displayed to the user.

    • string | null id: An optional id that can be used to reference the error, if none is provided the $key will be used as id.

      Default: null

    • array parameters: Optional: Any parameters that the snippet for the error message may need.

      Default: array ( )

  • Examples:

    • Add a notice to the cart.

      twig
      {% do services.cart.errors.warning('ADD_PRODUCTS_OR_GO_AWAY') %}
    • Add a notice to the cart with a custom id.

      twig
      {% do services.cart.errors.notice('YOU_SHOULD_REALLY_ADD_PRODUCTS', 'add-same-message') %}
    • Add a notice to the cart with parameters.

      twig
      {% do services.cart.errors.notice('MESSAGE_WITH_PARAMETERS', null, {'foo': 'bar'}) %}

has()

  • The has() method, checks if an error with a given id exists.

  • Returns bool

    Returns true if an error with that key exists, false otherwise.

  • Arguments:

    • string id: The id of the error that should be checked.

remove()

  • The remove() method removes the error with the given id.

  • Arguments:

    • string id: The id of the error that should be removed.

get()

  • The get() method returns the error with the given id.

  • Returns Shopware\Core\Checkout\Cart\Error\Error | null

    The Error with the given id, null if an error with that id does not exist.

  • Arguments:

    • string id: The id of the error that should be returned.

Shopware\Core\Checkout\Cart\Facade\ItemFacade

The ItemFacade is a wrapper around one line-item.

getPrice()

take()

  • take() splits an existing line-item by a given quantity.

    It removes the given quantity from the existing line-item and returns a new line-item with exactly that quantity.

  • Returns Shopware\Core\Checkout\Cart\Facade\ItemFacade | null

    Returns the new line-item as an ItemFacade or null if taking is not possible because the line-item has no sufficient quantity.

  • Arguments:

    • int quantity: The quantity that should be taken.

    • string | null key: Optional: The id of the new line-item. A random UUID will be used if none is provided.

      Default: null

  • Examples:

    • Take a quantity of 2 from an existing product line-item and add it to the cart again.

      twig
      {# @var services \Shopware\Core\Framework\Script\ServiceStubs #}
      
      {% do services.cart.products.add(hook.ids.get('p1'), 5) %}
      
      {% set product = services.cart.products.get(hook.ids.get('p1')) %}
      
      {% set split = product.take(2, 'new-key') %}
      
      {% do services.cart.products.add(split) %}

getId()

  • getId() returns the id of the line-item.

  • Returns string

    Returns the id.

getReferencedId()

  • getReferenceId() returns the id of the referenced entity of the line-item.

    E.g. for product line-items this will return the id of the referenced product.

  • Returns string | null

    Returns the id of the referenced entity, or null if no entity is referenced.

getQuantity()

  • getQuantity() returns the quantity of the line-item.

  • Returns int

    Returns the quantity.

getLabel()

  • getLabel() returns the translated label of the line-item.

  • Returns string | null

    Returns the translated label, or null if none exists.

getPayload()

getChildren()

getType()

  • getType() returns the type of this line-item.

    Possible types include product, discount, container, etc.

  • Returns string

    The type of the line-item.


Shopware\Core\Checkout\Cart\Facade\ItemsFacade

The ItemsFacade is a wrapper around a collection of line-items.

add()

  • add() adds a line-item to this collection.

  • Returns Shopware\Core\Checkout\Cart\Facade\ItemFacade

    Returns the added line-item.

  • Arguments:

  • Examples:

    • Add an absolute discount to the cart.

      twig
      {# @var services \Shopware\Core\Framework\Script\ServiceStubs #}
      {% do services.cart.products.add(hook.ids.get('p1')) %}
      
      {% if services.cart.items.count <= 0 %}
          {% return %}
      {% endif %}
      
      {% if services.cart.items.has('my-discount') %}
          {% return %}
      {% endif %}
      
      {% set price = services.cart.price.create({
          'default': { 'gross': -19.99, 'net': -19.99}
      }) %}
      
      {% do services.cart.discount('my-discount', 'absolute', price, 'Fanzy discount') %}

get()

  • get() returns the line-item with the given id from this collection.

  • Returns Shopware\Core\Checkout\Cart\Facade\ItemFacade | null

    The line-item with the given id, or null if it does not exist.

  • Arguments:

    • string id: The id of the line-item that should be returned.

has()

  • has() checks if a line-item with the given id exists in this collection.

  • Returns bool

    Returns true if the given line-item or a line-item with the given id already exists in the collection, false otherwise.

  • Arguments:

    • string|\ItemFacade id: The id or a line-item that should be checked if it already exists in the collection.

remove()

  • remove() removes the given line-item or the line-item with the given id from this collection.

  • Arguments:

    • string|\ItemFacade id: The id of the line-item or the line-item that should be removed.
  • Examples:

    • Add and then remove a product line-item from the cart.

      twig
      {% do services.cart.products.add(hook.ids.get('p1')) %}
      
      {% do services.cart.products.remove(hook.ids.get('p1')) %}

count()

  • count() returns the count of line-items in this collection.

    Note that it does only count the line-items directly in this collection and not child line-items of those.

  • Returns int

    The number of line-items in this collection.


Shopware\Core\Checkout\Cart\Facade\PriceFacade

The PriceFacade is a wrapper around a price.

getTotal()

  • getTotal() returns the total price for the line-item.

  • Returns float

    The total price as float.

getUnit()

  • getUnit() returns the unit price for the line-item.

    This is equivalent to the total price of the line-item with the quantity 1.

  • Returns float

    The price per unit as float.

getQuantity()

  • getQuantity() returns the quantity that was used to calculate the total price.

  • Returns int

    Returns the quantity.

create()

  • create() creates a new PriceCollection based on an array of prices.

  • Returns Shopware\Core\Framework\DataAbstractionLayer\Pricing\PriceCollection

    Returns the newly created PriceCollection.

  • Arguments:

    • array price: The prices for the new collection, indexed by the currency-id or iso-code of the currency.
  • Examples:

    • Create a new Price in the default currency.

      twig
      {% set price = services.cart.price.create({
          'default': { 'gross': 19.99, 'net': 19.99}
      }) %}

Shopware\Core\Checkout\Cart\Facade\ProductsFacade

The ProductsFacade is a wrapper around a collection of product line-items.

get()

  • get() returns the product line-item with the given product id.

  • Returns Shopware\Core\Checkout\Cart\Facade\ItemFacade | null

    The line-item associated with the given product id, or null if it does not exist.

  • Arguments:

    • string productId: The id of the product, of which the line-item should be returned.
  • Examples:

    • Get a product line-item by id.

      twig
      {% set product = services.cart.products.get(hook.ids.get('p1')) %}

add()

  • add() adds a new product line-item to this collection.

    In the case only a product id is provided it will create a new line-item from type product for the given product id.

  • Returns Shopware\Core\Checkout\Cart\Facade\ItemFacade

    The newly added product line-item.

  • Arguments:

    • string|\LineItem|\ItemFacade product: The product that should be added. Either an existing ItemFacade or LineItem or alternatively the id of a product.

    • int quantity: Optionally provide the quantity with which the product line-item should be created, defaults to 1.

      Default: 1

  • Examples:

    • Add a product to the cart by id.

      twig
      {% do services.cart.products.add(hook.ids.get('p1')) %}

create()

  • create() creates a new product line-item for the product with the given id in the given quantity.

    Note that the created line-item will not be added automatically to this collection, use add() for that.

  • Returns Shopware\Core\Checkout\Cart\Facade\ItemFacade

    The newly created product line-item.

  • Arguments:

    • string productId: The product id for which a line-item should be created.

    • int quantity: Optionally provide the quantity with which the product line-item should be created, defaults to 1.

      Default: 1

remove()

  • remove() removes the given line-item or the line-item with the given id from this collection.

  • Arguments:

    • string|\ItemFacade id: The id of the line-item or the line-item that should be removed.
  • Examples:

    • Add and then remove a product line-item from the cart.

      twig
      {% do services.cart.products.add(hook.ids.get('p1')) %}
      
      {% do services.cart.products.remove(hook.ids.get('p1')) %}

has()

  • has() checks if a line-item with the given id exists in this collection.

  • Returns bool

    Returns true if the given line-item or a line-item with the given id already exists in the collection, false otherwise.

  • Arguments:

    • string|\ItemFacade id: The id or a line-item that should be checked if it already exists in the collection.

count()

  • count() returns the count of line-items in this collection.

    Note that it does only count the line-items directly in this collection and not child line-items of those.

  • Returns int

    The number of line-items in this collection.


Shopware\Core\Checkout\Cart\Facade\StatesFacade

The StatesFacade allows access to the current cart states and functions.

add()

  • add() allows you to add one or multiple states as string values to the cart.

    This can be useful to check if your script did already run and did some manipulations to the cart.

  • Arguments:

    • string states: One or more strings that will be stored on the cart.

remove()

  • remove() removes the given state from the cart, if it existed.

  • Arguments:

    • string state: The state that should be removed.

has()

  • has() allows you to check if one or more states are present on the cart.

  • Returns bool

    Returns true if at least one of the passed states is present on the cart, false otherwise.

  • Arguments:

    • string states: One or more strings that should be checked.

get()

  • get() returns all states that are present on the cart.

  • Returns array

    An array containing all current states of the cart.