0

I need to have the functionality where if a customer has a VAT number added to their account then I need to change them to a trade store view.

I have the following code however the store view doesn't seem to change.

Could anyone advise where I am going wrong

<?php
namespace Vendor\Module\Observer;

use \Magento\Framework\Event\ObserverInterface;
use \Magento\Store\Model\StoreManagerInterface;

class CustomerLogin implements ObserverInterface
{
    protected $_storeManager;

    public function __construct(
        StoreManagerInterface $storeManager
    ) {
        $this->_storeManager = $storeManager;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        //Get Customer
        $customer = $observer->getEvent()->getCustomer();

        if ($customer->getTaxvat() != '' ) {
            $this->_storeManager->setCurrentStore('trade_store_view');
        }
    }
}

I have the following in my 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="customer_login_observer" instance="Vendor\Module\Observer\CustomerLogin" />
    </event>
</config>

I know the observer is firing as if I do var_dump tests I can see it gets into all the if statements

  • what event are you using here? – Shyam Krishna Sreekumar Jan 31 '18 at 11:53
  • I have updated my answer with my xml but I am using the customer_login_observer – Goose84 Jan 31 '18 at 11:56
  • my mistake, deleting answer. i misread as customer group – Shyam Krishna Sreekumar Jan 31 '18 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.