我是 Magento 的新手并使用他们的 API。我需要能够从 API 调用中获取产品 url。我看到我可以访问 url_key 和 url_path,但不幸的是,这不一定是产品的 URL(即它可能是 category/my-product-url.html),其中 url_key 将包含 my-product-url 和 url_path仅包含 my-product-url.html。更复杂的是,它甚至可能是 /category/sub-category/my-product-url.html。那么,如何获得包含类别的完整 url 以及在 url 重写信息中设置的所有内容?似乎这应该来自 product.info api调用的产品信息,但它没有。
问问题
13353 次
2 回答
3
Magento Product api 不提供此类功能
虽然有一些简单的方法可以在自定义模块中扩展特定的 API,但是如果您不想编写自定义模块,这是最快的方法(因为我认为这对于新的 magento 开发人员来说很困难)。
在编辑任何内容之前,将原始产品 API 类从 复制core
到local
文件夹(这样您的 Magento 安装将保持“更新保存”)。
- 从以下位置复制 Api.php:
app/code/core/Mage/Catalog/Model/Product/Api.php
- 至:
app/code/local/Mage/Catalog/Model/Product/Api.php
现在更改info
复制文件中的方法以包含full_url
. 将以下行添加到$result
-array。(确保在数组行的末尾设置必要的逗号。)
'full_url' => $product->getProductUrl(),
您生成的方法代码应如下所示:
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;
}
之后您可以通过 API调用product.info
和使用该字段。full_url
于 2009-11-20T00:32:00.373 回答
-1
实际上,即使你的路径是错误的,他指的是 app\code\core\Mage\Catalog\Model\Product\Product.php
于 2010-12-22T11:41:19.233 回答