abinjohnedamanaabinjohnedamana MemberComments: 14 Received thanks: 0 Member since: February 21

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.

1 Answer

  • Michael TelgmannMichael Telgmann ModeratorComments: 1098 Received thanks: 310 edited July 18 Member since: June 2014

    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

    Quote
    Accepted Answer
    Thanked by 1abinjohnedamana
  • Accepted Answer

Answers

  • vanwittlaervanwittlaer MemberComments: 342 Received thanks: 85 Member since: September 2015

    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.

  • abinjohnedamanaabinjohnedamana MemberComments: 14 Received thanks: 0 Member since: February 21

    How we can create shop context for a particular shop?

  • Michael TelgmannMichael Telgmann ModeratorComments: 1098 Received thanks: 310 edited July 18 Member since: June 2014

    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

    Quote
    Accepted Answer
    Thanked by 1abinjohnedamana
Sign In or Register to comment.