How to get current store ID in async controller





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















I have an AJAX request POSTing data to a controller in my Magento module.



$.ajax({
url: "/page/section/profile?isAjax=true",
type: "POST",
data: "profileId=" + profile.id,
success: function (result) {}
});


Starting at http://my-website/store2 I post to the /profile endpoint, where I'm trying to access the current store ID in the following (simplified) controller code:



use MagentoFrameworkAppActionContext;
use MagentoFrameworkAppResponseInterface;
use MagentoFrameworkAppActionAction;

class Profile extends Action
{
/**
* @var MagentoStoreModelStoreManagerInterface
*/
private $storeManager;

/**
* Profile constructor
*
* @param Context $context
*/
public function __construct(
Context $context,
MagentoStoreModelStoreManagerInterface $storeManager
) {
$this->storeManager = $storeManager;
parent::__construct($context);
}

public function execute()
{
$storeId = $this->storeManager->getStore()->getId(); // returns 1
$websiteId = $this->getRequest()->getParam('website', 0); // returns 0

return [$storeId, $websiteId];
}
}


however this always returns store ID 1 (default) instead of the expected store ID 2.



I am not currently logged in to Magento admin.



I have tried to obtain this data via e.g. $this->getRequest()->getParam('website'), in the controller, but that doesn't seem to help either.










share|improve this question

























  • which data are you trying to get using the getParam function?

    – magefms
    yesterday











  • @magefms I have tried to get 'website'. This returns 0.

    – strangerpixel
    yesterday











  • can you post your controller code

    – magefms
    yesterday











  • check updated answer @strangerpixel

    – magefms
    yesterday


















1















I have an AJAX request POSTing data to a controller in my Magento module.



$.ajax({
url: "/page/section/profile?isAjax=true",
type: "POST",
data: "profileId=" + profile.id,
success: function (result) {}
});


Starting at http://my-website/store2 I post to the /profile endpoint, where I'm trying to access the current store ID in the following (simplified) controller code:



use MagentoFrameworkAppActionContext;
use MagentoFrameworkAppResponseInterface;
use MagentoFrameworkAppActionAction;

class Profile extends Action
{
/**
* @var MagentoStoreModelStoreManagerInterface
*/
private $storeManager;

/**
* Profile constructor
*
* @param Context $context
*/
public function __construct(
Context $context,
MagentoStoreModelStoreManagerInterface $storeManager
) {
$this->storeManager = $storeManager;
parent::__construct($context);
}

public function execute()
{
$storeId = $this->storeManager->getStore()->getId(); // returns 1
$websiteId = $this->getRequest()->getParam('website', 0); // returns 0

return [$storeId, $websiteId];
}
}


however this always returns store ID 1 (default) instead of the expected store ID 2.



I am not currently logged in to Magento admin.



I have tried to obtain this data via e.g. $this->getRequest()->getParam('website'), in the controller, but that doesn't seem to help either.










share|improve this question

























  • which data are you trying to get using the getParam function?

    – magefms
    yesterday











  • @magefms I have tried to get 'website'. This returns 0.

    – strangerpixel
    yesterday











  • can you post your controller code

    – magefms
    yesterday











  • check updated answer @strangerpixel

    – magefms
    yesterday














1












1








1








I have an AJAX request POSTing data to a controller in my Magento module.



$.ajax({
url: "/page/section/profile?isAjax=true",
type: "POST",
data: "profileId=" + profile.id,
success: function (result) {}
});


Starting at http://my-website/store2 I post to the /profile endpoint, where I'm trying to access the current store ID in the following (simplified) controller code:



use MagentoFrameworkAppActionContext;
use MagentoFrameworkAppResponseInterface;
use MagentoFrameworkAppActionAction;

class Profile extends Action
{
/**
* @var MagentoStoreModelStoreManagerInterface
*/
private $storeManager;

/**
* Profile constructor
*
* @param Context $context
*/
public function __construct(
Context $context,
MagentoStoreModelStoreManagerInterface $storeManager
) {
$this->storeManager = $storeManager;
parent::__construct($context);
}

public function execute()
{
$storeId = $this->storeManager->getStore()->getId(); // returns 1
$websiteId = $this->getRequest()->getParam('website', 0); // returns 0

return [$storeId, $websiteId];
}
}


however this always returns store ID 1 (default) instead of the expected store ID 2.



I am not currently logged in to Magento admin.



I have tried to obtain this data via e.g. $this->getRequest()->getParam('website'), in the controller, but that doesn't seem to help either.










share|improve this question
















I have an AJAX request POSTing data to a controller in my Magento module.



$.ajax({
url: "/page/section/profile?isAjax=true",
type: "POST",
data: "profileId=" + profile.id,
success: function (result) {}
});


Starting at http://my-website/store2 I post to the /profile endpoint, where I'm trying to access the current store ID in the following (simplified) controller code:



use MagentoFrameworkAppActionContext;
use MagentoFrameworkAppResponseInterface;
use MagentoFrameworkAppActionAction;

class Profile extends Action
{
/**
* @var MagentoStoreModelStoreManagerInterface
*/
private $storeManager;

/**
* Profile constructor
*
* @param Context $context
*/
public function __construct(
Context $context,
MagentoStoreModelStoreManagerInterface $storeManager
) {
$this->storeManager = $storeManager;
parent::__construct($context);
}

public function execute()
{
$storeId = $this->storeManager->getStore()->getId(); // returns 1
$websiteId = $this->getRequest()->getParam('website', 0); // returns 0

return [$storeId, $websiteId];
}
}


however this always returns store ID 1 (default) instead of the expected store ID 2.



I am not currently logged in to Magento admin.



I have tried to obtain this data via e.g. $this->getRequest()->getParam('website'), in the controller, but that doesn't seem to help either.







magento2 magento2.3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday







strangerpixel

















asked yesterday









strangerpixelstrangerpixel

1114




1114













  • which data are you trying to get using the getParam function?

    – magefms
    yesterday











  • @magefms I have tried to get 'website'. This returns 0.

    – strangerpixel
    yesterday











  • can you post your controller code

    – magefms
    yesterday











  • check updated answer @strangerpixel

    – magefms
    yesterday



















  • which data are you trying to get using the getParam function?

    – magefms
    yesterday











  • @magefms I have tried to get 'website'. This returns 0.

    – strangerpixel
    yesterday











  • can you post your controller code

    – magefms
    yesterday











  • check updated answer @strangerpixel

    – magefms
    yesterday

















which data are you trying to get using the getParam function?

– magefms
yesterday





which data are you trying to get using the getParam function?

– magefms
yesterday













@magefms I have tried to get 'website'. This returns 0.

– strangerpixel
yesterday





@magefms I have tried to get 'website'. This returns 0.

– strangerpixel
yesterday













can you post your controller code

– magefms
yesterday





can you post your controller code

– magefms
yesterday













check updated answer @strangerpixel

– magefms
yesterday





check updated answer @strangerpixel

– magefms
yesterday










2 Answers
2






active

oldest

votes


















0














You can try like this in your controller:



$this->request()->getParam('website',0);


UPDATE:



use MagentoFrameworkAppActionContext;
use MagentoFrameworkAppResponseInterface;
use MagentoFrameworkAppActionAction;

class Profile extends Action
{
/**
* @var MagentoFrameworkAppRequestHttp
*/
protected $request;

/**
* Profile constructor
*
* @param Context $context
*/
public function __construct(
Context $context,
MagentoFrameworkAppRequestHttp $request
) {
$this->request= $request;
parent::__construct($context);
}

public function execute()
{
return $this->request->getParam('website',0);
}

}





share|improve this answer


























  • Unfortunately that still returns 0 for 'website'.

    – strangerpixel
    yesterday











  • did you run the upgrade and other required commands?

    – magefms
    yesterday











  • how about changing website to store like return $this->request->getParam('store',0); ?

    – magefms
    yesterday











  • @strangerpixel I see your code in your post, it return 0 because you are not injecting MagentoFrameworkAppRequestHttp $request in your constructor

    – magefms
    yesterday













  • I have tried it locally, it's the same outcome as $this->getRequest(), which returns a MagentoFrameworkAppRequestInterface. To your earlier point, 'store' param is also null. The request object in the controller only gives access to the params I have explicitly posted to it.

    – strangerpixel
    yesterday





















0














It turned out I needed to post data to the appropriate store URL, defined in the block beforehand.



Block:



public function getEndpointWithStoreCode()
{
return $this->storeManager->getStore()->getBaseUrl(
MagentoFrameworkUrlInterface::URL_TYPE_WEB,
true
) . "/page/section/profile?isAjax=true";
}


Template:



<script type="text/x-magento-init">
{
"*": {
"my_module/js/profile" : {
"profileEndpoint" : "<?= $block->getEndpointWithStoreCode() ?>"
}
}
}




JS:



$.ajax({
url: config.profileEndpoint,
type: "POST",
data: "profileId=" + profile.id,
success: function (result) {}
});


By POSTing directly to /store2/page/section/profile, the right store scope is locked in.






share|improve this answer
























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "479"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f269320%2fhow-to-get-current-store-id-in-async-controller%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can try like this in your controller:



    $this->request()->getParam('website',0);


    UPDATE:



    use MagentoFrameworkAppActionContext;
    use MagentoFrameworkAppResponseInterface;
    use MagentoFrameworkAppActionAction;

    class Profile extends Action
    {
    /**
    * @var MagentoFrameworkAppRequestHttp
    */
    protected $request;

    /**
    * Profile constructor
    *
    * @param Context $context
    */
    public function __construct(
    Context $context,
    MagentoFrameworkAppRequestHttp $request
    ) {
    $this->request= $request;
    parent::__construct($context);
    }

    public function execute()
    {
    return $this->request->getParam('website',0);
    }

    }





    share|improve this answer


























    • Unfortunately that still returns 0 for 'website'.

      – strangerpixel
      yesterday











    • did you run the upgrade and other required commands?

      – magefms
      yesterday











    • how about changing website to store like return $this->request->getParam('store',0); ?

      – magefms
      yesterday











    • @strangerpixel I see your code in your post, it return 0 because you are not injecting MagentoFrameworkAppRequestHttp $request in your constructor

      – magefms
      yesterday













    • I have tried it locally, it's the same outcome as $this->getRequest(), which returns a MagentoFrameworkAppRequestInterface. To your earlier point, 'store' param is also null. The request object in the controller only gives access to the params I have explicitly posted to it.

      – strangerpixel
      yesterday


















    0














    You can try like this in your controller:



    $this->request()->getParam('website',0);


    UPDATE:



    use MagentoFrameworkAppActionContext;
    use MagentoFrameworkAppResponseInterface;
    use MagentoFrameworkAppActionAction;

    class Profile extends Action
    {
    /**
    * @var MagentoFrameworkAppRequestHttp
    */
    protected $request;

    /**
    * Profile constructor
    *
    * @param Context $context
    */
    public function __construct(
    Context $context,
    MagentoFrameworkAppRequestHttp $request
    ) {
    $this->request= $request;
    parent::__construct($context);
    }

    public function execute()
    {
    return $this->request->getParam('website',0);
    }

    }





    share|improve this answer


























    • Unfortunately that still returns 0 for 'website'.

      – strangerpixel
      yesterday











    • did you run the upgrade and other required commands?

      – magefms
      yesterday











    • how about changing website to store like return $this->request->getParam('store',0); ?

      – magefms
      yesterday











    • @strangerpixel I see your code in your post, it return 0 because you are not injecting MagentoFrameworkAppRequestHttp $request in your constructor

      – magefms
      yesterday













    • I have tried it locally, it's the same outcome as $this->getRequest(), which returns a MagentoFrameworkAppRequestInterface. To your earlier point, 'store' param is also null. The request object in the controller only gives access to the params I have explicitly posted to it.

      – strangerpixel
      yesterday
















    0












    0








    0







    You can try like this in your controller:



    $this->request()->getParam('website',0);


    UPDATE:



    use MagentoFrameworkAppActionContext;
    use MagentoFrameworkAppResponseInterface;
    use MagentoFrameworkAppActionAction;

    class Profile extends Action
    {
    /**
    * @var MagentoFrameworkAppRequestHttp
    */
    protected $request;

    /**
    * Profile constructor
    *
    * @param Context $context
    */
    public function __construct(
    Context $context,
    MagentoFrameworkAppRequestHttp $request
    ) {
    $this->request= $request;
    parent::__construct($context);
    }

    public function execute()
    {
    return $this->request->getParam('website',0);
    }

    }





    share|improve this answer















    You can try like this in your controller:



    $this->request()->getParam('website',0);


    UPDATE:



    use MagentoFrameworkAppActionContext;
    use MagentoFrameworkAppResponseInterface;
    use MagentoFrameworkAppActionAction;

    class Profile extends Action
    {
    /**
    * @var MagentoFrameworkAppRequestHttp
    */
    protected $request;

    /**
    * Profile constructor
    *
    * @param Context $context
    */
    public function __construct(
    Context $context,
    MagentoFrameworkAppRequestHttp $request
    ) {
    $this->request= $request;
    parent::__construct($context);
    }

    public function execute()
    {
    return $this->request->getParam('website',0);
    }

    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited yesterday

























    answered yesterday









    magefmsmagefms

    2,5932526




    2,5932526













    • Unfortunately that still returns 0 for 'website'.

      – strangerpixel
      yesterday











    • did you run the upgrade and other required commands?

      – magefms
      yesterday











    • how about changing website to store like return $this->request->getParam('store',0); ?

      – magefms
      yesterday











    • @strangerpixel I see your code in your post, it return 0 because you are not injecting MagentoFrameworkAppRequestHttp $request in your constructor

      – magefms
      yesterday













    • I have tried it locally, it's the same outcome as $this->getRequest(), which returns a MagentoFrameworkAppRequestInterface. To your earlier point, 'store' param is also null. The request object in the controller only gives access to the params I have explicitly posted to it.

      – strangerpixel
      yesterday





















    • Unfortunately that still returns 0 for 'website'.

      – strangerpixel
      yesterday











    • did you run the upgrade and other required commands?

      – magefms
      yesterday











    • how about changing website to store like return $this->request->getParam('store',0); ?

      – magefms
      yesterday











    • @strangerpixel I see your code in your post, it return 0 because you are not injecting MagentoFrameworkAppRequestHttp $request in your constructor

      – magefms
      yesterday













    • I have tried it locally, it's the same outcome as $this->getRequest(), which returns a MagentoFrameworkAppRequestInterface. To your earlier point, 'store' param is also null. The request object in the controller only gives access to the params I have explicitly posted to it.

      – strangerpixel
      yesterday



















    Unfortunately that still returns 0 for 'website'.

    – strangerpixel
    yesterday





    Unfortunately that still returns 0 for 'website'.

    – strangerpixel
    yesterday













    did you run the upgrade and other required commands?

    – magefms
    yesterday





    did you run the upgrade and other required commands?

    – magefms
    yesterday













    how about changing website to store like return $this->request->getParam('store',0); ?

    – magefms
    yesterday





    how about changing website to store like return $this->request->getParam('store',0); ?

    – magefms
    yesterday













    @strangerpixel I see your code in your post, it return 0 because you are not injecting MagentoFrameworkAppRequestHttp $request in your constructor

    – magefms
    yesterday







    @strangerpixel I see your code in your post, it return 0 because you are not injecting MagentoFrameworkAppRequestHttp $request in your constructor

    – magefms
    yesterday















    I have tried it locally, it's the same outcome as $this->getRequest(), which returns a MagentoFrameworkAppRequestInterface. To your earlier point, 'store' param is also null. The request object in the controller only gives access to the params I have explicitly posted to it.

    – strangerpixel
    yesterday







    I have tried it locally, it's the same outcome as $this->getRequest(), which returns a MagentoFrameworkAppRequestInterface. To your earlier point, 'store' param is also null. The request object in the controller only gives access to the params I have explicitly posted to it.

    – strangerpixel
    yesterday















    0














    It turned out I needed to post data to the appropriate store URL, defined in the block beforehand.



    Block:



    public function getEndpointWithStoreCode()
    {
    return $this->storeManager->getStore()->getBaseUrl(
    MagentoFrameworkUrlInterface::URL_TYPE_WEB,
    true
    ) . "/page/section/profile?isAjax=true";
    }


    Template:



    <script type="text/x-magento-init">
    {
    "*": {
    "my_module/js/profile" : {
    "profileEndpoint" : "<?= $block->getEndpointWithStoreCode() ?>"
    }
    }
    }




    JS:



    $.ajax({
    url: config.profileEndpoint,
    type: "POST",
    data: "profileId=" + profile.id,
    success: function (result) {}
    });


    By POSTing directly to /store2/page/section/profile, the right store scope is locked in.






    share|improve this answer




























      0














      It turned out I needed to post data to the appropriate store URL, defined in the block beforehand.



      Block:



      public function getEndpointWithStoreCode()
      {
      return $this->storeManager->getStore()->getBaseUrl(
      MagentoFrameworkUrlInterface::URL_TYPE_WEB,
      true
      ) . "/page/section/profile?isAjax=true";
      }


      Template:



      <script type="text/x-magento-init">
      {
      "*": {
      "my_module/js/profile" : {
      "profileEndpoint" : "<?= $block->getEndpointWithStoreCode() ?>"
      }
      }
      }




      JS:



      $.ajax({
      url: config.profileEndpoint,
      type: "POST",
      data: "profileId=" + profile.id,
      success: function (result) {}
      });


      By POSTing directly to /store2/page/section/profile, the right store scope is locked in.






      share|improve this answer


























        0












        0








        0







        It turned out I needed to post data to the appropriate store URL, defined in the block beforehand.



        Block:



        public function getEndpointWithStoreCode()
        {
        return $this->storeManager->getStore()->getBaseUrl(
        MagentoFrameworkUrlInterface::URL_TYPE_WEB,
        true
        ) . "/page/section/profile?isAjax=true";
        }


        Template:



        <script type="text/x-magento-init">
        {
        "*": {
        "my_module/js/profile" : {
        "profileEndpoint" : "<?= $block->getEndpointWithStoreCode() ?>"
        }
        }
        }




        JS:



        $.ajax({
        url: config.profileEndpoint,
        type: "POST",
        data: "profileId=" + profile.id,
        success: function (result) {}
        });


        By POSTing directly to /store2/page/section/profile, the right store scope is locked in.






        share|improve this answer













        It turned out I needed to post data to the appropriate store URL, defined in the block beforehand.



        Block:



        public function getEndpointWithStoreCode()
        {
        return $this->storeManager->getStore()->getBaseUrl(
        MagentoFrameworkUrlInterface::URL_TYPE_WEB,
        true
        ) . "/page/section/profile?isAjax=true";
        }


        Template:



        <script type="text/x-magento-init">
        {
        "*": {
        "my_module/js/profile" : {
        "profileEndpoint" : "<?= $block->getEndpointWithStoreCode() ?>"
        }
        }
        }




        JS:



        $.ajax({
        url: config.profileEndpoint,
        type: "POST",
        data: "profileId=" + profile.id,
        success: function (result) {}
        });


        By POSTing directly to /store2/page/section/profile, the right store scope is locked in.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        strangerpixelstrangerpixel

        1114




        1114






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f269320%2fhow-to-get-current-store-id-in-async-controller%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What other Star Trek series did the main TNG cast show up in?

            Berlina muro

            Berlina aerponto