0

I have written a helper function that should take any product from a list.phtml, check if it has a parent (config) and if so return the parent's url instead of the simple product's url.

 public function getProductUrl($product)
    {
    if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) {
        return $product->getProductUrl();
    } else {
        $ids = $this->_config->getParentIdsByChild($product->getId());

        if (isset($ids[0])) {
            $configProduct = $this->_productRepository->getById($ids[0], false, $this->_storeManager->getStore()->getId());
            return $configProduct->getUrlModel()->getUrl($configProduct);
        } else {
            return $product->getProductUrl();
        }
    }
}

It basically works but it does not return the SEO url for the config product. It looks like this: https://www.example.com/catalog/product/view/id/29/s/my-awesome-product/category/4/

For testing I also tried to load the entire product before creating the url, no difference.

What can I do to make it return the SEO url?

Thank you

3 Answers 3

1

You can get a SEO URL using urlKey attribute.

return $this->getBaseUrl().$configProduct->getUrlKey().'.html';
0

First,ensure that your indexing is working fine.And do indexing if require

Then check $configProduct properly loaded

Now, replace

$configProduct->getUrlModel()->getUrl($configProduct);

with

$configProduct->getProductUrl();

1
  • Does not make any difference.
    – omsta
    Jun 19, 2018 at 9:42
0

Ok, I found the underlying issue.

While I had the simple products attached to a category, the configurable product was not. Once I added it to the same(!) category the URL was correct.

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.