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;
}
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
add a comment |
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
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
add a comment |
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
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
magento2 magento2.3
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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);
}
}
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 likereturn $this->request->getParam('store',0);
?
– magefms
yesterday
@strangerpixel I see your code in your post, it return 0 because you are not injectingMagentoFrameworkAppRequestHttp $request
in your constructor
– magefms
yesterday
I have tried it locally, it's the same outcome as$this->getRequest()
, which returns aMagentoFrameworkAppRequestInterface
. 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
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
}
}
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 likereturn $this->request->getParam('store',0);
?
– magefms
yesterday
@strangerpixel I see your code in your post, it return 0 because you are not injectingMagentoFrameworkAppRequestHttp $request
in your constructor
– magefms
yesterday
I have tried it locally, it's the same outcome as$this->getRequest()
, which returns aMagentoFrameworkAppRequestInterface
. 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
add a comment |
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);
}
}
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 likereturn $this->request->getParam('store',0);
?
– magefms
yesterday
@strangerpixel I see your code in your post, it return 0 because you are not injectingMagentoFrameworkAppRequestHttp $request
in your constructor
– magefms
yesterday
I have tried it locally, it's the same outcome as$this->getRequest()
, which returns aMagentoFrameworkAppRequestInterface
. 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
add a comment |
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);
}
}
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);
}
}
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 likereturn $this->request->getParam('store',0);
?
– magefms
yesterday
@strangerpixel I see your code in your post, it return 0 because you are not injectingMagentoFrameworkAppRequestHttp $request
in your constructor
– magefms
yesterday
I have tried it locally, it's the same outcome as$this->getRequest()
, which returns aMagentoFrameworkAppRequestInterface
. 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
add a comment |
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 likereturn $this->request->getParam('store',0);
?
– magefms
yesterday
@strangerpixel I see your code in your post, it return 0 because you are not injectingMagentoFrameworkAppRequestHttp $request
in your constructor
– magefms
yesterday
I have tried it locally, it's the same outcome as$this->getRequest()
, which returns aMagentoFrameworkAppRequestInterface
. 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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered yesterday
strangerpixelstrangerpixel
1114
1114
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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