In Magento 2, what is the cleanest way to have the same CRON job run for every store, and pull relevant configuration settings from the appropriate store?

Would it be to add a loop inside Vendor/Module/Cron/Job.php, execute method, checking every store, and setting $this->storeManager->setCurrentStore at the top of the method?

Followed by,

$this->scopeConfig->getValue('...', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)

2 Answers 2


Simply Inject the StoresConfig class in your Cron model class constructor, and read store config values with getStoresConfigByPath($path) for all stores. This will return the config values with the associated store ids:

Class File: /vendor/magento/module-store/Model/StoresConfig.php

public function getStoresConfigByPath($path)
    $stores = $this->_storeManager->getStores(true);
    $storeValues = [];
    /** @var $store \Magento\Store\Model\Store */
    foreach ($stores as $store) {
        try {
            $value = $this->_config->getValue($path, ScopeInterface::SCOPE_STORE, $store->getCode());
            $storeValues[$store->getId()] = $value;
        } catch (NoSuchEntityException $e) {
            // Store doesn't really exist, so move on.
    return $storeValues;

Here is the following procedure to setup cron in Magento2 :


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/cron_groups.xsd">
    <group id="cron_group_name">

Create crontab.xml : app/code/Vendor/ModuleName/etc/crontab.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
    <group id="cron_group_name">
        <job name="cronname_api" instance="Vendor\ModuleName\Cron\Cron" method="execute">
            <schedule>* * * * *</schedule>

Create Cron.php app/code/Vendor/ModuleName/Cron/Cron.php

    namespace Vendor\ModuleName\Cron;

    class Cron
    public function execute()
    // Paste your logic for multiple stores
   // Given example cron will run every minute execute() function 
  • Thank you, but how would I run particular code in execute() for every store, using system.xml values from each store?
    – djdy
    Jan 23, 2018 at 14:18
  • As you mentioned above you can use ScopeConfigInterface , also define multiple jobs for each of your store at by using store code or Id its up to you, As I don't know what logic you actually want to deploy inside the cron's , But I can say you pick one of option and it will work for all conditions without blocker , either ScopeConfigInterface Or Multiple Cron Jobs . Jan 23, 2018 at 14:35

Your Answer

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

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