Magento Switch Store by Customer Group

It’s really nice when you have many customer group and want to show different products to each customer group. This can be done by creating stores in Magento to view different products in each store and dedicate a store for a customer group.

On a Multi Store Magento site where you have many customer group and want to bind customer groups with stores. we can do it by redirecting a customer to a store after login by detecting customer group.

here is how I did this by and observer

isLoggedIn()) {
            $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
            //Mage::log('store:' . Mage::app()->getStore()->getStoreId(), null, "customer.log");

            if ($groupId == 4) {
                if (Mage::app()->getStore()->getStoreId() != 2) {
                    Mage::app()->setCurrentStore(2);
                    $params = array(
                        '_current' => TRUE,
                        '_use_rewrite' => TRUE,
                        '_store_to_url' => TRUE,
                        '_store' => 2
                    );
                    $url = Mage::getUrl('', $params);

                    //Mage::log('Store: 2:' . Mage::app()->getStore()->getStoreId() . $url, null, "customer.log");

                    Mage::app()->getResponse()->setRedirect($url);
                }

            } elseif ($groupId == 5) {
                if (Mage::app()->getStore()->getStoreId() != 3) {
                    Mage::app()->setCurrentStore(3);
                    $params = array(
                        '_current' => TRUE,
                        '_use_rewrite' => TRUE,
                        '_store_to_url' => TRUE,
                        '_store' => 3
                    );
                    $url = Mage::getUrl('', $params);

                    //Mage::log('Store: 3:' . Mage::app()->getStore()->getStoreId() . $url, null, "customer.log");

                    Mage::app()->getResponse()->setRedirect($url);
                }
            } else {
                if (Mage::app()->getStore()->getStoreId() != 1) {
                    Mage::app()->setCurrentStore(1);
                    $params = array(
                        '_current' => TRUE,
                        '_use_rewrite' => TRUE,
                        '_store_to_url' => TRUE,
                        '_store' => 1
                    );
                    $url = Mage::getUrl('', $params);

                    //Mage::log('Store: 1:' . Mage::app()->getStore()->getStoreId() . $url, null, "customer.log");

                    Mage::app()->getResponse()->setRedirect($url);
                }
            }

        }

    }
?>

Here I bind customer group id 4 with store id 2, customer group id 5 with store id 3 and all other customer with store id 1

Bbserver Event xml:

        
            
               
                   
                       singleton
                       monitor/observer
                       switchstore
                   
               
            
        

Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *