I have a Magento 2 ecommerce setup that has 2 site in it. Each site with different domain name. The main Magento ecommerce admin is located in http://website-admin.com and I have another site which is http://website1.com and another which is http://website2.com. Now this 2 website have different Root Category assigned to them. Now each website has it's own template that generate a custom menu. In this menu I'm retrieving all categories of this store and outputs the category and the url in the list format. But the issue now is that instead of the url to be in http://website2.com/category2 it's using the main website which is the http://website-admin.com/category2.
I'm fairly new to Magento and the Multisite. I'm not sure what to do here. Would appreciate a proper approach. The scenario here is that the menu being displayed in the frontend is generated in the Admin panel. Now since this is in the admin panel I had to use Emulation just to generate the store URL equivalent for the category. But upon visiting the category it's generating the main website as it's base url.
Would appreciate any code that can show me the proper display of category for multisite
Hello @madzmar_ullang
I understand the actual multisite scenario which you are trying to achieve.
So just a quick question - have you given a base url to your second website ? or you have added base url to only your main site ?
You will require to add base url of each different site you have created from the admin panel ! like you assign the different root category.
Then run indexer command and check - it will showcase you the proper URL.
For more reference refer this link - https://www.simicart.com/blog/magento-2-multiple-stores-domains/
Hope it helps !
Yeah I set a new base url for the second website.. I didn't run the indexer and i'm not sure how. Will follow the link you gave me.
Yes follow the link which i have shared and it will works for you !
Also to run indexer you will require to open your SSH terminal and run below command :
php bin/magento indexer:reindex php bin/magento cache:clean php bin/magento cache:flush
Hope it helps !
sadly it didn't work. I have no more idea on what to do. Hope someone can help me with this. For referrence I have this code
public function getCategoryList($subcategories, $store) { $layout = $this->_layoutFactory->create(); $layout = $layout->createBlock('Company\Module\Block\Category') ->setStore($store) ->setTemplate('Company_Module::category/category_list.phtml'); $content = ""; foreach ($subcategories as $subcategory) { $content .= $layout->setData([$subcategory, $store])->toHtml(); } return $content; } // Inside my .phtml file this generates the LI files <?php $data = $this->getData(); $category = $data[0]; $store = $data[1]; $this->setCurrentStore($store); ?> <li class='category-list'> <a class='categorylink' href="<?php echo $category->getUrl(); ?>" data-store-id="<?php echo $store->getId(); ?>"> <?php echo $category->getName(); ?> </a> <?php $subcategories = $this->getSubCategories($category); ?> <?php if ($category->hasChildren()): ?> <ul class='category'> <?php echo $this->getCategoryList($subcategories, $this->getCurrentStore()); ?> </ul> <?php endif; ?> </li>
My issue is the <?php echo $category->getUrl(); ?> it returns the category url but the base_url is set to the url of the admin panel instead of the website2.com based url
I understand your concern !
Can you go to the database and go to the core_config_data table and find below two key path from that table :
web/secure/base_url
web/unsecure/base_url
Then check its value and post here what its showing - is it showing admin url over there ? in one of the above key path or not.
I check the config and this is the value
I tried placing a debugger and below is the outputted html
As you can see the StoreBaseUrl is set to kininvie.wgs-ecom.test but the generated category url is wgs-ecom.test/product.html
True ! check from the admin panel - system value checkbox is checked from the base_url column under the general section !
So you will require to disable that system value checkbox and select your second website from the dropdown - and once that form is loaded check your new base url will be there or not.
if not kindly put the same over there click and save and then check !
Sorry I don't follow.. are you referring to this page?
The use default checkbox was ticked and I unticked it and saved. then regenerate the menu but still no luck,
After untick that option - can you see option on the left side top - store view option ?
Where in you will see your website name and a dropdown within it - click on that website name - list of other websites will be open - now select your second website from there.
Go to again url part - Base URLs section and add your second website URL over here and then click and save and then check !
It will resolved your problem for the same !
Hope it helps !