4

In Magento 2, is is possible to get a product's URL using the information returned via the API/Repositories?

That is, when grabbing a list of products from the API, the url_key will be returned as part of the custom_attribute array.

  8 => 
    array (size=2)
      'attribute_code' => string 'url_key' (length=7)
      'value' => string 'joust-duffle-bag' (length=16)

However, the actual product URL in the system will not be returned.

Is there a way, using only the API, get get a product's actual URL?

Or am I mistaken, and calls to the ProductRepository api method should include the URL. If so, what should those calls look like?

Or does this question not make sense due to the way Magento 2 handles product URLs? If so, what's "dumb" about this question?

I realize it's possible, using the provided PHP objects, to use a Magento\Catalog\Model\Product\Url object with an instantiated Magento\Catalog\Model\Product object. What I'm looking for is an "API only" way to return a product's URL.

  • Hi Alan - I don't believe we support that feature. what use cases do you need to support so an API call returns the product page's URL? – Chuck Jan 15 '16 at 22:51
  • @Chuck Theoretical Use Case: Third Party Email Newsletter Application wants to fetch product information to include in a newsletter, and wants a link to the product page so it can embed an HTML link to the product page. – Alan Storm Jan 15 '16 at 23:01
  • Another use case: some affiliate tracking code requires the product URL. – Vinai Dec 14 '16 at 18:44
  • Alan, I very much agree and was told that this feature was in the product backlog. github.com/magento/magento2/issues/10276#issuecomment-316124070 In the meantime, you can use product ID as I showed in my answer. – Scott C Wilson Jul 18 '17 at 19:24
3

After extensive research and two months of being open, it seems the answer to this question is No. The Magento 2 API can't be used to directly fetch a product URL.

2

The value url_key (which is returned by the Products API) can be used to build a URL for some products - for example, product 547 in the test dataset has url_key = "zoltan-gym-tee", and you can do

http://YOUR-SITE.com/zoltan-gym-tee.html

and it works.

You may also use the product ID (returned in the Products API) to build a URL.

For product 541, this would be

http://YOUR-SITE.com/catalog/product/view/id/541

For either of these to work, be sure the visibility flag is set correctly for this product in Admin->Products->Catalog. The visibility must include "Catalog."

  • 1
    +1 for useful information (and that's the work around we ended up going with), but this approach isn't without its problems. What if a system has the .html file extensions turned off for URLs, or has rewrite rules that don't use the url_key? Or what happens with parent/child products (configurable/simple, bundle, etc.) when you start building URLs with raw controller actions and IDs. Easy enough to fix on a local level, hard to know what the right thing to do for redistributable code is. – Alan Storm Jul 18 '17 at 21:06
  • @AlanStorm Agreed! That's why I nagged them to fix. (See my comment on your question.) – Scott C Wilson Jul 18 '17 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.