0

I have a requirement where Customer can select from available theme to be shown when they are logged in. Now I have theme's id in customer Eav setup custom attribute, I also have setup a customer_login event

events.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
  <event name="customer_login">
    <observer name="themepick_customer_login_observer" instance="Afnam\ThemePick\Observer\CustomerLogin" />
  </event>
</config>

and in Observer\CustomerLogin.php

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $customer_id = $observer->getEvent()->getCustomer()->getId();
    $customer = $this->customerRepository->getById( $customer_id );

    $id = $customer->getCustomAttribute( Constants::SELECTED_THEME )->getValue();

    $themes = $this->collectionFactory->create()->loadRegisteredThemes();
    foreach ($themes as $theme) {
      if ($theme->getId() == $id) {
        $this->config->assignToStore(
          $theme,
          [Store::DEFAULT_STORE_ID],
          ScopeConfigInterface::SCOPE_TYPE_DEFAULT
        );
      }
    }

  }

but it doesn't change the theme as expected, I have followed this link https://magento.stackexchange.com/a/140027/26896 and they are changing theme in InstallData

Another important issue is that I dont really want to change the system theme I rather want to apply different themes for different users So it doesn't change the theme for all users but the current user only, and once they logout or check site in other browser with other user so they see default or other assigned theme to that user, Hope I have made myself clear

0

Below changes require in Observer\CustomerLogin.php

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $config = $objectManager->get("\Magento\Theme\Model\Config"); 
    $storeManager  = $objectManager->get('\Magento\Store\Model\StoreManagerInterface'); 

    $customer_id = $observer->getEvent()->getCustomer()->getId();
    $customer = $this->customerRepository->getById( $customer_id );

    $id = $customer->getCustomAttribute( Constants::SELECTED_THEME )->getValue();
    $theme = $objectManager->get("\Magento\Theme\Model\ThemeFactory")->create()->load($id);

    $storeId= $storeManager->getStore()->getStoreId();
    if ($theme->getId()) {
    $config->assignToStore($theme,[$storeId],'store');
    }

}
-1

You need to flush/ clean the cache in order for loading a new theme.

Refer to this topic to clean the cache programmatically. click here

  • I have already tried flushing the cache and ran setup:upgrade after above script doesn't make any difference – riksof-zeeshan Apr 13 '18 at 6:28
  • Go to Content => Design => Confirguration to check whether the theme is set properly. See the example image link: prntscr.com/j4mlyj – Toàn Tam Apr 13 '18 at 6:46
  • its showing the default already applied theme in admin panel – riksof-zeeshan Apr 13 '18 at 8:44

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.