【问题标题】:Get product link from Magento API从 Magento API 获取产品链接
【发布时间】:2009-11-19 17:49:30
【问题描述】:

我是 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 调用的产品信息,但它没有。

【问题讨论】:

    标签: api url magento product


    【解决方案1】:

    Magento Product api 不提供此类功能

    虽然有一些简单的方法可以在自定义模块中扩展特定 API,但如果您不想编写自定义模块,这是最快的方法(因为我认为对于新的 magento 开发人员来说很难)。

    在编辑任何内容之前,将原始产品 API 类从 core 复制到 local 文件夹(这样您的 Magento 安装将保持“更新保存”)。


    • app/code/core/Mage/Catalog/Model/Product/Api.php复制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 字段。

    【讨论】:

    • 这并不完全正确。事实上,文件app/code/core/Mage/Catalog/Model/Product.api 甚至都不存在。这是正确的路径:app\code\core\Mage\Catalog\Model\Product\Api\V2.php
    【解决方案2】:

    其实即使你的路径是错误的,他指的是 app\code\core\Mage\Catalog\Model\Product\Product.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      • 2016-09-17
      相关资源
      最近更新 更多