4

I want to get product url with category key in it on home page.

for example:

www.example.com/category/product.html

Thanks

9
  • 2
    Admin -> Stores -> Configuration -> Catalog -> Catalog -> Search Engine Optimization -> "Use Categories Path for Product URLs" set to "Yes"
    – Jackson
    Commented Feb 8, 2017 at 7:49
  • Thanks, but it is adding category url key on category page (products) not on home page
    – Waqar
    Commented Feb 8, 2017 at 9:04
  • Please give more detail where u want exactly. Screenshot?
    – Jackson
    Commented Feb 8, 2017 at 9:05
  • I have a new products slider on home page and i want to add url key of category(first category in which that product is found) in its link.
    – Waqar
    Commented Feb 8, 2017 at 9:13
  • Product URL should be same throughout Website right? Only on homepage u want with Category? What is purpose?
    – Jackson
    Commented Feb 8, 2017 at 9:15

4 Answers 4

4

If you have a product collection, you will need to specify a category for each product in the collection.

If you are using a category collection the category from the collection will be used.

The product you are generating a URL for will need to know which category it should include in the url, which is why if you just load the product without mentioning a category, you will not return any category in the path even if you adjust the admin settings like others have recommended.

Some psuedo code to get you started:

// Taken from https://stackoverflow.com/questions/18300258/magento-getproducturl-does-not-include-category-path

$product->setCategoryId($categoryId)->getProductUrl();
1
  • Thank you. This is exactly what I needed. I just did this: $product->setCategoryId(max($product->getCategoryIds()))->getProductUrl(); - that ways I get a product url wih the deepest category for a product.
    – Zankar
    Commented Aug 26 at 10:48
3

go to Store -> Configuration -> Catalog -> Catalog -> Search Engine Optimization -> "Use Categories Path for Product URLs" set to "Yes"

enter image description here

After that Reindex using php bin/magento indexer:reindex

1

I used the below code today for a similar thing

public function __construct(
        ...   
             \Magento\Framework\Registry $registry,
                \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
                \Magento\Store\Model\StoreManagerInterface $storeManager
        ...
            ) {
        ...
        $this->storeManager = $storeManager;
        $this->registry = $registry;
        $this->categoryRepository = $categoryRepository;
        ...
    }

...
$categoryIds = $product->getCategoryIds();
    if (!$categoryIds) {
        return null;
    }
    $category = $this->categoryRepository->get($categoryIds[0], $this->storeManager->getStore()->getId());
    $this->registry->register('current_category',$category);
    $redirectionUrl =     $product->getProductUrl();
...
1
  • How to use above code ? Do I need to create custom module for this ?
    – chanchal
    Commented Jul 1, 2019 at 6:20
0

I did this by using following code

<?php $_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); ?>
                        <?php     $cats = $_item->getCategoryIds();
                                if(count($cats) ){

                                    if($cats[0]==2){
                                        $firstCategoryId = $cats[1];
                                    }
                                    else{
                                          $firstCategoryId = $cats[0];
                                    }

                            $_category = $_objectManager->create('Magento\Catalog\Model\Category')->load($firstCategoryId);
                            $categorykey = $_category->getUrlKey();
                            $siteurl= $block->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);      
                            $pro_url=$siteurl.$categorykey."/".$_item->getUrlKey().".html";
                            }
                            else{
                                $pro_url=$block->getProductUrl($_item);
                            }
                     ?>
0

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.