简体   繁体   中英

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.

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 eg 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. eg 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.

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>') .

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)

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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