Get product link from Magento API

13,349

Magento Product api does not provide such functionality

Although there are easy ways to extend specific API in custom modules, but here is the quickest way if you don't want to write a custom module ( as i think it's difficult for a new magento developer).

Copy the original product API-class from the core to the local folder before editing anything (that way your Magento installation stays "update-save").

  • copy Api.php from:
    app/code/core/Mage/Catalog/Model/Product/Api.php
  • to:
    app/code/local/Mage/Catalog/Model/Product/Api.php

Now change the info method within the copied file to include the full_url. Add the following line to the $result-array. (Make sure to set necessary commas at the end of the array-lines.)

'full_url' => $product->getProductUrl(),

Your resulting method code should look like:

public function info($productId, $store = null, $attributes = null, $identifierType = null)
{
    $product = $this->_getProduct($productId, $store, $identifierType);


    $result = array( // Basic product data
        'product_id' => $product->getId(),
        'sku'        => $product->getSku(),
        'set'        => $product->getAttributeSetId(),
        'type'       => $product->getTypeId(),
        'categories' => $product->getCategoryIds(),
        'websites'   => $product->getWebsiteIds(),
        'full_url'   => $product->getProductUrl(),
    );

    foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
        if ($this->_isAllowedAttribute($attribute, $attributes)) {
            $result[$attribute->getAttributeCode()] = $product->getData(
                                                            $attribute->getAttributeCode());
        }
    }

    return $result;
}

Afterwards you can call product.info and use the full_url field via the API.

Share:
13,349
Ryan
Author by

Ryan

Updated on June 04, 2022

Comments

  • Ryan
    Ryan almost 2 years

    I am new to Magento and using their API. I need to be able to get the product url from the API call. I see that I can access the url_key and url_path, but unfortunately that's not necessarily what the URL for the product is (ie it may be category/my-product-url.html) where url_key would contain my-product-url and url_path would only contain my-product-url.html. Further complicating things, it may even be /category/sub-category/my-product-url.html. So, how would I get the full url with the category and everything as it is setup in the url rewrite information? Seems like this should come with the product information from the product.info api call but it doesn't.