5

I have written cron to crate customer, but while running code through cron, I am getting error as

A customer website ID must be specified when using the website scope

code to get website id:

$websiteId  = $this->storeManager->getWebsite()->getWebsiteId();

$this->storeManager is instance of class:

\Magento\Store\Model\StoreManager

But that code always return 0(zero) as website id which result in error. Is there any way to get website or store id in cron?

1
  • if u figured it please write answer. Looking for same
    – Ajwad Syed
    Sep 8, 2019 at 12:07

4 Answers 4

5

There is a way you can set store id for crontabs. You may use emulation feature of magento.

$this->emulation->startEnvironmentEmulation($store_id, \Magento\Framework\App\Area::AREA_FRONTEND, true);

// Your code

$this->emulation->stopEnvironmentEmulation();

Hope this helps.

2
  • I did this but after some time I was having error "Process went away". Then I removed emulation and code started working. I am still looking how to retrive website id in cron. becasue $websiteId = (int)$this->storeManager->getWebsite()->getId(); retuns null in cron class
    – Ajwad Syed
    Feb 12, 2020 at 13:30
  • In cronojos, no store ids are settled in environment. So using emulation, you can setup background store id for the process in order to tell magento to consider required store based on your database entry ... This is the only way feasible .. The error you are talking must be becuase of some other code level issue. Try to debug and find the root cause. To make sure, remove entire code in between and only log something and check if the same error occurs or not.
    – Yash Shah
    Feb 13, 2020 at 4:55
1

You can get store id in cron job script using 'store code'

 <?php

namespace Package\Module\Model;

use Magento\Store\Model\Store as StoreModel;


class Cronjob
{
    /**
     * @var StoreModel
     */
    private $storeModel;

    public function __construct(
         StoreModel $storeModel

    ) {

        $this->storeModel = $storeModel;

    }

   /**
     * Get store Id
     *
     */
    public function getStoreId()
    {
    //default is store code 
    $store = $this->storeModel->load('default');
    $storeId =  $store->getId();
        return $storeId;
    }

}
1
  • $this->storeModel->load() It's deprecated way to load objects. Now "entities must not be responsible for their own loading." (quote from Magento core phpDoc). Just use StoreRepositoryInterface Jun 18, 2019 at 10:43
0

I think this is caused by the fact that the CRON process does not have a store or website assigned to it. You can do something like this to set store programmatically:

$this->storeManager->setCurrentStore(1);

Heads up about the CRON process not having a session either so that can be a problem too when you want to do some actions.

0

Magento way to get default storeId:

If it is not set. E.g. it happens in Crontab

<?php

namespace Package\Module\Model;

use Magento\Store\Model\Store as StoreModel;


class Cronjob
{
    /** @var \Magento\Store\Api\StoreRepositoryInterface */
    private $storeRepository;

    public function __construct(
         \Magento\Store\Api\StoreRepositoryInterface $storeRepository

    ) {

        $this->storeRepository = $storeRepository;

    }

   /**
     * Get store Id
     *
     */
    public function getStoreId()
    {
        //default is store code 
        $store = $this->storeRepository->get('default');

        return $store->getId();
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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