English 中文(简体)
Get product link from Magento API
原标题:

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.

问题回答

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.

Well actually even your path is wrong, the one he is referring to is appcodecoreMageCatalogModelProductProduct.php





相关问题
C# Networking API s [closed]

Lately I ve been looking for a good networking API i could possibly use and/or reference some of the code within, but i have mere luck searching for some on Google/Bing. Hopefully somebody here has ...

getting XML from other domain using ASP.NET

I m fairly new to ASP.NET. And I was wondering how I could go about getting xml from a site (Kuler s API in this case), and then post the result using AJAX? So what I want here, is to be able to do a ...

Most appropriate API for URL shortening service

I ve just finished an online service for shortening URLs (in php5 with Zend Framework); you can enter an URL and you get an short URL (like tinyurl and such sites). I m thinking about the API for ...

UML Diagram to Model API

I need to create a diagram to document a RESTFul API that build, which UML diagram should I use? Thanks in advance,

How best to expose Rails methods via an API?

Let s say I have a model foo, and my model has a publish! method that changes a few properties on that model and potentially a few others too. Now, the Rails way suggests that I expose my model over ...

简讯

我是否可以使用某些软件来建立简便的航天国家服务器,最好是在 Java? 所有我都希望我的航天国家服务机在任何要求中都用同样的IP地址来回答。

About paypal express checkout api

In this picture,there are 3 main steps:SetExpressCheckout,GetExpressCheckoutDetails and DoExpressCheckoutDetails,I m now sure SetExpressCheckout is to be called by myself,what about ...

热门标签