Search code examples
shopware

Shopware 6 get context in scheduled task


I just wondered how to get the context in scheduled tasks. There is the static method

Context::createDefaultContext()

but it's marked as internal. I know that the context shouldn't be created but rather passed down to your service from higher services. But this doesn't seem possible in scheduled tasks.


Solution

  • I think I found the answer in a GitHub issue comment: https://github.com/shopware/platform/issues/1245#issuecomment-673537348

    The only valid use case to create a default context is when you are in a CLI context, meaning when you write your own CLI command (or scheduled task in that regard). But you should be fully aware that you need to take care of handling translations and currency for example by yourself in that case.

    Another valid use case for the default context is the usage in tests of course, that was the original intent behind that method. In the docs the method is used to not bloat the code examples.

    So it seems to be the way to go in scheduled tasks to create the context there.