How to use shop instance in CLI context?

I have created a plugin. and i use Shopware()->Shop()->getCurrency()->getId() inside a function not in constructor i got the following error.

You have requested a non-existent service “shop”.

Is there any way to use this shop instance in my plugin? I am running the plugin via CLI

I am using shopware 5.5.6.

Well, when you are in CLI, the actual shop is not defined. If you want to refer to a particular one, you’d neet to create the shop context first. It is feasible but perhaps a bit tricky.

How we can create shop context for a particular shop?

Hello,

it is already mentioned several times here in the forum, but in German, so maybe you couldn’t find it  Smile

$repository = Shopware()->Container()->get('models')->getRepository(Shopware\Models\Shop\Shop::class);
$shopId = 1; // use your correct shop ID here
$shop = $repository->getActiveById($shopId);
$shop->registerResources();
$context = \Shopware\Components\Routing\Context::createFromShop(
    $shop,
    Shopware()->Container()->get('config')
);

Attention here: Since Shopware 5.6 the registerResources is deprecated. Use the shopware.components.shop_registration_service instead. 

Best regards from Schöppingen

cool Michael Telgmann

1 „Gefällt mir“