6.4 (current)
Search…
⌃K
Links

Custom endpoint script services reference

Custom Endpoint script services reference

The cache service allows you to invalidate the cache if some entity is updated.

invalidate()

  • invalidate() allows you to invalidate all cache entries with the given tag.
  • Arguments:
    • array tags: The tags for which all cache entries should be invalidated as array.
  • Examples:
    • Invalidate a hard coded tag.
      {% do services.cache.invalidate(['my-tag']) %}
    • Build tags based on written entities and invalidate those tags.
      {% set ids = hook.event.getIds('product_manufacturer') %}
      {% if ids.empty %}
      {% return %}
      {% endif %}
      {% set tags = [] %}
      {% for id in ids %}
      {% set tags = tags|merge(['my-manufacturer-' ~ id]) %}
      {% endfor %}
      {% do services.cache.invalidate(tags) %}
    • Build tags if products with a specific property is created and invalidate those tags.
      {% set ids = hook.event.getIds('product') %}
      {% set ids = ids.only('insert').with('description', 'parentId') %}
      {% if ids.empty %}
      {% return %}
      {% endif %}
      {% set tags = [] %}
      {% for id in ids %}
      {% set tags = tags|merge(['my-product-' ~ id]) %}
      {% endfor %}
      {% do services.cache.invalidate(tags) %}
The writer service allows you to write data, that is stored inside shopware. Keep in mind that your app needs to have the correct permissions for the data it writes through this service.

upsert()

  • The upsert() method allows you to create or update entities inside the database.
    If you pass an id in the payload it will do an update if an entity with that id already exists, otherwise it will be a create.
  • The WriteEvents that were generated by executing the upsert().
  • Arguments:
    • string entityName: The name of the entity you want to upsert, e.g. product or media.
    • array payload: The payload you want to upsert, as a list of associative arrays, where each associative array represents the payload for one entity.
  • Examples:
    • Create a new entity.
      {% do services.writer.upsert('tax', [
      { 'name': 'new Tax', 'taxRate': 99.9 }
      ]) %}
    • Update an existing entity.
      {% do services.writer.upsert('product', [
      { 'id': hook.productId, 'active': true }
      ]) %}

delete()

  • The delete() method allows you to delete entities from the database.
  • The WriteEvents that were generated by executing the delete().
  • Arguments:
    • string entityName: The name of the entity you want to delete, e.g. product or media.
    • array payload: The primary keys of the entities you want to delete, as a list of associative arrays, associative array represents the primary keys for one entity.
  • Examples:
    • Delete an entity.
      {% do services.writer.delete('product', [
      { 'id': hook.productId }
      ]) %}

sync()

  • The sync() method allows you to execute updates and deletes to multiple entities in one method call.
  • The result of the sync().
  • Arguments:
    • array payload: All operations that should be executed.
  • Examples:
    • Update an entity and delete another one with one sync() call.
      {% set payload = [
      {
      'entity': 'product',
      'action': 'upsert',
      'payload': [
      { 'id': hook.updateProductId, 'active': true }
      ]
      },
      {
      'entity': 'product',
      'action': 'delete',
      'payload': [
      { 'id': hook.deleteProductId }
      ]
      },
      ] %}
      {% do services.writer.sync(payload) %}
The response service allows you to create HTTP-Responses.

json()

  • The json() method allows you to create a JSON-Response.
  • The created response object, remember to assign it to the hook with hook.setResponse().
  • Arguments:
    • array data: The data that should be sent in the response as array.
    • int code: The HTTP-Status-Code of the response, defaults to 200.
      Default: 200
  • Examples:
    • Return hard coded values as JsonResponse.
      {% set response = services.response.json({ 'foo': 'bar' }) %}
      {% do hook.setResponse(response) %}
    • Search for products and return them in a JsonResponse.
      {# @var services \Shopware\Core\Framework\Script\ServiceStubs #}
      {% set products = services.repository.search('product', hook.request) %}
      {% set response = services.response.json({ 'products': products }) %}
      {% do hook.setResponse(response) %}

redirect()

  • The redirect() method allows you to create a RedirectResponse.
  • The created response object, remember to assign it to the hook with hook.setResponse().
  • Arguments:
    • string route: The name of the route that should be redirected to.
    • array parameters: The parameters needing to generate the URL of the route as an associative array.
    • int code: he HTTP-Status-Code of the response, defaults to 302.
      Default: 302
  • Examples:
    • Redirect to an Admin-API route.
      {% set response = services.response.redirect('api.product.detail', { 'path': productId }) %}
      {% do hook.setResponse(response) %}
    • Redirect to a storefront page.
      {% set response = services.response.redirect('frontend.detail.page', { 'productId': productId }) %}
      {% do hook.setResponse(response) %}

render()

  • The render() method allows you to render a twig view with the parameters you provide and create a StorefrontResponse.
    Note that the render() method will throw an exception if it is called from outside a SalesChannelContext (e.g. from an /api route) or if the Storefront-bundle is not installed.
  • The created response object with the rendered template as content, remember to assign it to the hook with hook.setResponse().
  • Arguments:
    • string view: The name of the twig template you want to render e.g. @Storefront/storefront/page/content/detail.html.twig
    • array parameters: The parameters you want to pass to the template, ensure that you pass the page parameter from the hook to the templates.
      Default: array ( )
  • Examples:
    • Fetch a product, add it to the page and return a rendered response.
      {% set product = services.store.search('product', { 'ids': [productId]}).first %}
      {% do hook.page.addExtension('myProduct', product) %}
      {% do hook.setResponse(
      services.response.render('@MyApp/storefront/page/custom-page/index.html.twig', { 'page': hook.page })
      ) %}