如何扩展 Magento API Catalog_product.list 以包含更多产品信息 XML-RPC

发布于 2024-12-10 09:38:46 字数 809 浏览 1 评论 0原文

好的,这是我的情况。

我们使用 Magento Store 作为服装店 iPad 应用程序的在线目录。

有多个类别和数百种产品。

通过使用 XML-RPC 可用的所有标准 api 调用,我们已经成功地让我们漂亮的 iPad 应用程序运行起来。

但加载类别列表确实需要很长时间。原因是catalog_product.list仅返回有关产品的基本信息,例如id和sku。因此,我们必须为列表中的每个产品建立新的连接,以获取我们需要的其他信息。例如名称、价格、缩略图。为 100 个产品建立新的 XML-RPC 连接非常耗时。目前超过30秒。当然,在第一次加载后,我们可以将这些信息本地存储在 ipad 中,但重要的是第一次加载也很快。

当前方法返回示例:catelog_product.list

position = "";
    "product_id" = 805;
    set = 4;
    sku = 1901252;
    type = simple;
},
    {
    position = "";
    "product_id" = 807;
    set = 4;
    sku = 2143405;
    type = simple;
},

问题1)

有没有办法用现有的标准Magento API 来解决这个问题?

问题 2)

如果不是,那么我需要在哪里更新catalog_product.list 方法,以便它包含我们需要的额外信息。

注意:我非常熟悉 PHP,但不太熟悉 Magento 及其框架的确切结构。

任何帮助将不胜感激。

Ok, here is my situation.

We are using Magento Store as a online catalog for an iPad App for a Clothing store.

There are multiple categories and a few hundred products.

From all the standard api calls available to us using XML-RPC we have managed to get our nice iPad application working.

It does how ever take way to long to load category listings. The reason for this is the catalog_product.list only returns basic information about a product e.g. id and sku. So we then have to make a new connection for every product on our list to get the other information we need. e.g. Name, Price, Thumb Images . Making a new XML-RPC connection for say 100 products is very time consuming. more than 30 seconds currently. Naturally after the first load we could store this info locally in the ipad but its importan the first load is fast as well.

Sample Return of current method: catelog_product.list

position = "";
    "product_id" = 805;
    set = 4;
    sku = 1901252;
    type = simple;
},
    {
    position = "";
    "product_id" = 807;
    set = 4;
    sku = 2143405;
    type = simple;
},

Question 1)

Is there a way to solve this problem with the existing standard Magento API?

Question 2)

If not then where do I need to be looking to update the catalog_product.list method so it includes the extra info we need.

Note: I'm pretty familiar with PHP but I'm not very familar with the exact structure of Magento and its framework.

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心病无药医 2024-12-17 09:38:46

转到 \app\code\core\Mage\Catalog\Model\Product\Api.php,找到 items 方法并查看下一段代码(我的 CE 1.6 中的第 80 行)

        $result[] = array( // Basic product data
            'product_id' => $product->getId(),
            'sku'        => $product->getSku(),
            'name'       => $product->getName(),
            'set'        => $product->getAttributeSetId(),
            'type'       => $product->getTypeId(),
            'category_ids'       => $product->getCategoryIds()
        );

在此处添加所需的属性甚至编写 $result[] = $product->getData(); 来获取所有标准属性。如果您需要一些自定义属性,请查看上面的代码

    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addStoreFilter($this->_getStoreId($store))
        ->addAttributeToSelect('name');

(我的 CE 1.6 中的第 58 行)并添加行 ->addAttributeToSelect('')

Go to \app\code\core\Mage\Catalog\Model\Product\Api.php, find items method and look at next piece of code (line 80 in my CE 1.6)

        $result[] = array( // Basic product data
            'product_id' => $product->getId(),
            'sku'        => $product->getSku(),
            'name'       => $product->getName(),
            'set'        => $product->getAttributeSetId(),
            'type'       => $product->getTypeId(),
            'category_ids'       => $product->getCategoryIds()
        );

Add needed attributes here or even write $result[] = $product->getData(); to fetch all standard attributes. If you need some custom attribute there, look at the code

    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addStoreFilter($this->_getStoreId($store))
        ->addAttributeToSelect('name');

above (line 58 in my CE 1.6) and add line ->addAttributeToSelect('<your_attribute_code>').

长发绾君心 2024-12-17 09:38:46

修改 app/code/core 中的代码是一种不好的做法。你必须延长它。

我遇到了同样的问题,以下是我在 Magento CE 1.9.0.1 中使用 API V2 所做的操作:

  • 在您的应用程序/代码/本地中创建一个新模块。不要忘记将模块添加到您的 app/etc/modules 目录中。

  • 在您的 config.xml 中,添加以下重写规则(替换为您的类名):

    <全局>
    <型号>
    <目录>
    <重写>NS_Catalog_Model_Product_Api_V2

然后,创建类:

class NS_Catalog_Model_Product_Api_V2 extends Mage_Catalog_Model_Product_Api_V2
{
    /**
     * Retrieve list of products with basic info (id, sku, type, set, name)
     *
     * @param null|object|array $filters
     * @param string|int $store
     * @return array
     */
    public function items($filters = null, $store = null)
    {
        $collection = Mage::getModel('catalog/product')->getCollection()
            ->addStoreFilter($this->_getStoreId($store))
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('price')
            ->addAttributeToSelect('custom_attribute_1')
            ->addAttributeToSelect('custom_attribute_2') //and so on...
        ;

        /** @var $apiHelper Mage_Api_Helper_Data */
        $apiHelper = Mage::helper('api');
        $filters = $apiHelper->parseFilters($filters, $this->_filtersMap);
        try {
            foreach ($filters as $field => $value) {
                $collection->addFieldToFilter($field, $value);
            }
        } catch (Mage_Core_Exception $e) {
            $this->_fault('filters_invalid', $e->getMessage());
        }
        $result = array();
        foreach ($collection as $product) {
            /** @var Mage_Catalog_Model_Product $product */

            $result[] = array(
                'product_id'   => $product->getId(),
                'price'        => $product->getPrice(),
                'attr_1'       =>  $product->getData('custom_attribute_1'),
                'sku'          => $product->getSku(),
                'name'         => $product->getName(),
                'set'          => $product->getAttributeSetId(),
                'type'         => $product->getTypeId(),
                'category_ids' => $product->getCategoryIds(),
                'website_ids'  => $product->getWebsiteIds()
            );
        }
        return $result;
    }
}

但这还不够......您必须重载 wsdl.xml 和 wsi.xml。

  • 从模块的 etc 目录中的 app/code/core/Mage/Catalog/etc/(wsdl|wsi).xml 复制文件。

  • 找到实体:complexType name="catalogProductEntity"

  • 将自定义属性添加到列表

  • 清除缓存(如果您使用 PHP 客户端,请记住 PHP 存储 WSDL 的副本,默认情况下位于 /tmp)

It is a bad practice to modify code in app/code/core. You will have to extend it.

I had the same issue and here is what I did to get it working with API V2 in Magento CE 1.9.0.1 :

  • Create a new module in your app/code/local. Don't forget to add the module in your app/etc/modules directory.

  • In your config.xml, add the following rewrite rules (replace with your class names) :

    <global>
    <models>
    <catalog>
    <rewrite> <product_api_v2>NS_Catalog_Model_Product_Api_V2</product_api_v2>
    </rewrite>
    </catalog>
    </models>
    </global>

Then, create the class :

class NS_Catalog_Model_Product_Api_V2 extends Mage_Catalog_Model_Product_Api_V2
{
    /**
     * Retrieve list of products with basic info (id, sku, type, set, name)
     *
     * @param null|object|array $filters
     * @param string|int $store
     * @return array
     */
    public function items($filters = null, $store = null)
    {
        $collection = Mage::getModel('catalog/product')->getCollection()
            ->addStoreFilter($this->_getStoreId($store))
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('price')
            ->addAttributeToSelect('custom_attribute_1')
            ->addAttributeToSelect('custom_attribute_2') //and so on...
        ;

        /** @var $apiHelper Mage_Api_Helper_Data */
        $apiHelper = Mage::helper('api');
        $filters = $apiHelper->parseFilters($filters, $this->_filtersMap);
        try {
            foreach ($filters as $field => $value) {
                $collection->addFieldToFilter($field, $value);
            }
        } catch (Mage_Core_Exception $e) {
            $this->_fault('filters_invalid', $e->getMessage());
        }
        $result = array();
        foreach ($collection as $product) {
            /** @var Mage_Catalog_Model_Product $product */

            $result[] = array(
                'product_id'   => $product->getId(),
                'price'        => $product->getPrice(),
                'attr_1'       =>  $product->getData('custom_attribute_1'),
                'sku'          => $product->getSku(),
                'name'         => $product->getName(),
                'set'          => $product->getAttributeSetId(),
                'type'         => $product->getTypeId(),
                'category_ids' => $product->getCategoryIds(),
                'website_ids'  => $product->getWebsiteIds()
            );
        }
        return $result;
    }
}

But it is not enough... you will have to overload the wsdl.xml and wsi.xml.

  • Copy the files from app/code/core/Mage/Catalog/etc/(wsdl|wsi).xml in your module's etc directory.

  • Locate the entity : complexType name="catalogProductEntity"

  • Add your custom attributes to the list

  • clear the caches (if you use a PHP client, keep in mind that PHP stores a copy of the WSDL, by default in /tmp)

疾风者 2024-12-17 09:38:46

@Zyava 指出了要修改的文件,这非常有用。
但我使用了另一种方法来更改此文件:

$result[] = array( // Basic product data
    'product_id' => $product->getId(),
    'sku'        => $product->getSku(),
    'name'       => $product->getName(),
    'set'        => $product->getAttributeSetId(),
    'type'       => $product->getTypeId(),
    'category_ids'       => $product->getCategoryIds(),
    '<your_attribute_code>' => $product->getData('<your_attribute_code>')
);

只需为要检索的属性添加一行即可。
这在我的 CE 1.6.1 上有效。但它有一个小问题:如果在 Magento 管理面板中更改属性代码,API 代码将被破坏。因此请格外小心,不要更改此处添加的属性代码。

@Zyava has pointed out the file to modify, which is very useful.
But I used another way to change this file:

$result[] = array( // Basic product data
    'product_id' => $product->getId(),
    'sku'        => $product->getSku(),
    'name'       => $product->getName(),
    'set'        => $product->getAttributeSetId(),
    'type'       => $product->getTypeId(),
    'category_ids'       => $product->getCategoryIds(),
    '<your_attribute_code>' => $product->getData('<your_attribute_code>')
);

Just add a line for an attribute you want to retrieve.
This worked on my CE 1.6.1. But there is a little problem with it: if the the attribute code is changed in Magento admin panel, the API code will break. So take extra care not to change the attribute code which is added here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文