1

I am working on fetching category URL for stores which are part of two different websites on a multiwebsite setup i.e abc.com and abc.co.uk, Issue happens when I fetch URL for abc.co.uk it returns me the URL of abc.com only. I have tried fetching with below solutions but none of them has worked:

1) Here in collection factory class I used is as below and latter is the code

\Magento\Catalog\Model\Category

$collection = $this->_categoryCollectionFactory->getCollection()->setStoreId($storeId)->addAttributeToSelect('*')->addIsActiveFilter();

2) I looped through all the categories and tried fetching it from repository

$this->categoryRepository->get($category->getEntityId(), $storeId);

3) Another solution tried with looping the categories fetched based on store id, looping it and generating URL as below, Although below URL provides me with a solution for correct website URL but still the category URL path is incorrect.

$this->storeManager->getStore($storeId)->getBaseUrl().$category->getUrlPath().$category->getUrlKey().$categoryUrlSuffix;

Any help or guidance on this would be much appreciated.

1 Answer 1

0

Try this will help you:

protected $_store;

public function __construct(
    \Magento\Store\Model\StoreManagerInterface $store,
    $data = []
) {
    $this->_store = $store;
    parent::__construct($data);
}

$categoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categories = $categoryFactory->create()                              
    ->addAttributeToSelect('*') // for all attribute
    ->setStore($this->_store->getStore()); //categories from current store will be fetched

foreach ($categories as $category){
    $category->getUrlKey();
}
4
  • Already tried with Resourcemodel after posting question but that hasn't worked as well it still returns the old URL only!
    – mp196
    May 16, 2018 at 13:19
  • sorry but old Url means ? in this code give you store wise url @mp196
    – Divyesh
    May 16, 2018 at 15:26
  • As mentioned in my question i have two domains abc.com and abc.co.uk while fetching url for abc.co.uk it provides me the abc.com category url only.
    – mp196
    May 17, 2018 at 9:06
  • so you just check what your abc.co.uk domain store id and pest in code like setStore(2)
    – Divyesh
    May 17, 2018 at 9:11

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.