Magento SOAP API – Get Correct Product URL

apimultistoreproductsoapurl

Using the SOAP API, how can I get URL for the product (when requesting from catalog_product.info) that will always work?

Using url_key and appending ".html" is not good enough because in many stores that won't work.

So at the moment I have this:

$url = $shop_url . '/catalog/product/view/id/' . $product_id;

The $shop_url being the domain eg http://www.store.com.

The only issue with that is that the product may not be visible on all stores…. E.g. a single magento may have more than one storefront, each with a different url and each showing a subset of products… so thats hard.

Any ideas?

EDIT: I will also accept an answer which would give me a way to test, positively, that the ID method above would work for different stores.

Best Answer

I'm not sure if this is what you need, but here is a simple extension that allows you to get the product url.
The main idea is to rewrite the info method in the product api model and make it return the url of the product also.
For this you will need the following files:
app/etc/modules/[Namespace]_[Module].xml - the declaration file

<?xml version="1.0"?>
<config>
    <modules>
        <[Namespace]_[Module]>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Catalog />
                <Mage_Api />
            </depends>
        </[Namespace]_[Module]>
    </modules>
</config>

app/code/local/[Namespace]/[Module]/etc/config.xml - the configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <[Namespace]_[Module]>
            <version>0.0.1</version>
        </[Namespace]_[Module]>
    </modules>
    <global>
        <models>
            <catalog>
                <rewrite>
                    <product_api>[Namespace]_[Module]_Model_Product_Api</product_api>
                </rewrite>
            </catalog>
        </models>
    </global>
</config>

app/code/local/[Namespace]/[Module]/Model/Product/Api.php - your new class

<?php
class [Namespace]_[Module]_Model_Product_Api extends Mage_Catalog_Model_Product_Api {
    public function info($productId, $store = null, $attributes = null, $identifierType = null){
        $product = $this->_getProduct($productId, $store, $identifierType);
        $result = parent::info($productId, $store = null, $attributes = null, $identifierType = null);
        //add a new element in here called product_url. You can even wrap it in some condition to see if the product is assigned to the current website or if the product is enabled or visible. ($product->getWebsiteIds(), $product->getStatus(), $product->getVisibility()). 
        $result['product_url'] = $product->getUrlInStore($store);
        return $result;
    }
}

Note: This works only for API V1. In order to make it work for v2 you need to add a wsdl to your module. See how you can do it.