I tried to retrieve it as:

\Magento\Store\Model\StoreManagerInterface $storeManager


$websiteId = (int)$this->storeManager->getWebsite()->getId();

But it returns null in cron class.

How to get website ID there?


The cron is run on an Application level rather than a Website level. What i mean by that is, a single crontask needs to be able to handle all website / store views.

So you can either add an entry into the system config where you can set a website id and pull that into the crontask. Or iterate all the websites likes so.

$websites = $this->storeManager->getWebsites();
foreach($websites as $id => $website) {
| improve this answer | |

Another option is to set current store in the cron task itself



Then you can retrieve it

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.