0

Hi I have created 3 to 4 stores programatically

Now i want to assign my custom theme to specific store using programatically.

I have store id of each store which i have created.

Below code i have tried , but it assign theme to all the stores including default as well.

/**
     * Assign Theme
     *
     * @return void
     */
    protected function assignTheme($group_id)
    {
        $themes = $this->collectionFactory->create()->loadRegisteredThemes();
        /**
         * @var \Magento\Theme\Model\Theme $theme
         */
        foreach ($themes as $theme) {
            if ($theme->getCode() == self::THEME_NAME) {
                $this->config->assignToStore(
                    $theme,
                    [Store::DEFAULT_STORE_ID],
                    ScopeConfigInterface::SCOPE_TYPE_DEFAULT
                );
            }
        }
    }

How can i pass store id to this code , or any alternate way to do this ?

1 Answer 1

1

You can do it by installData or upgradeData scripts. (Refer this)

Then use following code in your script.

You can get theme_id from theme table.

use Magento\Framework\App\Config\ConfigResource\ConfigInterface;

/**
     * @var ConfigInterface
     */
    private $configInterface;

public function __construct(
        .....................
        ConfigInterface $configInterface,
        ................
    ) {
        ................
        $this->configInterface = $configInterface;
        ..................
    }


$this->configInterface->saveConfig('design/theme/theme_id', {{theme_id}}, 'stores', {{store_id}});

NOTE: change {{theme_id}} and {{store_id}} with your theme and store id

2
  • I already did that i don't want to use this in installData or upgradeData Scripts . I have created this method in model and i am using from there Feb 19, 2018 at 10:58
  • why don't you use the same method in your model method? Store config could be saved from anywhere. Feb 19, 2018 at 11:01

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.