Magento 2 Plugin Error – Fix Illegal Offset Type in StoreRepositoryPlugin.php

magento2plugin

Exception: Warning: Illegal offset type in Module/ModuleName/Plugin/StoreRepositoryPlugin.php

Getting above error at this line: if ($this->entitiesById[$id]) {

I'm trying to override "vendor\magento\module-store\Model\StoreRepository.php", so i have create custom plugin.

di.xml

<type name="Magento\Store\Model\StoreRepository">
 <plugin name="change_behavior_of" type="Module\Modulename\Plugin\StoreRepositoryPlugin" sortOrder="1" />
</type>

Module\Modulename\Plugin\StoreRepositoryPlugin.php

<?php
namespace Module\Modulename\Plugin;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\App\Config;
use Magento\Store\Model\StoreFactory;
use Magento\Store\Model\ResourceModel\Store\CollectionFactory;

class StoreRepositoryPlugin
{

     protected $storeFactory;

/**
 * @var \Magento\Store\Model\ResourceModel\Store\CollectionFactory
 */
protected $storeCollectionFactory;

/**
 * @var \Magento\Store\Api\Data\StoreInterface[]
 */
protected $entities = [];

/**
 * @var \Magento\Store\Api\Data\StoreInterface[]
 */
protected $entitiesById = [];

/**
 * @var bool
 */
protected $allLoaded = false;

/**
 * @var Config
 */
private $appConfig;


     public function __construct(
        \Magento\Store\Model\StoreFactory $storeFactory,
        \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeCollectionFactory
    ) {
        $this->storeFactory = $storeFactory;
        $this->storeCollectionFactory = $storeCollectionFactory;
    }



    public function beforeGetById($id)
    {


        if ($this->entitiesById[$id]) {
            return $this->entitiesById[$id];
        }

        $storeData = $this->getAppConfig()->get('scopes', "stores/$id", []);
        $store = $this->storeFactory->create([
            'data' => $storeData
        ]);



        if ($store->getStoreId() === null) {
            throw new NoSuchEntityException(__('Requested store is not found'));
        }

        $this->entitiesById[$id] = $store;
        $this->entities[$store->getCode()] = $store;
        return $store;
    }

    private function getAppConfig()
    {
        if (!$this->appConfig) {
            $this->appConfig = ObjectManager::getInstance()->get(Config::class);
        }
        return $this->appConfig;
    }

}

UPDATED CODE:

di.xml

<preference for="Magento\Store\Model\StoreRepository" type="Module\Modulename\Plugin\StoreRepositoryPlugin" />

StoreRepositoryPlugin.php

<?php
namespace Module\Modulename\Plugin;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\App\Config;
use Magento\Store\Model\StoreFactory;
use Magento\Store\Model\ResourceModel\Store\CollectionFactory;

class StoreRepositoryPlugin extends \Magento\Store\Model\StoreRepository
{




     protected $storeFactory;

    /**
     * @var \Magento\Store\Model\ResourceModel\Store\CollectionFactory
     */
    protected $storeCollectionFactory;

    /**
     * @var \Magento\Store\Api\Data\StoreInterface[]
     */
    protected $entities = [];

    /**
     * @var \Magento\Store\Api\Data\StoreInterface[]
     */
    protected $entitiesById = [];

    /**
     * @var bool
     */
    protected $allLoaded = false;

    /**
     * @var Config
     */
    private $appConfig;

     public function __construct(
        \Magento\Store\Model\StoreFactory $storeFactory,
        \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeCollectionFactory
    ) {
        $this->storeFactory = $storeFactory;
        $this->storeCollectionFactory = $storeCollectionFactory;
    }



    public function getById($id)
    {


        if ($this->entitiesById[$id]) {
            return $this->entitiesById[$id];
        }

        $storeData = $this->getAppConfig()->get('scopes', "stores/$id", []);
        $store = $this->storeFactory->create([
            'data' => $storeData
        ]);



        if ($store->getStoreId() === null) {
            throw new NoSuchEntityException(__('Requested store is not found'));
        }

        $this->entitiesById[$id] = $store;
        $this->entities[$store->getCode()] = $store;
        return $store;
    }

    private function getAppConfig()
    {
        if (!$this->appConfig) {
            $this->appConfig = ObjectManager::getInstance()->get(Config::class);
        }
        return $this->appConfig;
    }

}

Best Answer

Please check I have make preference for it.

di.xml

<preference for="Magento\Store\Model\StoreRepository" type="Module\Modulename\Plugin\StoreRepositoryPlugin" />

Your StoreRepositoryPlugin.php look like this.

    <?php
namespace Module\Modulename\Plugin;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\App\Config;
use Magento\Store\Model\StoreFactory;
use Magento\Store\Model\ResourceModel\Store\CollectionFactory;

class StoreRepositoryPlugin extends \Magento\Store\Model\StoreRepository
{

     //your code here

}

Still you have any query let me know.