简体   繁体   English

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

[英]How to extend Magento API catalog_product.list to include more product information XML-RPC

Ok, here is my situation. 好的,这是我的情况。

We are using Magento Store as a online catalog for an iPad App for a Clothing store. 我们使用Magento Store作为服装店iPad应用程序的在线目录。

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. 从使用XML-RPC的所有标准api调用中,我们设法使我们的iPad应用程序运行良好。

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 eg id and sku. 原因是catalog_product.list只返回有关产品的基本信息,例如id和sku。 So we then have to make a new connection for every product on our list to get the other information we need. 因此,我们必须为列表中的每个产品建立新连接,以获取我们需要的其他信息。 eg Name, Price, Thumb Images . 例如姓名,价格,拇指图像。 Making a new XML-RPC connection for say 100 products is very time consuming. 为100个产品建立新的XML-RPC连接非常耗时。 more than 30 seconds currently. 目前超过30秒。 Naturally after the first load we could store this info locally in the ipad but its importan the first load is fast as well. 自然地,在第一次加载后,我们可以在ipad本地存储此信息,但其重要的第一个加载速度也很快。

Sample Return of current method: catelog_product.list 示例返回当前方法: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) 问题1)

Is there a way to solve this problem with the existing standard Magento API? 有没有办法用现有的标准Magento API解决这个问题?

Question 2) 问题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. 如果没有,那么我需要在哪里更新catalog_product.list方法,以便它包含我们需要的额外信息。

Note: I'm pretty familiar with PHP but I'm not very familar with the exact structure of Magento and its framework. 注意:我对PHP非常熟悉,但我对Magento及其框架的确切结构并不熟悉。

Any help would be greatly appreciated. 任何帮助将不胜感激。

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) 转到\\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()
        );

Add needed attributes here or even write $result[] = $product->getData(); 在这里添加所需的属性甚至可以写$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>') . 上面(我的CE 1.6中的第58行)并添加了行->addAttributeToSelect('<your_attribute_code>')

It is a bad practice to modify code in app/code/core. 修改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 : 我遇到了同样的问题,这就是我在Magento CE 1.9.0.1中使用API​​ V2所做的工作:

  • Create a new module in your app/code/local. 在app / code / local中创建一个新模块。 Don't forget to add the module in your app/etc/modules directory. 不要忘记在app / etc / modules目录中添加模块。

  • In your config.xml, add the following rewrite rules (replace with your class names) : 在config.xml中,添加以下重写规则(替换为您的类名):

    <global> <models> <catalog> <rewrite> <product_api_v2>NS_Catalog_Model_Product_Api_V2</product_api_v2> </rewrite> </catalog> </models> </global> <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. 但这还不够......你必须重载wsdl.xml和wsi.xml。

  • Copy the files from app/code/core/Mage/Catalog/etc/(wsdl|wsi).xml in your module's etc directory. 将app / code / core / Mage / Catalog / etc /(wsdl | wsi).xml中的文件复制到模块的etc目录中。

  • Locate the entity : complexType name="catalogProductEntity" 找到实体: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) 清除缓存(如果你使用PHP客户端,请记住PHP存储了WSDL的副本,默认情况下在/ tmp中)

@Zyava has pointed out the file to modify, which is very useful. @Zyava已指出要修改的文件,这非常有用。 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. 这适用于我的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. 但它有一点问题:如果在Magento管理面板中更改了属性代码,API代码将会中断。 So take extra care not to change the attribute code which is added here. 因此,请特别注意不要更改此处添加的属性代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM